import numpy as np
import matplotlib.pyplot as plt
N=5
y= [20,10,15,13,13]
index = np.arange(N)
pl = plt.bar(left=index,height=y,color='blue',width=0.8)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
N=5
y= [20,10,15,13,13]
index = np.arange(N)
pl = plt.bar(left=0,bottom=index,width=y,color='red',height=0.5,orientation='horizontal')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
N=5
y= [20,10,15,13,13]
index = np.arange(N)
pl = plt.barh(left=0,bottom=index,width=y) #用barh函数
plt.show()
2.多个项目在一起的条形图(在开始的时候加上一个线宽,+bar_width)
import numpy as np
import matplotlib.pyplot as plt
index=np.arange(5)
sales_A = [20,10,15,13,13]
sales_B = [12,14,14,15,15]
bar_width = 0.3
pl = plt.bar(index,sales_A,bar_width,color='r')
pl = plt.bar(index+bar_width,sales_B ,bar_width,color='b')
plt.show()
3.生成层叠的条形图
import numpy as np
import matplotlib.pyplot as plt
index=np.arange(5)
sales_A = [20,10,15,13,13]
sales_B = [12,14,14,15,15]
bar_width = 0.3
pl = plt.bar(index,sales_A,bar_width,color='r')
pl = plt.bar(index,sales_B ,bar_width,color='b')
plt.show()
2.直方图的画法
import numpy as np
import matplotlib.pyplot as plt
mu = 100 #均值
sigma = 20 # 标准差
x= mu + sigma*np.random.randn(2000)
plt.hist(x,bins= 100,color = 'red',normed=True)# 直方图用hist函数,三个比较常用的函数,几个直方,bins ,normed 是指的是否进行标准化
plt.show()
import numpy as np
import matplotlib.pyplot as plt
x=np.random.randn(1000)+2
y= np.random.randn(1000)+3
plt.hist2d(x,y,bins=40)
plt.show()
绘制饼状图
import numpy as np
import matplotlib.pyplot as plt
labels='A', 'B','D','C'
explode=[0,0.2,0,0]
fraces = [15,30,45,10]
plt.axes(aspect=1) #正圆的比例是1
plt.pie(x=fraces,labels =labels,autopct='%.of%%',explode=explode,shadow=True) #autopct是显示每个部分所占的比例,explode是突出显示某个部分
#0.5是距离中心的位置,shadow是是否利用立体阴影
plt.show()
4.箱型图又称为盒须图 上边缘,上四分位数,中位数,下四分位数,下边缘,异常值