0

我正在尝试用 plotly 绘制 Scatter。我可以使用这个命令完美地做到这一点:

iplot(df.unstack('column').iplot(asFigure=True, kind='line', title='Title', dimensions=(900,500), xTitle='Month', yTitle='Number')) 

但是当我使用以下命令时:

fig = go.Figure(data = data, layout = layout)
pyo.offline.iplot(fig, filename='style-scatter')

输出图表很奇怪!

我正在使用允许更灵活地更改布局的代码进行绘图。我需要定制图表。

import plotly.graph_objs as go
import plotly.offline as pyo


from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot # Import Plotly Modules
init_notebook_mode(connected=True) # connect the JavaScript to our notebook.
import cufflinks as cf # Allow using Plotly Offline
cf.go_offline() # Allow using Plotly Offline

pyo.init_notebook_mode(connected=True)

import pandas as pd
import numpy as np

import datetime

df =pd.read_csv("https://www.dropbox.com/s/90y07129zn351z9/test_data.csv?dl=1",encoding = "ISO-8859-1")

df.head()
date    site    received    sent    enroll
0   7/10/2018   2   NaN NaN 1.0
1   7/10/2018   2   NaN NaN 1.0
2   7/11/2018   2   NaN NaN NaN
3   7/11/2018   2   NaN NaN NaN
4   7/11/2018   2   NaN NaN NaN

df["date"]=pd.to_datetime(df['date'])

summary=df.pivot_table(index=['site','date'], aggfunc = {'enroll': 'count'}) # Pivot_Table with enrollment by SITE by MONTH
summary

        enroll
site    date    
2   2018-06-29  6
2018-07-02  15
2018-07-03  10
2018-07-04  2
2018-07-05  4
2018-07-06  13
2018-07-09  7
2018-07-10  23
2018-07-11  8
2018-07-12  15
2018-07-13  14
2018-07-16  14
2018-07-17  10
2018-07-18  8
2018-07-19  12
2018-07-23  13
2018-07-24  13
2018-07-25  48
2018-07-26  38
2018-07-27  11
3   2018-06-19  6
2018-06-20  9
2018-06-21  9
2018-06-22  13
2018-06-26  18
2018-06-27  19
2018-06-28  16
2018-06-29  20
2018-07-02  22
2018-07-03  17
... ... ...
4   2018-07-16  4
2018-07-17  12
2018-07-18  19
2018-07-19  17
2018-07-20  21
2018-07-23  4
2018-07-24  5
2018-07-25  10
2018-07-26  17
2018-07-27  9
5   2018-06-19  13
2018-06-20  16
2018-06-21  16
2018-06-22  16
2018-06-26  16
2018-06-27  7
2018-06-28  9
2018-07-02  9
2018-07-03  12
2018-07-10  20
2018-07-11  20
2018-07-12  19
2018-07-13  13
2018-07-16  9
2018-07-18  24
2018-07-19  23
2018-07-20  17
2018-07-23  25
2018-07-24  28
2018-07-25  17
86 rows × 1 columns

trace1 = go.Scatter(
    x=summary.index,
    y=summary.index,
    name='Summary'
)

data = [trace1]

layout = go.Layout(barmode = "relative", title="Summary of My Data",
                   xaxis= dict(title= 'Month',ticklen= 5,zeroline= False), 
                   yaxis= dict(title= 'Count',ticklen= 5,zeroline= False))

fig = go.Figure(data = data, layout = layout)
pyo.offline.iplot(fig, filename='style-scatter')

输出很奇怪!

但是如果我运行这个命令:

iplot(summary.unstack('site').iplot(asFigure=True, kind='line', title='Summary of My Data', dimensions=(900,500), xTitle='Month', yTitle='Number'))

我得到漂亮的想要的图表。

我想在下面运行这些代码,因为它们为我的图表布局提供了更好的选择:

fig = go.Figure(data = data, layout = layout)
pyo.offline.iplot(fig, filename='style-scatter')
4

1 回答 1

0

我自己修好了:-)。如果您遇到同样的问题,请按照以下说明操作。
注意:如果属性同时在 go.Layout() 和 iplot() 中,请注意 go.Layout() 属性会覆盖 iplot() 中的每个属性。

    layout = go.Layout(
    title='Your Title', 
    #width="", height='',
    #legend=dict(x=0, y=3, orientation="h"),
    #margin={'l': 20, 'b': 40, 't': 10, 'r': 65},
    font=dict(family='Calibri Light', size=12, color='rgb(162,82,46)'), 
    plot_bgcolor=
        'rgb(162,82,46)',
        #'rgb(149,141,85)',
        #'rgb(164,148,131)',

    xaxis=dict(title='X name', tickangle=45, ticklen=5,
    #zeroline=False,
    gridwidth=2,
    showticklabels=True,
    nticks=6),

    yaxis=dict(title='Y name', ticklen=5, gridwidth=4),
    )
    iplot(summary.unstack('site').iplot(
        asFigure=True, 
        kind='line', 
        mode='lines+markers', 
        layout=layout
                                             ))
于 2019-08-18T02:52:51.723 回答