导包
-
import numpy as np
-
import matplotlib.pyplot as plt
代码
网格线图
-
ypoints = np.array([1,3,9,25,12,32,5,1])
-
数据插入到图表
-
marker:符号
-
color:颜色,可简写为c
-
linewidth:线条宽度
-
#plt.plot(ypoints,'o:g')
-
#plt.plot(ypoints,marker='o',linestyle=':',color='g')
-
plt.plot(ypoints,marker='o',ls=':',c='g',linewidth='1.5')
marker可以定义的符号如下:
散点图
-
import numpy as np
-
import matplotlib.pyplot as plt
-
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
-
y = np.array([1, 4, 9, 16, 7, 11, 23, 18])
-
sizes = np.array([20,50,100,200,500,1000,60,90])
-
colors=np.array(["red","green","black","orange","purple","beige","cyan","magenta"])
-
plt.subplot(1,2,1)
-
plt.scatter(x, y, s=sizes,c=colors,cmap='afmhot_r') #viridis颜色条
-
plt.title("scatter")
-
plt.colorbar() #颜色条输出
-
x2 = np.random.rand(50)
-
y2 = np.random.rand(50)
-
colors2 = np.random.rand(50)
-
plt.subplot(1,2,2)
-
plt.scatter(x2,y2,c=colors2,cmap='afmhot_r')
-
plt.title("scatter2")
-
plt.colorbar()
-
plt.show()
柱形图
-
import matplotlib.pyplot as plt
-
import numpy as np
-
#定义数据
-
x=np.array(['math','english','chinese','history','physic'])
-
y=np.array([88,77,66,55,99])
-
c=np.array(["#4CAF50","red","hotpink","#556B2F","pink"])
-
plt.subplot(1,2,1)
-
#插入数据显示
-
plt.bar(x,y,color=c,width=0.5)
-
plt.title('score')
-
plt.xlabel('course')
-
plt.ylabel('grade')
-
plt.subplot(1,2,2)
-
#定义数据
-
x=np.array(['math','english','chinese','history','physic'])
-
y=np.array([88,77,66,55,99])
-
plt.bar(x,y,color='pink',width=0.5)
-
plt.title('score')
-
plt.xlabel('course')
-
plt.ylabel('grade')
-
plt.show()