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

时间:2018-09-20 05:46:39

标签: python plotly

我一直在遵循袖扣的例子。 在多轴示例中,出现以下错误。 问题是什么 ?? 大熊猫版本为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__

3 个答案:

答案 0 :(得分:2)

应用这段代码后我获得了成功

1

答案 1 :(得分:0)

is不是您要找的东西吗? 我已将本示例中的代码应用于您的数据:

| ASSET_ID | PRICE |                 DATE |    MA |
|----------|-------|----------------------|-------|
|       43 | 33.12 | 2018-09-17T00:00:00Z | 33.12 |
|       43 | 34.02 | 2018-09-18T00:00:00Z | 33.57 |
|       43 | 30.22 | 2018-09-19T00:00:00Z | 32.45 |
|       43 | 31.52 | 2018-09-20T00:00:00Z | 32.22 |
|       43 | 32.52 | 2018-09-21T00:00:00Z | 32.28 |
|       43 | 33.52 | 2018-09-22T00:00:00Z | 32.49 |
|       43 | 34.52 | 2018-09-23T00:00:00Z | 32.78 |
|       43 | 35.52 | 2018-09-24T00:00:00Z | 33.12 |
|       43 | 36.52 | 2018-09-25T00:00:00Z |  33.5 |
|       43 | 37.52 | 2018-09-26T00:00:00Z |  33.9 |
|       43 | 38.52 | 2018-09-27T00:00:00Z | 34.32 |
|       43 | 39.52 | 2018-09-28T00:00:00Z |  34.9 |
|       43 | 40.52 | 2018-09-29T00:00:00Z | 35.49 |
|       43 | 41.52 | 2018-09-30T00:00:00Z | 36.52 |

输出: plot with two yaxis

答案 2 :(得分: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)