简体   繁体   English

从matplotlib时间序列图中删除日期

[英]Drop the date from a matplotlib time series plot

I have a simple time series plot in Pandas, that can be emulated by the following code below: 我在Pandas中有一个简单的时间序列图,可以通过以下代码模拟:

import pandas as pd
import numpy as np
from datetime import datetime, timedelta, date
import time
import matplotlib.pyplot as plt

df2 = pd.DataFrame({'A' : np.random.rand(1440).cumsum()}, index = pd.date_range('1/1/2015', periods=1440, freq='1min'))

df2.A.plot()

Which generates the following graph: 这会生成以下图表: 在此输入图像描述

My problem is that the date displayed is not relevant to the graph itself and I wish to remove it and leave only the time series on the x axis. 我的问题是显示的日期与图表本身无关,我希望将其删除,只留下x轴上的时间序列。

How do I do this? 我该怎么做呢?

you can use matplotlib.dates and its HourLocator to set the date/time formatting: 您可以使用matplotlib.dates及其HourLocator来设置日期/时间格式:

import pandas as pd
import numpy as np
from datetime import datetime, timedelta, date
import time
import matplotlib.pyplot as plt
import matplotlib.dates as dates

df2 = pd.DataFrame({'A' : np.random.rand(1440).cumsum()}, index = pd.date_range('1/1/2015', periods=1440, freq='1min'))

df2.A.plot()
plt.gca().xaxis.set_major_locator(dates.HourLocator())
plt.gca().xaxis.set_major_formatter(dates.DateFormatter('%H:%M'))

plt.show()

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM