用python中的matplotlib绘制图形
这是我的密码:用python中的matplotlib绘制图形,python,pandas,matplotlib,visualization,Python,Pandas,Matplotlib,Visualization,这是我的密码: 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轴看起来很糟糕 我只想通过时间索引生成x
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轴看起来很糟糕
我只想通过时间
索引生成x轴,而不是2017-09-08 06:00
,而是像06
一样
我怎样才能解决这个问题 一种方法是创建一个新列,将数据索引重新格式化以满足您的需要 另一种方法是使用定义自定义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()
您需要更新此数据帧,使其仅占小时部分,而不是完整的日期时间。您的索引是
datetime64
类型还是对象
类型?关于此问题,以前在此处提出过很多问题。更多可能的副本: