Plotly:如何绘制条形图折线图与条形图结合作为子图? [英] Plotly: How to plot a bar & line chart combined with a bar chart as subplots?

查看:132
本文介绍了Plotly:如何绘制条形图折线图与条形图结合作为子图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过 plotly 在 python 中绘制两个不同的图表.我有两个图,一个图由合并图(折线图和条形图)组成,如下所示,

I am trying to plot two different charts in python through plotly. I have two plots, one plot consists of merged graph ( line and bar chart) like the following,

,

另一个是柱状图如下,

我想用这两个组合图表显示一个图表并显示相同的图表.我已经在 plotly 中通过 ma​​ke_subplots 尝试了这个,但我无法正确实现结果.下面是创建这两个图表的代码,

I wanted to display one single chart with these two combined charts and display the same. I have tried this in plotly through make_subplots but I am not able to achieve the results properly. Below are the codes for creating these two charts,

Line_Bar_chart 代码:

import plotly.graph_objects as go
from plotly.offline import iplot

trace1  = go.Scatter(
        mode='lines+markers',
        x = df['Days'],
        y = df['Perc_Cases'],
        name="Percentage Cases",
        marker_color='crimson'
    )

trace2 = go.Bar(
        x = df['Days'],
        y = df['Count_Cases'],
        name="Absolute_cases",
        yaxis='y2',
        marker_color ='green',
        marker_line_width=1.5,
        marker_line_color='rgb(8,48,107)',
        opacity=0.5
    )

data = [trace1, trace2]

layout = go.Layout(
    title_text='States_Name',
    yaxis=dict(
        range = [0, 100],
        side = 'right'
    ),
    yaxis2=dict(
        overlaying='y',
        anchor='y3',
    )
)
fig = go.Figure(data=data, layout=layout)
iplot(fig, filename='multiple-axes-double')
**Line_Bar_chart Code**:

条形图代码:

trace2 = go.Bar(
        x = df['Days'],
        y = df['Perc_Cases'],
        yaxis='y2',
        marker_color ='green',
        marker_line_width=1.5,
        marker_line_color='rgb(8,48,107)',
        opacity=0.5,
    )
layout = go.Layout(
    title_text='States_Name',
    yaxis2=dict(
        overlaying='y',

    )
)
fig = go.Figure(data=trace2, layout=layout)
iplot(fig, filename='multiple-axes-double')

关于如何制作这两个图的子图的任何帮助都会有所帮助,

Any help on how to make subplots of these two graphs like below would be helpful,

推荐答案

这里的关键是通过中的rowcol将你的trace分配给subplotfig.add_trace().而且您不必使用 from plotly.offline import iplot 来获取最新的 plotly 更新.

The key here is to assign your traces to the subplot through row and col in fig.add_trace(). And you don't have to use from plotly.offline import iplot for the latest plotly updates.

剧情:

代码:

# imports
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import pandas as pd
import numpy as np

# data
df = pd.DataFrame({'Index': {0: 1.0,
                              1: 2.0,
                              2: 3.0,
                              3: 4.0,
                              4: 5.0,
                              5: 6.0,
                              6: 7.0,
                              7: 8.0,
                              8: 9.0,
                              9: 10.0},
                             'A': {0: 15.0,
                              1: 6.0,
                              2: 5.0,
                              3: 4.0,
                              4: 3.0,
                              5: 2.0,
                              6: 1.0,
                              7: 0.5,
                              8: 0.3,
                              9: 0.1},
                             'B': {0: 1.0,
                              1: 4.0,
                              2: 2.0,
                              3: 5.0,
                              4: 4.0,
                              5: 6.0,
                              6: 7.0,
                              7: 2.0,
                              8: 8.0,
                              9: 1.0},
                             'C': {0: 12.0,
                              1: 6.0,
                              2: 5.0,
                              3: 4.0,
                              4: 3.0,
                              5: 2.0,
                              6: 1.0,
                              7: 0.5,
                              8: 0.2,
                              9: 0.1}})
# set up plotly figure
fig = make_subplots(1,2)

# add first bar trace at row = 1, col = 1
fig.add_trace(go.Bar(x=df['Index'], y=df['A'],
                     name='A',
                     marker_color = 'green',
                     opacity=0.4,
                     marker_line_color='rgb(8,48,107)',
                     marker_line_width=2),
              row = 1, col = 1)

# add first scatter trace at row = 1, col = 1
fig.add_trace(go.Scatter(x=df['Index'], y=df['B'], line=dict(color='red'), name='B'),
              row = 1, col = 1)

# add first bar trace at row = 1, col = 2
fig.add_trace(go.Bar(x=df['Index'], y=df['C'],
                     name='C',
                     marker_color = 'green',
                     opacity=0.4,
                     marker_line_color='rgb(8,48,107)',
                    marker_line_width=2),
              row = 1, col = 2)

fig.show()

这篇关于Plotly:如何绘制条形图折线图与条形图结合作为子图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