如何在Python 3中显示所有日期在绘图图中的所有数据?

时间:2018-11-14 10:08:11

标签: python plot graph plotly linechart

我正在尝试使用plotly绘制折线图。我想在整个DATE绘制特定地区的销售情况。首先,我尝试在DATE列上绘制所有区域的SALES,绘制成功。但是,当我尝试为特定区域绘制SALES时,该图显示的是一半数据。该图显示了DATE列的值,但仅显示了1个月(如此处所示),即使特定区域具有3个月的数据,也显示了从“ 1月1日到2月1日”的值。我尝试使用其他区域,但仅在过滤到REGION级别时才显示1个月的图形,而为整个REGION绘制时则显示所有DATE值。请建议我以任何方式在图表中显示特定区域的所有3个月的数据。

import pandas as pd

Region=['East', 'East', 'East', 'East', 'East', 'East', 'East', 'East',
       'East', 'East', 'North', 'North', 'North', 'North', 'North',
       'North', 'North', 'North', 'North', 'North', 'North', 'North',
       'North', 'North', 'North', 'North', 'North', 'North', 'North',
       'North', 'North', 'North', 'North', 'North', 'North', 'South',
       'South', 'South', 'South', 'South', 'South', 'South', 'South',
       'South', 'South', 'South', 'South', 'South', 'South', 'South',
       'South', 'South', 'South', 'South', 'South', 'South', 'South',
       'South', 'South', 'South', 'West', 'West', 'West', 'West', 'West',
       'West', 'West', 'West', 'West', 'West', 'West', 'West', 'West',
       'West', 'West', 'West', 'West']

Date=['2018-01-01', '2018-01-05', '2018-01-22', '2018-02-01', '2018-02-02', '2018-02-19', '2018-02-23', '2018-03-09', '2018-03-18', '2018-03-23', '2018-01-02', '2018-01-04', '2018-01-05', '2018-01-06', '2018-01-09', '2018-01-13', '2018-01-22', '2018-01-31', '2018-02-02', '2018-02-03', '2018-02-05', '2018-02-06', '2018-02-09', '2018-02-20', '2018-02-22', '2018-02-23', '2018-02-24', '2018-03-08', '2018-03-10', '2018-03-12', '2018-03-13', '2018-03-17', '2018-03-19', '2018-03-24', '2018-03-26', '2018-01-01', '2018-01-03', '2018-01-04', '2018-01-06', '2018-01-08', '2018-01-10', '2018-01-12', '2018-01-22', '2018-02-04', '2018-02-05', '2018-02-06', '2018-02-07', '2018-02-08', '2018-02-09', '2018-02-22', '2018-02-23', '2018-02-24', '2018-02-25', '2018-02-26', '2018-03-11', '2018-03-12', '2018-03-14', '2018-03-16', '2018-03-23', '2018-03-25', '2018-01-02', '2018-01-03', '2018-01-07', '2018-01-11', '2018-01-22', '2018-02-03', '2018-02-04', '2018-02-07', '2018-02-08', '2018-02-21', '2018-02-24', '2018-02-25', '2018-03-10', '2018-03-11', '2018-03-15', '2018-03-24', '2018-03-25']

Sales=[ 20215,   6000,  22124,  12506,  20582,  16893,  17618, 407253,
       404091, 428236,  20122,  12600,  15460,  12000,  13800,  16505,
        57836,  12919,  31994,  18375,  15075,  18097,  16800,  17208,
        39300,  17119,  21662, 332309, 444733, 403845, 427565, 430442,
       429492, 433257, 450149,   7200,  19200,  19349,  12796,  25751,
        18130,  21314,  53821,  20311,  16110,  19344,  20969,  16440,
        19899,  17016,  18226,  15752, 334597,  32906, 430401, 427898,
       458760, 393953, 463615, 425819,  13800,  24579,  22825,  19597,
        34444,  14978,  21231,  19634,  21256,  16549, 125037,  36875,
       420822, 432303, 423303, 450958, 431653]

Profit=[ 210,  156,  188,  240,  210,  229,  206, 5168, 5139, 5149,  196,
        150,  253,  162,  330,  163,  662,  186,  416,  203,  194,  222,
        240,  213,  414,  188,  185, 4150, 5184, 5305, 5105, 5435, 5189,
       5287, 5080,  132,  144,  221,  229,  506,  230,  219,  682,  204,
        219,  221,  220,  217,  203,  230,  221,  220, 4276,  433, 5073,
       4987, 5095, 5189, 5251, 5132,  138,  240,  398,  222,  385,  243,
        207,  219,  184,  254, 1452,  440, 5310, 5187, 5060, 4992, 5212]

d=pd.DataFrame({'Region':Region, 'Date':pd.to_datetime(Date,yearfirst=True), 'Sales':Sales, 'Profit':Profit})

数据框可以看作是

enter image description here

  

图形-绘图代码

import plotly
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot,iplot
plotly.offline.init_notebook_mode()

# Create first trace
trace = go.Scatter(
    x = list(d.Date.sort_values()),
    y = list(d.Sales.sort_values()),
)

data = [trace]

plot(data, filename='basic-line')
  

下面给出的图形显示了整个区域的所有地区的销售额   3个月(1月,2月和3月)正常。

enter image description here

# Create second trace
trace = go.Scatter(
    x = list(d.Date.sort_values()),
    y = list(d[d.Region=='South']['Sales'].sort_values()),
)

data = [trace]

plot(data, filename='basic-line')
  

但是当它被过滤为一个南部地区时,它只显示一个1   即使该数据存在于SOUTH列中也是如此。

enter image description here

谁能告诉我如何在绘图中显示剩余月份的数据。

0 个答案:

没有答案