如何在 plotly 中覆盖同一图中的两个图(在 plotly 中创建帕累托图)?

How to overlay two plots in same figure in plotly ( Create Pareto chart in plotly )?(如何在 plotly 中覆盖同一图中的两个图(在 plotly 中创建帕累托图)?)
本文介绍了如何在 plotly 中覆盖同一图中的两个图(在 plotly 中创建帕累托图)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在同一图中绘制条形图和散点图,但它只显示散点图.

I was trying to plot barplot and scatterplot in the same plot in plotly, but it shows only 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)

输出

  • 同时显示条形图和散点图
  • 左侧 y 轴上的条形图 y 刻度
  • 右侧 y 轴上的散点图 y 刻度
  • xticklabels 旋转 90 度

推荐答案

试试这个:

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)
fig.add_trace(trace2,secondary_y=True)
fig['layout'].update(height = 600, width = 800, title = title,xaxis=dict(
      tickangle=-90
    ))
iplot(fig)

给,

这篇关于如何在 plotly 中覆盖同一图中的两个图(在 plotly 中创建帕累托图)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)