matplotlib简明教程

简介

直线例子

import matplotlib.pyplot as plt
import numpy as np
plt.plot([1,3,2,4]) #这里画第一条线,只设置了纵坐标,横坐标默认为N-1
x=range(6)
plt.plot(x,[i**2 for i in x]) #这里画第二条线,分别指定了x,y轴的值
x1=np.array([1,2,3])  #使用numpy的数组
plt.plot(x1,x1*2)
plt.plot(x,[i**1.5 for i in x],x,[i**0.5 for i in x])#同时画2条线
plt.grid(True) #显示网格
plt.xlim(0,10) #设置x,y轴
plt.xticks(x,['a','b','c']) #设置x轴的显示字符
plt.ylim(0,10)
plt.xlabel("this is x")  #设置x,y的label
plt.ylabel("this is y")
plt.title('title1')  #设置标题
plt.plot(x,x*2,'--yo',label='normal') #设置plot的label和颜色,y表示黄色,‘--’表示直线显示为虚线,'o'表示坐标点用圆圈表示。
plt.legend() #显示plot的label
plt.show()
plt.savefig('test.png') #保存图片

散点图

import numpy as np
import maplotlib.pyplot as plt

n = 1024
X = np.random.normal(0,1,n)
Y = np.random.normal(0,1,n)

plt.scatter(X,Y)
plt.show()

柱状图

import numpy as np
import maplotlib.pyplot as plt

n = 12
X = np.arange(n)
Y1 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n)
Y2 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n)

plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')
plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')

for x,y in zip(X,Y1):
    plt.text(x+0.4, y+0.05, '%.2f' % y, ha='center', va= 'bottom')

plt.ylim(-1.25,+1.25)
plt.show()

饼图

import numpy as np
import maplotlib.pyplot as plt

n = 20
Z = np.random.uniform(0,1,n)
plt.pie(Z)
plt.show()

更多种类的图:http://www.labri.fr/perso/nrougier/teaching/matplotlib/#other-types-of-plots

你可能感兴趣的:(python,数据挖掘)