简体   繁体   English

在条形图中添加第二个带百分比的 y 轴 plotly

[英]Add second y-axis with percentage in bar chart plotly

Given df:给定 df:

  names  values  pct
0     a      10  0.1
1     b      20  0.2
2     c      70  0.7

Return a bar chart with secondary y-axis as a percentage (col pct)返回一个条形图,其中第二个 y 轴为百分比 (col pct)

Current code:当前代码:


import pandas as pd
import numpy as np
import plotly.express as px

df = pd.DataFrame({'names':['a', 'b', 'c'], 'values':[10,20,70], 'pct':[0.1, 0.2, 0.7]})

# the figure 
fig = px.bar(df, x='names', y='values')
fig.show()

expected outcome:预期结果: 在此处输入图像描述

##EDIT## The solution provided works, is there a way to also add color to the bars, whilst not messing up the look (bar distance/allignment with xticks) ##EDIT## 提供的解决方案有效,有没有一种方法可以为条形图添加颜色,同时不会弄乱外观(条形图距离/与 xticks 对齐)

Here is my attempt modyfing the solution provided:这是我尝试修改提供的解决方案:

import pandas as pd
import numpy as np
import plotly.express as px
from plotly.subplots import make_subplots

df = pd.DataFrame({'names':['a', 'b', 'c'], 'values':[10,20,70], 'pct':[0.1, 0.2, 0.7]})

fig = make_subplots(specs=[[{"secondary_y": True}]])

fig1 = px.bar(df, x='names', y='values', color='names'  ) # added color 
fig2 = px.bar(df, x='names', y='pct', color='names')
 # plot singulalry all the bars provided. 
for i in range(len(fig1.data)): 
  fig.add_trace(fig1.data[i], secondary_y=False)
  fig.add_trace(a.data[i], secondary_y=True)

fig.update_layout(yaxis2=dict(tickvals=[0.1,0.2,0.7], tickformat='.1%', title_text='Secondary y-axis percentage'))
fig.update_layout(xaxis=dict(title_text='name'), yaxis=dict(title_text='values'))
fig.update_layout(bargap=0.0)
fig.show()

However it is producing double legend entry and missaligned xaxis labels OUT但是,它会产生双图例条目和未对齐的 x 轴标签 OUT 在此处输入图像描述

To change the color of the bar chart by category, it is necessary to correspond with the markers in the graph object. in express, each is a bar chart by color, understood as 6 categorical variables, and I assume you are drawing abc categorical variables on top of each other.条形图按类别改变颜色,需要对应图object中的marker。express中每一个都是一个颜色条形图,理解为6个分类变量,假设你画的是abc分类变量在彼此之上。 In the graph object, two graphs are drawn and the designated color is specified with a marker;在图object中,绘制了两个图,并用marker指定了指定的颜色; it is set in hexadecimal format, but may be specified as red, blue, or green.它以十六进制格式设置,但可以指定为红色、蓝色或绿色。

import pandas as pd
from plotly.subplots import make_subplots
import plotly.graph_objects as go

df = pd.DataFrame({'names':['a', 'b', 'c'], 'values':[10,20,70], 'pct':[0.1, 0.2, 0.7]})

colors = ['#636efa','#EF553B','#00cc96']

fig = make_subplots(specs=[[{"secondary_y": True}]])

fig.add_trace(go.Bar(x=df['names'], y=df['values'], marker_color=colors, showlegend=False), secondary_y=False)
fig.add_trace(go.Bar(x=df['names'], y=df['pct'], marker_color=colors, showlegend=False), secondary_y=True)

fig.update_layout(yaxis2=dict(tickvals=[0.1,0.2,0.7], tickformat='.1%', title_text='Secondary y-axis percentage'))
fig.update_layout(xaxis=dict(title_text='name'), yaxis=dict(title_text='values'))
fig.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM