在x轴上仅显示每个<n>日期(或按月分组)以减少x轴混乱

时间:2019-04-09 04:05:24

标签: python matplotlib plot axis-labels

我目前正在通过使用Jupyter Notebook和matplotlib库从JSON数据生成一些图形来学习一些Python。我已经能够制作出很棒的图表,但是我不确定如何使我的x轴混乱。请参见下面的屏幕截图。每天都有一个值,传入的数据有数百天。这会造成一个非常混乱的x轴,无法读取。

代码:

dates = [i['daily_sales_date'] for i in json_data]
values = [i['daily_sales'] for i in json_data]
print('sample date: ' + dates[0])
print('sample value: ' + str(values[0]))
df = pd.DataFrame({'dates':dates, 'values':values})
df['dates']  = [pd.to_datetime(i) for i in df['dates']]
plt.bar(dates, values)

结果:

enter image description here

那条黑色的粗条是我所有的约会:)。我尝试查看一些示例,这些示例应该使x轴日期变得整洁,但我没有让它们起作用。我很乐意要么只显示沿轴的日期的子集,要么只显示月份名称。我能做的最好的事情就是使x轴标签完全不显示:/有什么建议吗?

enter image description here

我根据建议之一尝试了以下操作:

from matplotlib.dates import MonthLocator
register_matplotlib_converters()

然后创建图形:

enter image description here

如您所见,我现在有2019年(年份),但是没有月份?为了清晰起见(应要求),下面提供了示例数据

[
    {
        "RowInsertDateTime": "2019-04-10T13:10:00.6",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 4994.2,
        "daily_sales_date": "2019-04-10T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-04-09T23:00:01.213",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 8868.75,
        "daily_sales_date": "2019-04-09T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-04-08T23:00:02.093",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 4618.55,
        "daily_sales_date": "2019-04-08T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-04-07T23:00:01.52",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 5710.01,
        "daily_sales_date": "2019-04-07T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-04-06T23:00:01.42",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 9674.46,
        "daily_sales_date": "2019-04-06T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-04-05T23:50:01.977",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 9243.66,
        "daily_sales_date": "2019-04-05T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-04-04T23:50:01.5",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 8865.75,
        "daily_sales_date": "2019-04-04T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-04-03T23:00:01.003",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 5530.14,
        "daily_sales_date": "2019-04-03T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-04-02T23:00:01.71",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 4893.77,
        "daily_sales_date": "2019-04-02T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-04-01T23:00:01.61",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 3741.6,
        "daily_sales_date": "2019-04-01T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-31T23:00:00.893",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 8727.52,
        "daily_sales_date": "2019-03-31T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-30T23:00:01.263",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 9572.48,
        "daily_sales_date": "2019-03-30T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-29T23:50:01.937",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 20003.71,
        "daily_sales_date": "2019-03-29T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-28T23:50:00.933",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 29890.54,
        "daily_sales_date": "2019-03-28T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-27T23:00:01.267",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 19669.24,
        "daily_sales_date": "2019-03-27T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-26T23:00:13.68",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 18655.44,
        "daily_sales_date": "2019-03-26T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-25T23:00:12.427",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 4876.38,
        "daily_sales_date": "2019-03-25T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-24T23:00:16.313",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 8467.17,
        "daily_sales_date": "2019-03-24T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-23T23:00:23.517",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 12542.34,
        "daily_sales_date": "2019-03-23T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-22T23:50:14.363",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 12119.07,
        "daily_sales_date": "2019-03-22T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-21T23:50:12.527",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 9403.31,
        "daily_sales_date": "2019-03-21T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-20T23:00:15.797",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 5872.87,
        "daily_sales_date": "2019-03-20T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-19T23:10:09.547",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 4634.91,
        "daily_sales_date": "2019-03-19T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-18T23:00:10.887",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 5789.16,
        "daily_sales_date": "2019-03-18T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-17T23:00:07.93",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 9743.17,
        "daily_sales_date": "2019-03-17T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-16T23:00:12.367",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 10729.08,
        "daily_sales_date": "2019-03-16T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-15T23:50:09.177",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 9404.83,
        "daily_sales_date": "2019-03-15T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-14T23:50:11.423",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 9029.93,
        "daily_sales_date": "2019-03-14T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-13T23:00:17.653",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 4464.14,
        "daily_sales_date": "2019-03-13T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-12T23:00:14.063",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 4711.15,
        "daily_sales_date": "2019-03-12T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-11T23:00:11.227",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 7090.3,
        "daily_sales_date": "2019-03-11T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-10T23:00:07.127",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 8083.23,
        "daily_sales_date": "2019-03-10T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-09T23:10:10.253",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 10253.7,
        "daily_sales_date": "2019-03-09T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-08T23:50:09.863",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 12339.06,
        "daily_sales_date": "2019-03-08T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-07T23:50:10.497",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 10200.52,
        "daily_sales_date": "2019-03-07T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-06T23:10:10.87",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 6694.55,
        "daily_sales_date": "2019-03-06T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-05T23:10:08.707",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 5779.48,
        "daily_sales_date": "2019-03-05T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-04T23:00:09.39",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 4954.72,
        "daily_sales_date": "2019-03-04T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-03T23:00:10.75",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 8473.28,
        "daily_sales_date": "2019-03-03T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-02T23:00:09.637",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 11327.68,
        "daily_sales_date": "2019-03-02T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-01T23:50:11.49",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 11075.8,
        "daily_sales_date": "2019-03-01T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-02-28T23:50:10.217",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 9143.1,
        "daily_sales_date": "2019-02-28T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-02-27T23:00:09.44",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 5523.66,
        "daily_sales_date": "2019-02-27T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-02-26T23:00:08.913",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 5235.5,
        "daily_sales_date": "2019-02-26T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-02-25T23:00:19.74",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 5379.84,
        "daily_sales_date": "2019-02-25T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-02-24T23:00:09.44",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 7194.78,
        "daily_sales_date": "2019-02-24T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-02-23T23:00:11.783",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 9438.9,
        "daily_sales_date": "2019-02-23T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-02-22T23:50:07.167",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 9989.46,
        "daily_sales_date": "2019-02-22T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-02-21T23:50:06.98",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 10120.73,
        "daily_sales_date": "2019-02-21T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-02-20T23:00:14.46",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 5732.03,
        "daily_sales_date": "2019-02-20T00:00:00"
    }
]

