【问题标题】:draw graph by matplotlib in python在 python 中通过 matplotlib 绘制图形
【发布时间】:2018-12-16 18:42:26
【问题描述】:

这是我的代码:

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(12,8))
plt.xlabel('Time')
plt.ylabel('Number of Spared Bikes')
plt.plot(needs.index, needs['110'], label='#101')
plt.show()

我需要的是一个数据框:

使用这段代码,我得到了这个图表:

如上图所示,x 轴看起来很糟糕。

我只想通过time 索引来制作x 轴,而不是2017-09-08 06:00,而是就像06

我该如何解决这个问题?

【问题讨论】:

标签: python pandas matplotlib visualization


【解决方案1】:

一种方法是创建一个新列,重新格式化数据索引以满足您的需求。

另一种方法是使用matplotlib.xticks()定义自定义x轴标签

#apply custom processing on your label. rotarion='vertical' will give you even more space to place the labels
labels=needs.index.apply(lambda x: process_label(x)) 
#display the modified label
plt.xticks(needs.index, labels, rotation='vertical')
plt.plot(needs.index, data)
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 2018-05-18
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    相关资源
    最近更新 更多