python绘制柱状图

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()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值