x and y must have same first dimension, but have shapes (119,) and (120,)
解决方法:保证维度相同
import random
from matplotlib import pyplot as plt
# x表示时间120分钟
x = range(1,120) 此处修改为121
y = [random.randint(20,35) for i in range(120)]
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y)
plt.savefig("pract2.png")
plt.show()
x = range(1,121) y = [random.randint(20,35) for i in range(120)] xy维度保持一致
from matplotlib import pyplot as plt
import random
import matplotlib
from matplotlib import font_manager
f1=font_manager.FontProperties(fname=r"C:/Windows/Fonts/simsun.ttc")
y_1 = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
y_2 = [1,0,3,1,2,2,3,3,2,1 ,2,1,1,1,1,1,1,1,1,1]
x = range(11,31)
#设置图形大小
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y_1,label="自己",color="#F08080")
plt.plot(x,y_2,label="同桌",color="#DB7093",linestyle="--")
_x=list(x)
x_label=["{}岁".format(i) for i in range(11,31)]
plt.xticks(_x,x_label,rotation=45,fontproperties=f1)
plt.yticks(range(0,9))
plt.grid(alpha=0.5)
plt.legend(prop=f1,loc="upper left")
plt.show()
展示图像