我想在plotly.express中合并两个堆叠条形图
第一幅图的代码是:
import pandas as pd
import plotly.express as px
from plotly.subplots import make_subplots
rscu1 = pd.read_csv("JQ038231.1_RSCU_stack.csv")
rscu2 = pd.read_csv("MG970255.1_RSCU_stack.csv")
rscu = pd.concat([rscu1, rscu2], keys=["JQ038231", "MG970255"])
rscu["species"] = rscu.index.get_level_values(0)
rscu = rscu.astype({"Fill": "category"})
rscu = rscu.astype({"aaRatio": "object"})
fig = px.bar(rscu, x="AA", y="RSCU", color="Fill", barmode = 'stack', text="aaRatio",
hover_data=['AA', "RSCU"], facet_row="species",
color_discrete_map={1: "#6598c9", 2: "#cb4a28", 3: "#9ac664", 4: "#7f5499"})
fig.update_xaxes(tickangle=0, title=None, ticks="outside")
fig.update_layout(
autosize=False,
width=950,
height=450,
showlegend=False)
for data in fig.data:
data["width"] = 0.9
fig.update_traces(textposition='outside')
fig.update_layout(uniformtext_minsize=8, uniformtext_mode='show')
第二幅图的代码是:
fig_bottom = px.bar(rscu1, x="AA", y="Equality", color="Fill", barmode = 'stack', text="AA",
hover_data=['AA'], height=220, width=950,
color_discrete_map={1: "#6598c9", 2: "#cb4a28", 3: "#9ac664", 4: "#7f5499"})
fig_bottom.update_traces(textposition='inside', textfont_size=14)
fig_bottom.update_layout(uniformtext_minsize=9, uniformtext_mode='show', showlegend=False,)
fig_bottom.update_layout({"plot_bgcolor": "rgba(0, 0, 0, 0)",
"paper_bgcolor": "rgba(0, 0, 0, 0)"})
fig_bottom.update_yaxes(title=None, showticklabels=False)
fig_bottom.update_xaxes(title=None, showticklabels=False)
for data in fig_bottom.data:
data["width"] = 0.9
是否有办法将它们合并为一个图形,最终图形(由ggplot制作)为:
这里使用的数据可以在https://github.com/dongzhang0725/sample_data中找到
更新
根据前面的回答,您可以
make_subplots()
、add_trace()
和最后update_layout()
来自GitHub的源
溶液
感谢Rob,根据他的回答,我用以下代码解决了我的问题:
相关问题 更多 >
编程相关推荐