Python实现可视化(六)--柱状图、图例放在图外

Python实现可视化(六)--柱状图

python实现柱状图可视化

已知:数据基础格式为dataframe
基础数据格式

# 加载基础包
import matplotlib.pyplot as plt
import pandas as pd

# x轴刻度
x = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18',
     '19', '20', '21', '22', '23']
height1 = list(df_final[df_final['index'] == '0']['占比'])
plt.bar(x, +np.array(height1), color='black', width=0.5)
height2 = list(df_final[df_final['index'] == '0.2']['占比'])
plt.bar(x, +np.array(height2), bottom=np.array(height1), color='grey', width=0.5)	#bottom决定纵向位置
height3 = list(df_final[df_final['index'] == '0.4']['占比'])
plt.bar(x, +np.array(height3), bottom=np.array(height1) + np.array(height2), color='darkturquoise', width=0.5)
height4 = list(df_final[df_final['index'] == '0.6']['占比'])
plt.bar(x, +np.array(height4), bottom=np.array(height1) + np.array(height2) + np.array(height3), color='blue', width=0.5)
height5 = list(df_final[df_final['index'] == '0.8']['占比'])
plt.bar(x, +np.array(height5), bottom=np.array(height1) + np.array(height2) + np.array(height3) + np.array(height4),
        color='purple', width=0.5)
height6 = list(df_final[df_final['index'] == '1']['占比'])
plt.bar(x, +np.array(height6), bottom=np.array(height1) + np.array(height2) + np.array(height3) + np.array(height4)
                                      + np.array(height5), color='gold', width=0.5)
height7 = list(df_final[df_final['index'] == '1.5']['占比'])
plt.bar(x, +np.array(height7),  bottom=np.array(height1) + np.array(height2) + np.array(height3) + np.array(height4)
                                      + np.array(height5) + np.array(height6), color='darkorange', width=0.5)
height8 = list(df_final[df_final['index'] == 'plus']['占比'])
plt.bar(x, +np.array(height8), bottom=np.array(height1) + np.array(height2) + np.array(height3) + np.array(height4)
                                      + np.array(height5) + np.array(height6) + + np.array(height7), color='red', width=0.5)

plt.legend(['0', '1', '2', '3', '4', '5', '6', '7'], 
			ncol=1, 	# 图例要排成多少列
			loc=2,		# 图例的中心点
			bbox_to_anchor=(1.01, 1.0),		# 偏移的百分比
			borderaxespad=0.,
			fontsize=16,
			title="图例标题")  # 图例标题
plt.xlabel('时间', fontsize=20)
plt.ylabel('占比', fontsize=20)
plt.title('累积条形图', fontsize=20)
plt.tick_params(labelsize=20)   # 设置坐标轴刻度
plt.tight_layout()  # 调整画布窗口
plt.show()

最终输出的结构图如下
结果图

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值