1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > python-matplotlib-柱状堆积图

python-matplotlib-柱状堆积图

时间:2022-12-15 22:19:25

相关推荐

python-matplotlib-柱状堆积图

matplot的画图,虽然好像很简单,但是又很搞人

bar官方文档

import pandas as pdimport matplotlib.pyplot as pltimport numpy as npbar_data = pd.read_csv('data.csv', header=None)plt.rcParams['font.family'] = "Times New Roman"fig, ax = plt.subplots(figsize=(5, 3), dpi=200)# 这里的数据是1行24列x= np.linspace(0, 23, 24)bar1= bar_data.iloc[0, 0:].valuesbar2= bar_data.iloc[1, 0:].valuesbar3= bar_data.iloc[2, 0:].valueswidth = .4ax.bar(x, bar1, width, align='center', label='bar1',color='white',hatch="//",ec='k',lw=.6)ax.bar(x, bar2, width, bottom=bar1, label='bar2',color='gray',ec='k',lw=.6)ax.bar(x, bar3, width, bottom=bar1+ bar2, label='bar3',color='white',hatch="...",ec='k',lw=.6)ax.set_ylim(0, 3)ax.tick_params(direction='out', labelsize=12, length=5.5, width=1, top=False, right=False)ax.legend(fontsize=11, frameon=False, loc='upper center', ncol=4)ax.set_ylabel('Electricity Cost', fontsize=13)ax.set_xlabel('Time Slot', fontsize=13)text_font = {'size': '17', 'weight': 'bold', 'color': 'black'}ax.text(.03, .93, "(a)", transform=ax.transAxes, fontdict=text_font, zorder=4)ax.text(.87, -.08, '\nVisualization by DataCharm', transform=ax.transAxes,ha='center', va='center', fontsize=5, color='black', fontweight='bold', family='Roboto Mono')plt.show()

1.窗口大小 figsize=(宽, 高), dpi=分辨率

尺寸单位:英寸(1 英寸=2.54 厘米),分辨率:每1英寸上的像素点数

plt.subplots(figsize=(5, 3), dpi=200)

2.绘制单个柱状图

ax.bar(x, bar1, width, align='center', label='bar1',color='white',hatch="//",ec='k',lw=.6)

x轴数据,

y轴数据,

宽度:取值在0~1之间,默认为0.8

排列:‘center’或者’edge’,居中或者左对齐

标签名

颜色

填充图案: {’/’, ‘’, ‘|’, ‘-’, ‘+’, ‘x’, ‘o’, ‘O’, ‘.’, ‘*’}

ec:设置填充图案的颜色

lw:设置填充图案的宽度

3.堆叠柱状图

ax.bar(x, bar2, width, bottom=bar1, label='bar2',color='gray',ec='k',lw=.6)ax.bar(x, bar3, width, bottom=bar1+ bar2, label='bar3',color='white',hatch="...",ec='k',lw=.6)

bottom底部:关键是第三组要堆叠在第一组和第二组的和之上

4.效果

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。