如何在同一图中重叠两个图形?

问题描述 投票:1回答:1

我试图在同一图中绘制barplot和scatterplot,但仅显示散点图。

如何显示两个图?

数据

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter

import plotly
import plotly.offline as py
import plotly.graph_objs as go
import plotly.figure_factory as ff
import plotly.tools as tls
from plotly.subplots import make_subplots
from plotly.offline import plot, iplot, init_notebook_mode
init_notebook_mode(connected=False)

df = pd.DataFrame({
            'price': [ 4.0, 17.0, 7.0, 7.0, 2.0, 1.0, 1.0],
            'item': ['apple', 'banana', 'carrot', 'plum',
                    'orange', 'date', 'cherry']})

df = df.sort_values(num,ascending=False)
df['cumulative_sum'] = df[num].cumsum()
df['cumulative_perc'] = 100*df['cumulative_sum']/df[num].sum()

df['demarcation'] = 80


num = 'price'
cat = 'item'
title = 'Pareto Chart'

代码

trace1 = go.Bar(
    x=df[cat],
    y=df[num],
    name=num,
    marker=dict(
        color='rgb(34,163,192)'
               )
)
trace2 = go.Scatter(
    x=df[cat],
    y=df['cumulative_perc'],
    name='Cumulative Percentage',
    yaxis='y2',

)

data = [trace1,trace2]

fig = dict(data=data)
iplot(fig)

输出

enter image description here

必填

  • 同时显示条形图和散点图
  • 左y轴上的barchart y-ticks
  • 右侧y轴上的散点图y滴答
  • xticklabel旋转90度
python plotly
1个回答
0
投票

尝试makeplots:

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

trace1 = go.Bar(
    x=df[cat],
    y=df[num],
    name=num,
    marker=dict(
        color='rgb(34,163,192)'
               )
)
trace2 = go.Scatter(
    x=df[cat],
    y=df['cumulative_perc'],
    name='Cumulative Percentage',
    yaxis='y2'

)

fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(trace1, 1, 1)
fig.add_trace(trace2, 1, 1,secondary_y=True)
fig['layout'].update(height = 600, width = 800, title = title)
iplot(fig)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.