import pandas as pd
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore")
plt.rcParams['font.sans-serif']='SimHei'
plt.rcParams['axes.unicode_minus']=False
font = {
'family': 'STSong',
'serif': 'Times New Roman',
'weight': '660',
'size': 15}
plt.rc('font', **font)
plt.figure(figsize=(10,6),dpi=300)
x_len = np.arange(len(Q1_IAQI['污染物']))
#设置了6个X轴标签,所以这里要定位6个柱子的开始位置
total_width, n = 0.92, 6
width = total_width/n
# xticks 就是六个柱子的开始位置
xticks = x_len - (total_width - width) / 2
ax = plt.axes()
plt.grid(axis="y", c='#d2c9eb', linestyle = '--',zorder=0)#添加网格线,axis="y",只添加横线
plt.bar(xticks, Q1_IAQI['2020.8.25'], width=0.9*width, label="2020-8-25", color="darkorange",edgecolor='black',linewidth = 2, zorder=10)#edgecolor='black',边框用黑色
plt.bar(xticks+width, Q1_IAQI['2020.8.26'], width=0.9*width, label="2020-8-26", color="forestgreen",edgecolor='black',linewidth = 2, zorder=10)
plt.bar(xticks+2*width, Q1_IAQI['2020.8.27'], width=0.9*width, label="2020-8-27", color="royalblue",edgecolor='black',linewidth = 2, zorder=10)
plt.bar(xticks+3*width, Q1_IAQI['2020.8.28'], width=0.9*width, label="2020-8-27", color="m",edgecolor='black',linewidth = 2, zorder=10)
####添加数字文本
# plt.text(xticks[0], Q1_IAQI['2020.8.25'][0] + 0.3, Q1_IAQI['2020.8.25'][0], ha='center',fontproperties='Times New Roman', fontsize=12, zorder=10)
# plt.text(xticks[1], Q1_IAQI['2020.8.25'][1] + 0.3, Q1_IAQI['2020.8.25'][1], ha='center', fontproperties='Times New Roman', fontsize=12, zorder=10)
# plt.text(xticks[2], Q1_IAQI['2020.8.25'][2] + 0.3, Q1_IAQI['2020.8.25'][2], ha='center', fontproperties='Times New Roman', fontsize=12, zorder=10)
# plt.text(xticks[0] + width, Q1_IAQI['2020.8.26'][0] + 0.3, Q1_IAQI['2020.8.26'][0], ha='center',fontproperties='Times New Roman', fontsize=10, zorder=10)
# plt.text(xticks[1] + width, Q1_IAQI['2020.8.26'][1] + 0.3, Q1_IAQI['2020.8.26'][1], ha='center',fontproperties='Times New Roman', fontsize=10, zorder=10)
# plt.text(xticks[2] + width, Q1_IAQI['2020.8.26'][2] + 0.3, Q1_IAQI['2020.8.26'][2], ha='center', fontproperties='Times New Roman',fontsize=10, zorder=10)
plt.xticks(x_len, Q1_IAQI['污染物'], fontproperties='Times New Roman',fontsize = 12)#改变横坐标
plt.legend(prop={'family' : 'Times New Roman', 'size': 12}, ncol = 2)#图例设置
plt.xlabel("污染物")
plt.ylabel("IAQI",fontproperties='Times New Roman')
plt.show()
python绘制柱状图
于 2023-09-06 09:00:25 首次发布