import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# 设置画布大小
plt.figure(1, figsize=(26, 13))
# 获取坐标轴
ax = plt.gca()
for label in ax.get_xticklabels():
# 横轴标签旋转 30°
label.set_rotation(30)
label.set_horizontalalignment('right')
# 画图 x 、y、颜色、形状
plt.plot(df_data_index,ts, 'b', marker = '*')
plt.plot(df_data_index, trend,'r', marker='o')
plt.plot(df_data_index, trend_['tem_up_threshhold'], 'g--')
plt.plot(df_data_index, trend_['tem_low_threshhold'], 'g--')
#显示图例
plt.legend(loc='upper left')
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S'))#设置时间标签显示格式
ax.xaxis.set_major_locator(mdates.HourLocator())#X轴的间隔为小时
# 保存画图
png_dir = './rule_png/' + file_name
if(not os.path.exists(png_dir)):
os.makedirs(png_dir)
png_path = os.path.join(png_dir, date_ymd + '_' + str(len(df_data)) + '.png')
plt.savefig(png_path)
# 显示画图
plt.show()
python matplotlib 画图
最新推荐文章于 2024-06-20 16:34:51 发布