带3 y轴的分组条形图

时间:2020-10-19 17:00:38

标签: pandas matplotlib plotly seaborn bar-chart

下午好! 我打算创建一个具有三个y轴的分组条形图。

#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进行编码,那么我不需要专门使用plotlymatplotlib

感谢您对帮助新手学习python的关注!

0 个答案:

没有答案