循环作图

import pandas as pd
import matplotlib as mpl

mpl.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
from matplotlib.ticker import IndexLocator

zhfont1 = fm.FontProperties(fname='/home/gzblue/xgx/xx_copy_files/SIMKAI.TTF')

if __name__ == '__main__':

guo_shi_kong = pd.read_csv(r'/home/gzblue/xgx/new_weizhan/o3_shikong_windows.csv', encoding='gbk')
guokong = guo_shi_kong.loc[:, ['DATA_TIME_', '三瓦窑', '十里店', '君平街', '大石西路', '沙河铺', '灵岩寺', '金泉两河']]
guokong_list = ['三瓦窑', '十里店', '君平街', '大石西路', '沙河铺', '灵岩寺', '金泉两河']
new = guo_shi_kong.loc[:, [i for i in guo_shi_kong.columns if i not in guokong_list]]
guo_shi_kong_list = list(guo_shi_kong.columns)
shikong_list = list(set(guo_shi_kong_list) - set(guokong_list))
shikong_list.sort() # 字母会排前面
shikong = guo_shi_kong.loc[:, shikong_list]
# print(shikong.columns)
n = 0
while n < 10518:
n1 = n
n2 = n
n2 = n2 + 168
n = n2
time_08_01 = guokong.loc[n1:n2, 'DATA_TIME_'].values
# print('!!!!!!$$$$$$$2$$$$$$$$$$$$',time_08_01)
guokong_08_01 = guokong.loc[n1:n2, :]
shikong_08_01 = shikong.loc[n1:n2, :]
x_all_labels = [i.split(r'-')[-1][:-6].replace(' ', '-') for i in time_08_01]
x_labels_index_values = [i for i in x_all_labels if '-16' in i]
# print(x_labels_index_values)
xmajorLocator = IndexLocator(base=24, offset=4) # 将x主刻度标签设置为20的倍数
xminorLocator = IndexLocator(base=1, offset=0) # 将x次刻度标签设置为1的倍数

# #国控站作图
# guokong_dict={'sanwajiao':'三瓦窑','shilidian':'十里店','junpingjie':'君平街','dashixilu':'大石西路','shahepu':'沙河铺','lingyansi':'灵岩寺','jinquanliangqu':'金泉两河'}
# i=-1
# width=0.25
# fig, ax = plt.subplots(7, 1, figsize=(10, 21))
# for key in guokong_dict.keys():
# guokong_dict[key]=guokong_08_01[guokong_dict[key]].values
# i=i+1
# ax[i].set_ylabel(r'o3(μg/m3)' ,color='b', fontproperties=zhfont1, size=18)
# ax[i].tick_params('y', colors='b')
# ax[i].set_xlim(-width, len(time_08_01) + width - 1)
# ax[i].set_title(r'国控站_'+str(key)+str( time_08_01[0])+'-'+str(time_08_01[168])+r'_o3浓度(μg/m3)', fontproperties=zhfont1,size=22)
# # ax[i].set_title(r'国控站_'+str(key)+r'2017/8/1/12:00:00—2017/8/8/12:00'+r'_o3浓度(μg/m3)', fontproperties=zhfont1,size=22)
# ax[i].plot(time_08_01, guokong_dict[key], 'b-')
# ax[i].set_xticks(range(len(time_08_01)))
# ax[i].set_xticklabels(x_labels_index_values, fontproperties=zhfont1, size=18, rotation=45) # x轴的标签字体的旋转
# ax[i].xaxis.set_minor_locator(xminorLocator)
# ax[i].xaxis.set_major_locator(xmajorLocator)
# save_path = r'/home/gzblue/xgx/ljjjj/guokong'
# sname = save_path + '/' + r'guokong' + str( time_08_01[0])[:10].replace('-','_')+'_'+str(time_08_01[168])[0:10].replace('-','_')+ r'_o3浓度.png'
# plt.tight_layout()
# plt.savefig(sname, format='png', dpi=300, bbox_inches='tight', pad_inches=1)
# # print(sname)
# plt.close()

## 市控站作图

s = 1
while s < 33:
s1 = s
s2 = s
s2 = s2 + 7
s = s2 + 1
use_shikong = shikong_08_01.iloc[:, s1:s2 + 1]
# # print(use_shikong)
use_shikong_list = list(use_shikong.columns)
if '龙泉驿区区政府' in use_shikong_list:
use_shikong_list.remove('龙泉驿区区政府')
use_shikong_list.insert(0, '龙泉驿区区政府')

print(use_shikong_list)
fig, ax = plt.subplots(8, 1, figsize=(20, 22))
i = -1
width = 0.25
for station in use_shikong_list:
# print(station)
data = use_shikong[str(station)]
i += 1
ax[i].set_ylabel(r'o3(μg/m3)', color='b', fontproperties=zhfont1, size=18)
ax[i].tick_params('y', colors='b')
ax[i].set_xlim(-width, len(time_08_01) + width - 1)
ax[i].set_title(
r'市控站_' + str(station) + str(time_08_01[0]) + '-' + str(time_08_01[168]) + r'_o3浓度(μg/m3)',
fontproperties=zhfont1, size=22)
ax[i].plot(time_08_01, data, 'b-')
ax[i].set_xticks(range(len(time_08_01)))
ax[i].set_xticklabels(x_labels_index_values, fontproperties=zhfont1, size=18, rotation=45) # x轴的标签字体的旋转
ax[i].xaxis.set_minor_locator(xminorLocator)
ax[i].xaxis.set_major_locator(xmajorLocator)
save_path = r'/home/gzblue/xgx/ljjjj/shikong'
sname = save_path + '/' + r'shikong' + '_' + str(time_08_01[0])[:10].replace('-',
'_') + '_' + str(
time_08_01[168])[0:10].replace('-', '_') +'_'+ str(s2)+ r'_o3浓度.png'
plt.tight_layout()
plt.savefig(sname, format='png', dpi=100, bbox_inches='tight', pad_inches=0.1)
# print(sname)
plt.close()
# break
# break
原文地址:https://www.cnblogs.com/avivi/p/11354254.html