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

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

我在剧情中遵循袖扣的例子。在多轴示例中,我收到以下错误。问题是什么 ??熊猫版本为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__
python plotly
2个回答
0
投票

is不是你想要的?我从这个例子中采用了你的数据代码:

# 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')

输出:plot with two yaxis


0
投票

这适合我。

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)
© www.soinside.com 2019 - 2024. All rights reserved.