2

I want to plot a pandas series which index is incountinuous DatatimeIndex. My code is as follows:

import matplotlib.dates as mdates
index = pd.DatetimeIndex(['2000-01-01 00:00:00', '2000-01-01 00:01:00',
           '2000-01-01 00:02:00', '2000-01-01 00:03:00',
           '2000-01-01 00:07:00',
           '2000-01-01 00:08:00'],
          dtype='datetime64[ns]')
df = pd.Series(range(6), index=index)
print(df)
plt.plot(df.index, df.values)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%M"))
plt.show()

The output is:enter image description here But the result is not what I really want, because 2000-01-01 00:04:00 is also plotted on the image. The desired outcome is that on the x-axis 03:00 is next to 07:00 and the image should be a straight line. Hope for your great idea.

J. Zheng
  • 325
  • 2
  • 12

1 Answers1

2

One possible solution is convert index to string by strftime and use Series.plot:

s = pd.Series(range(6), index=index)
print(s)
2000-01-01 00:00:00    0
2000-01-01 00:01:00    1
2000-01-01 00:02:00    2
2000-01-01 00:03:00    3
2000-01-01 00:07:00    4
2000-01-01 00:08:00    5
dtype: int32

s.index = s.index.strftime('%M')
s.plot()

Another solution is plot by arange and then add xticks:

x = np.arange(len(s.index))
plt.plot(x, s)
plt.xticks(x, s.index.strftime('%M'))
plt.show()

graph

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • In my project, I use `LineCollection` to draw a multi-color line. You can take a look at my former question. https://stackoverflow.com/questions/44642966/how-to-plot-multi-color-line-if-x-axis-is-date-time-index-of-pandas `LineCollection` requires coordinates are able to be 'float'. Although this method is very good, but it can't be used in my project. Thank you anyway. – J. Zheng Jun 22 '17 at 05:48
  • @JZeng I think this answer is directly applicable to your problem. Have you tried to use it? What is the problem? – ImportanceOfBeingErnest Jun 22 '17 at 06:52
  • Sorry for my carelessness, this method really works for my project. But the xticks is too dense. I want to show 'year' not 'minute', but I found set locator and formatter do not help. do you have any good idea? Thanks a lot for your kindness. – J. Zheng Jun 22 '17 at 08:45
  • `plt.xticks(x, s.index.strftime('%Y'))` does not work? maybe need change it by http://strftime.org/. – jezrael Jun 22 '17 at 08:47
  • for example, there are too many minutes of 2012, if I use `plt.xticks(x, s.index.strftime(%Y))`, there are too many 2012 at x-axis, each minute a 2012, but I want only one 2012 for a year's data. – J. Zheng Jun 22 '17 at 08:55
  • Do you think something like [this](https://stackoverflow.com/a/38186557/2901002) ? – jezrael Jun 22 '17 at 08:58
  • Their answer may not help me. I want labels auto generated from index, because I do not know whether there are same data in 2011 as 2012 or not. – J. Zheng Jun 22 '17 at 09:17
  • It seems it is more complicated, is possible create new question? It seems need some matplotlib guru knowledge (not me)... Sorry. :( – jezrael Jun 22 '17 at 09:19