简体   繁体   English

如何使用 plotly 和袖扣绘制多轴?

[英]How to plot multi axes using plotly and cufflinks?

I am following the example of cufflinks in plotly.我正在关注袖扣的例子。 In the multi axes example, I get the following error.在多轴示例中,我收到以下错误。 What is the problem ??问题是什么 ?? The pandas version is 0.23.0 and the cufflinks version is 0.14.4. pandas 版本是 0.23.0,袖扣版本是 0.14.4。

import pandas as pd
import numpy as np

from plotly.offline import init_notebook_mode, iplot
import plotly.plotly as py
import plotly.graph_objs as go
import cufflinks as cf
init_notebook_mode(connected=True)

df = cf.datagen.lines(4, mode='abc')
df[['c', 'd']] = df[['c', 'd']] * 100
df.iplot(secondary_y=['c','d'])

fig1 = df.iplot(columns=['a', 'b'], asFigure=True)
fig2 = df.iplot(columns=['c', 'd'], kind='bar', secondary_y=['c', 'd'], asFigure=True)
fig2['data'].extend(fig1['data'])
py.iplot(fig2, filename='pandas/secondary y with bar chart')

AttributeError                            Traceback (most recent call last)
<ipython-input-76-a8425145c10f> in <module>()
      1 fig1 = df.iplot(columns=['a', 'b'], asFigure=True)
----> 2 fig2 = df.iplot(columns=['c', 'd'], kind='bar', secondary_y=['c', 'd'], asFigure=True)
      3 fig2['data'].extend(fig1['data'])
      4 py.iplot(fig2)

C:\ProgramData\Anaconda3\lib\site-packages\cufflinks\plotlytools.py in _iplot(self, kind, data, layout, filename, sharing, title, xTitle, yTitle, zTitle, theme, colors, colorscale, fill, width, dash, mode, interpolation, symbol, size, barmode, sortbars, bargap, bargroupgap, bins, histnorm, histfunc, orientation, boxpoints, annotations, keys, bestfit, bestfit_colors, mean, mean_colors, categories, x, y, z, text, gridcolor, zerolinecolor, margin, labels, values, secondary_y, secondary_y_title, subplots, shape, error_x, error_y, error_type, locations, lon, lat, asFrame, asDates, asFigure, asImage, dimensions, asPlot, asUrl, online, **kwargs)
   1151 ## Check secondary axis
   1152         if secondary_y:
-> 1153                 figure=tools._set_axis(figure,secondary_y,side='right')
   1154                 if secondary_y_title:
   1155                         figure.layout.yaxis2.title=secondary_y_title

C:\ProgramData\Anaconda3\lib\site-packages\cufflinks\tools.py in _set_axis(self, traces, on, side, title)
   1166                 if k not in fig.axis['ref_axis']:
   1167                         try:
-> 1168                                 del fig['layout'][id]
   1169                         except KeyError:
   1170                                 pass

AttributeError: __delitem__

应用这段代码时我取得了成功

df[['a','b','c','d']].iplot(secondary_y=['c', 'd'])

It is not what you are looking for?不是你在找什么? I am adopted the code from this example to your data:我将本示例中的代码用于您的数据:

# import necessary libraries
from plotly.offline import init_notebook_mode, iplot
import plotly.graph_objs as go
import cufflinks as cf
init_notebook_mode(connected=True)
# get DataFrame
df = cf.datagen.lines(4, mode='abc')
# Create two traces
trace1 = go.Scatter(x=df['a'], y=df['b'])
trace2 = go.Scatter(x=df['c'], y=df['d'])
# Create data from traces
data = [trace1, trace2]
# Create layout
layout = go.Layout(title='Double Y Axis Example',
    # Define first yaxis
    yaxis=dict(
        title='yaxis title'
    ),
    # Define second yaxis
    yaxis2=dict(
        title='yaxis2 title',
        titlefont=dict(
            color='rgb(148, 103, 189)'
        ),
        tickfont=dict(
            color='rgb(148, 103, 189)'
        ),
        overlaying='y',
        side='right'
    )
)
# Create figure
fig = go.Figure(data=data, layout=layout)
# Plot the figure
iplot(fig, filename='pandas/secondary y with bar chart.html')

Output:输出: 用两个 yaxis 绘图

This works for me.这对我有用。

from cufflinks import tools
import plotly.graph_objs as go

df = cf.datagen.lines(4, mode='abc')
df[['c', 'd']] = df[['c', 'd']] * 100

fig = go.Figure(**tools.merge_figures([
    df.figure(columns=['a', 'b']),
    df.figure(columns=['c', 'd'], kind='bar')
])).set_axis(['c', 'd'], side='right')

cf.iplot(fig)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM