1.plot折线图:
import matplotlib.pyplot as plt
x = [1,2,3]
y = [5,7,4]
x2 = [1,2,3]
y2 =[10,14,12]
plt.plot(x,y,label='first line')
plt.plot(x2,y2,label='second line')
plt.xlabel('Plot Number')
plt.ylabel('Important var')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.show()
2.bar柱状图
import matplotlib.pyplot as plt
x = [2,4,6,8,10]
y = [8,6,2,5,6]
x2 = [1,2,3,4,10]
y2 =[10,14,12,5,7]
x3 = [1,3,5,7,9]
y3 = [2,3,5,7,4]
plt.bar(x,y,label='first line',color='g')
plt.plot(x2,y2,label='second line')
plt.bar(x3,y3,label='third line',color='b')
plt.xlabel('Plot Number')
plt.ylabel('Important var')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.show()
3.hist显示所在区间占有比例
import matplotlib.pyplot as plt
population_ages = [22,55,62,45,21,22,34,42,4,99,102,110]
bins = [0,10,30,40,50,70,80,90,100,110]
plt.hist(population_ages,bins,color='r') #显示在bins区间上population_ages所占的比例
plt.xlabel('Plot Number')
plt.ylabel('Important var')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.show()
4.stockplot堆栈式的
import matplotlib.pyplot as plt
days = [1,2,3,4,5]
sleeping = [7,8,6,11,7]
eating = [2,3,4,2,3]
working = [7,8,9,8,7]
playing = [8,5,7,8,13]
plt.plot([],[],color='r',label='sleeping', linewidth=5)
plt.plot([],[],color='b',label='eating', linewidth=5)
plt.plot([],[],color='g',label='working', linewidth=5)
plt.plot([],[],color='m',label='playing', linewidth=5)
plt.stackplot(days, sleeping, eating, working, playing, colors=['r','b','g','m',])
plt.xlabel('Plot Number')
plt.ylabel('Important var')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.show()
5.pie饼状图
import matplotlib.pyplot as plt
days = [1,2,3,4,5]
sleeping = [7,8,6,11,7]
eating = [2,3,4,2,3]
working = [7,8,9,8,7]
playing = [8,5,7,8,13]
slices = [7,2,2,13]
activities = ['sleeping', 'eating', 'working', 'playing']
plt.pie(slices, labels=activities, colors=['r','c','b','g'], startangle=90, shadow=True, explode=(0,0.1,0,0))
plt.xlabel('Plot Number')
plt.ylabel('Important var')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.show()
本文介绍了使用Matplotlib绘制五种常见图表的方法,包括折线图、柱状图、直方图、堆栈图和饼状图,并提供了详细的代码示例。
771

被折叠的 条评论
为什么被折叠?



