importmatplotlib.pyplot as pltimportnumpy as np
x= np.linspace(-10, 20, 50, True)
y= 2 * x + 5
#一、画散点图
plt.scatter(x,y,s=40,c='Orange',edgecolors='k',marker='s',alpha=0.5)#s:大小;c:内部颜色;edgecolors:边框颜色;marker:点的形状;alpha:透明度
plt.show()
matplot画板颜色请点击这里;点的形状请点击这里#二、画折线图与线段
plt.figure()
plt.plot(x, y, label='line1')
plt.plot(x, y+ 4, c='r', lw=2, ls=':', label='line2')#c:颜色;lw:线宽;ls:线段类型,有“:”“--”“-”等;lebel:画图图例;
plt.plot([-10, 0], [-10, 0], ls='--', label='line3') #两点连线
plt.legend()
plt.show()#三、画柱状图#1、单一柱状图
plt.figure()
bar_x= np.arange(5)
bar_y= np.arange(5)
rects= plt.bar(bar_x, height=bar_y, width=0.8,orientation='vertical')#给每个柱添加数据标识
for rect inrects:
plt.text(rect.get_x()+ rect.get_width() / 2, rect.get_height(), rect.get_height(), ha='center', va='bottom')
plt.show()#2、横向柱状图#method 1 :
plt.figure()
plt.barh(bar_y,bar_x,height=0.8,left=0) #left柱状图左边的起始位置,0表示从x=0开始
plt.show()#method 2 :
plt.figure()
plt.bar(x=0,bottom=bar_x, width=bar_y, height=0.5,orientation='horizontal') #bottom:柱状图底部起始位置为y=bar_x,orientation设置柱状图方向
plt.show()#3、并列图
plt.figure()
plt.bar(bar_x, height=bar_y, width=0.2,orientation='vertica