Python 画图

使用python的科学计算库,达到快速计算的效果。

标准的Python中用列表(list)保存一组值,可以当作数组使用。但由于列表的元素可以是任何对象,因此列表中保存的是对象的指针。这样一来,为了保存一个简单的列表[1,2,3],就需
要有三个指针和三个整数对象。对于数值运算来说,这种结构显然比较浪费内存和 CPU 计算时间。

使用numpy的array模块可以解决这个问题。细节不在此赘述。这里主要记录一些matplotlib的基本使用方法

  • first plot

#first plot with matplotlib

import matplotlib.pyplot as plt

plt.plot([1,3,2,4])

plt.show()

in order to avoid pollution of global namespace, it is strongly recommended to never import like:

from <module> import *

  • simple plot

import matplotlib as mpl

import matplotlib.pyplot as plt

import numpy as np

x = np.arange(0.0,6.0,0.1)

plt.plot(x, [xi**2 for xi in x],label = 'First',linewidth = 4,color = 'black')

plt.plot(x, [xi**2+2 for xi in x],label = 'second',color = 'red')

plt.plot(x, [xi**2+5 for xi in x],label = 'third')

plt.axis([0,7,-1,50])

plt.xlabel(r"$\alpha$",fontsize=20)

plt.ylabel(r'y')

plt.title('simple plot')

plt.legend(loc = 'upper left')

plt.grid(True)

plt.savefig('simple plot.pdf',dpi = 200)

print mpl.rcParams['figure.figsize'] #return 8.0,6.0

print mpl.rcParams['savefig.dpi'] #default to 100 the size of the pic will be 800*600

#print mpl.rcParams['interactive']

plt.show()

Python-3

  • Decorate plot with styles and types

import matplotlib as mpl

import matplotlib.pyplot as plt

import numpy as np

x = np.arange(0.0,6.0,0.1)

plt.plot(x, [xi**2 for xi in x],label = 'First',linewidth = 4,color = 'black') #using color string to specify color

plt.plot(x, [xi**2+2 for xi in x],'r',label = 'second') #using abbreviation to specify color

plt.plot(x, [xi**2+5 for xi in x],color = (1,0,1,1),label = 'Third') #using color tuple to specify color

plt.plot(x, [xi**2+9 for xi in x],color = '#BCD2EE',label = 'Fourth') #using hex string to specify color

plt.xticks(np.arange(0.0,6.0,2.5))

plt.xlabel(r"$\alpha$",fontsize=20)

plt.ylabel(r'y')

plt.title('simple plot')

plt.legend(loc = 'upper left')

plt.grid(True)

plt.savefig('simple plot.pdf',dpi = 200)

print mpl.rcParams['figure.figsize'] #return 8.0,6.0

print mpl.rcParams['savefig.dpi'] #default to 100 the size of the pic will be 800*600

#print mpl.rcParams['interactive']

plt.show()

image

  • types of graph

image

  • Bars

import matplotlib.pyplot as plt

import numpy as np

dict = {'A': 40, 'B': 70, 'C': 30, 'D': 85}

for i, key in enumerate(dict): plt.bar(i, dict[key]);

plt.xticks(np.arange(len(dict))+0.4, dict.keys());

plt.yticks(dict.values());

plt.grid(True)

plt.show()

image_1

  • Pies

import matplotlib.pyplot as plt

plt.figure(figsize=(10,10));

x = [4, 9, 21, 55, 30, 18]

labels = ['Swiss', 'Austria', 'Spain', 'Italy', 'France',

'Benelux']

explode = [0.2, 0.1, 0, 0, 0.1, 0]

plt.pie(x, labels=labels, explode=explode, autopct='%1.1f%%');

plt.show()

image_2

  • Scatter

import matplotlib.pyplot as plt

import numpy as np

x = np.random.randn(12,20)

y = np.random.randn(12,20)

mark = ['s','o','^','v','>','<','d','p','h','8','+','*']

for i in range(0,12):

plt.scatter(x[i],y[i],marker = mark[i],color =(np.random.rand(1,3)),s=50,label = str(i+1))

plt.legend()

plt.show()

image_4

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

一篇文章带你了解JavaScript日期

日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用

java将前端的json数组字符串转换为列表

记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang

Python 画图 - HelloWorld