模块'plotly.validators.carpet'没有属性'HoverinfoValidator'

时间:2019-11-25 12:33:24

标签: python plot plotly

我目前正在使用plotly服务来绘制一些访客的流量数据。我的数据框结构

      date        score     actuals anomaly metric_name    shift    percentage_change   anomaly_class
967 2019-08-31  -0.079238   5888.0  2        7             4536.0   22.961957              2
966 2019-08-30  0.027939    4536.0  0        7             3597.0   20.701058              0
965 2019-08-29  0.072523    3597.0  0        7             4045.0   -12.454823             0
964 2019-08-28  0.046619    4045.0  0        7             3490.0   13.720643              0
963 2019-08-27  0.082069    3490.0  0        7             3280.0   6.017192               0

我正在使用以上数据集使用plotly软件包绘制图形。绘图时我面临一个问题。我的绘图版本是4.3.0。下面是我正在尝试的代码

from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import chart_studio.plotly as py
import matplotlib.pyplot as plt
from matplotlib import pyplot
import plotly.graph_objs as go
init_notebook_mode(connected=True)
def plot_anomaly(df,metric_name):
    #df.load_date = pd.to_datetime(df['date'].astype(str), format="%Y%m%d")
    dates = df.date
    #identify the anomaly points and create a array of its values for plot
    bool_array = (abs(df['anomaly']) > 0)
    actuals = df["actuals"][-len(bool_array):]
    anomaly_points = bool_array * actuals
    anomaly_points[anomaly_points == 0] = np.nan
    #A dictionary for conditional format table based on anomaly
    color_map = {0: "blue", 1: "yellow", 2: "red"}

    #Table which includes Date,Actuals,Change occured from previous point
    table = go.Table(
        domain=dict(x=[0, 1],
                    y=[0, 0.3]),
        columnwidth=[1, 2],
        # columnorder=[0, 1, 2,],
        header=dict(height=20,
                    values=[['<b>Date</b>'], ['<b>Actual Values </b>'], ['<b>% Change </b>'],
                            ],
                    font=dict(color=['rgb(45, 45, 45)'] * 5, size=14),
                    fill=dict(color='#d562be')),
        cells=dict(values=[df.round(3)[k].tolist() for k in ['date', 'actuals', 'percentage_change']],
                   line=dict(color='#506784'),
                   align=['center'] * 5,
                   font=dict(color=['rgb(40, 40, 40)'] * 5, size=12),
                   # format = [None] + [",.4f"] + [',.4f'],
                   # suffix=[None] * 4,
                   suffix=[None] + [''] + [''] + ['%'] + [''],
                   height=27,
                   fill=dict(color=[test_df['anomaly_class'].map(color_map)],#map based on anomaly level from dictionary
                   )
                   ))
    #Plot the actuals points
    Actuals = go.Scatter(name='Actuals',
                         x=dates,
                         y=df['actuals'],
                         xaxis='x1', yaxis='y1',
                         mode='markers',
                         marker=dict(size=12,
                                     line=dict(width=1),
                                     color="blue"))
#Highlight the anomaly points
    anomalies_map = go.Scatter(name="Anomaly",
                               showlegend=True,
                               x=dates,
                               y=anomaly_points,
                               mode='markers',
                               xaxis='x1',
                               yaxis='y1',
                               hoverinfo='none',
                               marker=dict(color="red",
                                           size=11,
                                           line=dict(
                                               color="red",
                                               width=2)))


    axis = dict(
            showline=True,
            zeroline=False,
            showgrid=True,
            mirror=True,
            ticklen=4,
            gridcolor='#ffffff',
            tickfont=dict(size=10))
    layout = dict(
            width=1000,
            height=865,
            autosize=False,
            title=metric_name,
            margin=dict(t=75),
            showlegend=True,
            xaxis1=dict(axis, **dict(domain=[0, 1], anchor='y1', showticklabels=True)),
            yaxis1=dict(axis, **dict(domain=[2 * 0.21 + 0.20, 1], anchor='x1',hoverformat='.2f')))
    layout = go.Layout(hovermode=False)
    fig = go.Figure(data=[table, anomalies_map, Actuals], layout=layout)
    iplot(fig)
    pyplot.show()

错误:

AttributeError: module 'plotly.validators.carpet' has no attribute 'HoverinfoValidator

有人可以告诉我如何解决此错误吗?

0 个答案:

没有答案