pandas - 带 3 y 轴的分组条形图
问题描述
下午好!我正在创建一个带有三个 y 轴的分组条形图。
我已经在 plotly 中尝试过这个示例(Plotly: Grouped Bar Chart with multiple axes),但是当我尝试创建第三个轴时,我无法将它移到绘图区域之外,因为
position
不允许输入数字0-1 范围外。我也尝试过使用 matplotlib,按照这个示例(https://matplotlib.org/3.1.1/gallery/ticks_and_spines/multiple_yaxis_with_spines.html,但是当我将绘图模式更改为
bar
并使用此代码时:
#Gerar bar graph por Região Hidrográfica e município, com info de dens demograf
#% de rede coletora, e de dif rel despesa e receita
figRH={}
def make_patch_spines_invisible(ax):
ax.set_frame_on(True)
ax.patch.set_visible(False)
for sp in ax.spines.values():
sp.set_visible(False)
for key, df in new_dict.items():
# ax1.set_ylabel('Diferença relativa entre receita e despesa (%)', color=color)
# plt.title(key+" - Dif. rel. rec. e despes. (%); Dens. Demogr.; % de rede coletora")
# plt.xticks(rotation=90)
# plt.ylim(-100,100)
# plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), fancybox=True, ncol=len(dfRH["Município"].unique())+2)
# fig.set_size_inches(25, 12, forward=True)
fig, host = plt.subplots()
fig.subplots_adjust(right=0.9)
par1 = host.twinx()
par2 = host.twinx()
# Offset the right spine of par2. The ticks and label have already been
# placed on the right by twinx above.
par2.spines["right"].set_position(("axes", 1.2))
# Having been created by twinx, par2 has its frame off, so the line of its
# detached spine is invisible. First, activate the frame but make the patch
# and spines invisible.
make_patch_spines_invisible(par2)
# Second, show the right spine.
par2.spines["right"].set_visible(True)
p1, = host.bar("Município", "Diferença relativa entre receita e despesa (%)_x",data=df)
p2, = par1.bar("Município", "IN032_AE - Margem da despesa com pessoal total (equivalente) (%)", data=df)
p3, = par2.bar("Município", "Dens. Demogr (hab/km²)", data=df, label="Dens. Demogr (hab/km²)")
# host.set_xlim(0, 2)
# host.set_ylim(0, 2)
# par1.set_ylim(0, 4)
# par2.set_ylim(1, 65)
host.set_xlabel("Município")
host.set_ylabel("Diferença relativa entre receita e despesa (%)_x")
par1.set_ylabel("Índice de Atendimento com Coleta e sem Tratamento")
par2.set_ylabel("Dens. Demogr (hab/km²)")
host.yaxis.label.set_color(p1.get_color())
par1.yaxis.label.set_color(p2.get_color())
par2.yaxis.label.set_color(p3.get_color())
tkw = dict(size=4, width=1.5)
host.tick_params(axis='y', colors=p1.get_color(), **tkw)
par1.tick_params(axis='y', colors=p2.get_color(), **tkw)
par2.tick_params(axis='y', colors=p3.get_color(), **tkw)
host.tick_params(axis='x', **tkw)
lines = [p1, p2, p3]
host.legend(lines, [l.get_label() for l in lines])
plt.show()
# fig.savefig(('/content/drive/My Drive/Colab Files/Graficos/difRelRecDespes' + '-' + key + '.png'), dpi=300)
# plt.clf()
我收到此错误:
<ipython-input-12-a468857237bc> in <module>()
34 par2.spines["right"].set_visible(True)
35
---> 36 p1, = host.bar("Município", "Diferença relativa entre receita e despesa (%)_x",data=df)
37 p2, = par1.bar("Município", "IN032_AE - Margem da despesa com pessoal total (equivalente) (%)", data=df)
38 p3, = par2.bar("Município", "Dens. Demogr (hab/km²)", data=df, label="Dens. Demogr (hab/km²)")
ValueError: too many values to unpack (expected 1)
而且我不明白为什么...
- 如果有一种方法可以
seaborn
轻松编码,我不需要专门使用plotly
或matplotlib
感谢您对帮助新人学习 python 的关注!
解决方案
推荐阅读
- ruby - 正则表达式匹配中英文标签
- python - 如何从电报机器人发送文件?
- amazon-web-services - 如何让第二个 NAT 备用,以防 AWS 上的同一公共子网上的主服务器发生故障
- reactjs - Gutenberg/React 将动态属性传递给过滤函数
- vba - 使用文件名中的 Outlook 子文件夹名称将邮件保存到硬盘驱动器
- firebase - 使用特定 UID 锁定写访问
- mysql - 转换用户输入 tp MYSQL TIME 文字
- c# - 存储过程在范围标识上返回 0
- python - 如何在本地 Python 项目的 Yocto 配方中安装 requirements.txt 的依赖项
- xslt - 如何在 xslt 或 xsl-fo 中按预期在 for 循环中进行排序(按字母顺序排列)?