3 个答案:

答案 0 :(得分:2)

您唯一的主要问题是“日期”是字符串。如果将字符串转换为日期,则绘图将按预期显示。您已经在数据框内部进行了此操作,但是不要在任何其他代码中使用该列。

df = pd.DataFrame({'dates':dates, 'values':values})
df['dates']  = pd.to_datetime(df['dates'])  # possibly format="..."
plt.bar(df['dates'].values, df['values'].values)

# The complete example would be:

import pandas as pd
import matplotlib.pyplot as plt

json_data = [
    {
        "RowInsertDateTime": "2019-04-10T13:10:00.6",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 4994.2,
        "daily_sales_date": "2019-04-10T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-04-09T23:00:01.213",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 8868.75,
        "daily_sales_date": "2019-04-09T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-04-08T23:00:02.093",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 4618.55,
        "daily_sales_date": "2019-04-08T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-04-07T23:00:01.52",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 5710.01,
        "daily_sales_date": "2019-04-07T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-04-06T23:00:01.42",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 9674.46,
        "daily_sales_date": "2019-04-06T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-04-05T23:50:01.977",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 9243.66,
        "daily_sales_date": "2019-04-05T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-04-04T23:50:01.5",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 8865.75,
        "daily_sales_date": "2019-04-04T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-04-03T23:00:01.003",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 5530.14,
        "daily_sales_date": "2019-04-03T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-04-02T23:00:01.71",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 4893.77,
        "daily_sales_date": "2019-04-02T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-04-01T23:00:01.61",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 3741.6,
        "daily_sales_date": "2019-04-01T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-31T23:00:00.893",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 8727.52,
        "daily_sales_date": "2019-03-31T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-30T23:00:01.263",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 9572.48,
        "daily_sales_date": "2019-03-30T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-29T23:50:01.937",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 20003.71,
        "daily_sales_date": "2019-03-29T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-28T23:50:00.933",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 29890.54,
        "daily_sales_date": "2019-03-28T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-27T23:00:01.267",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 19669.24,
        "daily_sales_date": "2019-03-27T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-26T23:00:13.68",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 18655.44,
        "daily_sales_date": "2019-03-26T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-25T23:00:12.427",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 4876.38,
        "daily_sales_date": "2019-03-25T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-24T23:00:16.313",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 8467.17,
        "daily_sales_date": "2019-03-24T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-23T23:00:23.517",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 12542.34,
        "daily_sales_date": "2019-03-23T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-22T23:50:14.363",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 12119.07,
        "daily_sales_date": "2019-03-22T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-21T23:50:12.527",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 9403.31,
        "daily_sales_date": "2019-03-21T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-20T23:00:15.797",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 5872.87,
        "daily_sales_date": "2019-03-20T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-19T23:10:09.547",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 4634.91,
        "daily_sales_date": "2019-03-19T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-18T23:00:10.887",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 5789.16,
        "daily_sales_date": "2019-03-18T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-17T23:00:07.93",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 9743.17,
        "daily_sales_date": "2019-03-17T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-16T23:00:12.367",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 10729.08,
        "daily_sales_date": "2019-03-16T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-15T23:50:09.177",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 9404.83,
        "daily_sales_date": "2019-03-15T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-14T23:50:11.423",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 9029.93,
        "daily_sales_date": "2019-03-14T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-13T23:00:17.653",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 4464.14,
        "daily_sales_date": "2019-03-13T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-12T23:00:14.063",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 4711.15,
        "daily_sales_date": "2019-03-12T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-11T23:00:11.227",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 7090.3,
        "daily_sales_date": "2019-03-11T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-10T23:00:07.127",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 8083.23,
        "daily_sales_date": "2019-03-10T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-09T23:10:10.253",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 10253.7,
        "daily_sales_date": "2019-03-09T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-08T23:50:09.863",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 12339.06,
        "daily_sales_date": "2019-03-08T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-07T23:50:10.497",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 10200.52,
        "daily_sales_date": "2019-03-07T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-06T23:10:10.87",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 6694.55,
        "daily_sales_date": "2019-03-06T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-05T23:10:08.707",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 5779.48,
        "daily_sales_date": "2019-03-05T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-04T23:00:09.39",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 4954.72,
        "daily_sales_date": "2019-03-04T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-03T23:00:10.75",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 8473.28,
        "daily_sales_date": "2019-03-03T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-02T23:00:09.637",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 11327.68,
        "daily_sales_date": "2019-03-02T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-03-01T23:50:11.49",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 11075.8,
        "daily_sales_date": "2019-03-01T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-02-28T23:50:10.217",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 9143.1,
        "daily_sales_date": "2019-02-28T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-02-27T23:00:09.44",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 5523.66,
        "daily_sales_date": "2019-02-27T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-02-26T23:00:08.913",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 5235.5,
        "daily_sales_date": "2019-02-26T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-02-25T23:00:19.74",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 5379.84,
        "daily_sales_date": "2019-02-25T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-02-24T23:00:09.44",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 7194.78,
        "daily_sales_date": "2019-02-24T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-02-23T23:00:11.783",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 9438.9,
        "daily_sales_date": "2019-02-23T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-02-22T23:50:07.167",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 9989.46,
        "daily_sales_date": "2019-02-22T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-02-21T23:50:06.98",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 10120.73,
        "daily_sales_date": "2019-02-21T00:00:00"
    },
    {
        "RowInsertDateTime": "2019-02-20T23:00:14.46",
        "ServerName": "P781S001",
        "StoreName": "PRICELINE WERRIBEE",
        "daily_sales": 5732.03,
        "daily_sales_date": "2019-02-20T00:00:00"
    }
]

dates = [i['daily_sales_date'] for i in json_data]
values = [i['daily_sales'] for i in json_data]
print('sample date: ' + dates[0])
print('sample value: ' + str(values[0]))

df = pd.DataFrame({'dates':dates, 'values':values})
df['dates']  = pd.to_datetime(df['dates'])  # possibly format="..."
plt.bar(df['dates'].values, df['values'].values)

plt.show()

答案 1 :(得分:1)

使用刻度线定位器

from matplotlib.dates import MonthLocator

ax.xaxis.set_major_locator(MonthLocator())

这将为每个月创建一个刻度。有关更多自定义选项,请参见docs

答案 2 :(得分:0)

您只能显示第n个日期以减少混乱:

ticks = ax.get_xticks()
labels = ax.get_xticklabels()
n = len(ticks) // 10  # Show 10 ticks.
ax.set_xticks(ticks[::n])
ax.set_xticklabels(labels[::n])