Plotly:如何绘制条形图和折线图以及条形图作为子图?

Joe*_*Joe 7 python pandas plotly

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

Line_Bar_Chart,

另一个是条形图,如下所示,

条形图

我想用这两个组合图表显示一个图表并显示相同的图表。我已经试过这plotly通过make_subplots但我不能够正确地达到的结果。下面是创建这两个图表的代码,

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**:
Run Code Online (Sandbox Code Playgroud)

条形图代码

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')
Run Code Online (Sandbox Code Playgroud)

关于如何制作这两个图的子图的任何帮助都会有所帮助, 在此处输入图片说明

ves*_*and 7

这里的关键是将您的痕迹分配给通过rowcol中的子图fig.add_trace()。而且您不必使用from plotly.offline import iplot最新的情节更新。

阴谋:

在此输入图像描述

代码:

# 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()
Run Code Online (Sandbox Code Playgroud)