Python-matplotlib基本操作   发布时间:2017-09-03 13:10:28

Python-matplotlib基本操作

废话不多说,直接来干货。
1.用matplotlib画函数图:

import numpy as np from matplotlib import pyplot as plt x = np.arange(0, 3 * np.pi,0.1) siny=np.sin(x) cosy=np.cos(x) plt.plot(x,siny,label='sin') plt.plot(x,cosy,label='cos') plt.title('sin and cos') plt.xlabel('xlabel') plt.ylabel('ylabel') plt.legend(loc=4) plt.axis([0,9,-3,3]) plt.show()

画函数图

2.画函数图进阶vcD4NCjxwcmUgY2xhc3M9"brush:java;"> import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np x = np.arange(0.0,6,0.1) plt.plot(x, [xi**2 for xi in x],label = 'First',linewidth = 4,color = (0,0,0)) plt.plot(x, [xi**2+2 for xi in x],'g',label = 'second') plt.plot(x, [xi**2+5 for xi in x],color = (1,0,1,1),label = 'Third') plt.plot(x, [xi**2+9 for xi in x],color = '#BCD2EE',label = 'Fourth') plt.axis([0,7,-1,50]) plt.xticks(np.arange(0.0,6.0,2.5)) plt.xlabel("x",fontsize=15) plt.ylabel(r'y') plt.title('simple plot') plt.legend(loc = 1) #改变图标位置 plt.grid(True) plt.savefig('simple plot.jpg',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 plt.show()

进阶

3.画柱状图/饼状图

import matplotlib.pyplot as plt from scipy.misc import imshow,imread import numpy as np dict = {'A': 40, 'B': 70, 'C': 30, 'D': 85} plt.figure(figsize=(10,5)) plt.subplot(1,2,2) keys=[] values=[] for key in dict: keys+=[key] values+=[dict[key]] plt.pie(values,labels=keys,autopct='%0.2f') a=[] plt.subplot(1,2,1) for i, key in enumerate(dict): plt.bar(i, dict[key]) a+=[dict[key]] plt.xticks(np.arange(len(dict)), dict.keys()) plt.yticks(np.array(a)) #plt.grid(True) plt.savefig('picture.jpg',dpi=200) I=imread('picture.jpg') imshow(I) plt.show()

柱状图和饼状图

4.饼状图和折线图对比

import matplotlib.pyplot as plt import matplotlib as mpl plt.figure(figsize=(15,5)); 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.subplot(1,2,1) plt.pie(x, labels=labels, explode=explode, autopct='%0.2f') plt.subplot(1,2,2) #plt.pie(x, labels=labels, explode=explode, autopct='%0.2f') plt.plot(x) plt.xticks([0,1,2,3,4,5],[1,2,3,4,5,6]) plt.show()

饼状图和折线图

5.散点图

import matplotlib.pyplot as plt import numpy as np x = np.random.randn(12,20) y = np.random.randn(12,20) mark = ['s','o'] for i in range(0,2): plt.scatter(x[i],y[i],marker = mark[i],color =(np.random.rand(1,3)),s=50,label = str(i+1)) #s是调整点的大小的 plt.legend(loc=1) plt.show()

散点图

6.频率分布直方图

import numpy as np from matplotlib import pyplot as plt lenths=np.random.randn(1000) def draw_hist(lenths): plt.figure(figsize=(5,5)) plt.hist(lenths,100) #第一个参数是数据(列表) ,第二个参数是划分多少块 plt.xlabel('lenth') plt.xlim(-5,5) plt.ylabel('Frequency') plt.title('nomalize') plt.savefig('normalize.jpg',dpi=200) plt.show() draw_hist(lenths)

频率分布直方图

7.柱状图和条形图

import matplotlib.pyplot as plt import numpy as np def bar_chart_generator(): l=[1,2,3,4,5] l1 = ['a','b','c','d','e'] h = [20, 14, 38, 27, 9] w = [0.1, 0.2, 0.3, 0.4, 0.5] b = [1,2,3,4,5] plt.subplot(1,2,1) # for i in l: # plt.bar(l[i-1], h[i-1],w[i-1],b[i-1])#若循环打印,则各条纹颜色会不同 plt.bar(l, h,w,b,color=(0,0,0)) plt.xticks(l,l1) plt.subplot(1,2,2) plt.barh(l,h) plt.yticks(l,l1) plt.savefig('bar.png') bar_chart_generator()

柱状图和条形图

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:天门网站建设 http://tianmen.45qun.com