气温随时间变化的规律
为寻找当地气温随时间变化的规律,对四十天内气温的变化进行统计得到如下数组:
[32, 25, 15, 19, 18, 23, 27, 23, 24, 24, 24, 25, 17, 17, 17, 22, 26, 27, 28, 28, 20, 17, 19, 19, 23, 19, 19, 23, 24, 26, 27, 28, 28, 29, 26, 27, 29, 30, 29, 29]
from matplotlib import pyplot as plt
import matplotlib
matplotlib.rc("font",family='FangSong')
temperature_1 = [32, 25, 15, 19, 18, 23, 27, 23, 24, 24, 24, 25, 17, 17, 17, 22, 26, 27, 28, 28, 20, 17, 19, 19, 23, 19, 19, 23, 24, 26, 27, 28, 28, 29, 26, 27, 29, 30, 29, 29]
temperature_2 = [24, 17, 15, 20, 18, 21, 23, 23, 23, 23, 24, 22, 17, 18, 17, 20, 24, 27, 29, 28, 17, 17, 21, 22, 25, 21, 20, 23, 25, 23, 27, 29, 29, 28, 24, 26, 29, 30, 27, 25]
count = 0
for i in temperature_1:
count +=1
x = range(1,count+1,1)
y_1 = temperature_1
y_2 = temperature_2
_xticks_labels = ["第{}日".format(i) for i in x]
plt.xticks(x[::3], _xticks_labels[::3], rotation=45)
plt.grid(alpha=0.3)
plt.scatter(x, y_1, label="临汾")
plt.scatter(x, y_2, label="太原")
plt.legend()
plt.xlabel("时间", loc="right")
plt.ylabel("温度(单位:摄氏度)", loc="top")
plt.title("四十日两地气温变化散点图")
plt.subplots_adjust(left=0.1, right=0.97, top=0.95, bottom=0.15)
plt.savefig("./scatter.png")
plt.show()
- 绘制命令为
plt.scatter()
- plt.subplots_adjust(left=0.1, right=0.97, top=0.95, bottom=0.15) :
- 根据需要对图片内容与边缘的距离进行调整,数值范围为0~1;
- 距离参数以左边框与下边框为基准;
该代码运行结果为: