-1

Here is my code:

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()

What I need is a dataframe:

enter image description here

with this code, I got this graph:

enter image description here

As you can see the graph above, x axis looks awful.

I just want to make x axis by time index, not 2017-09-08 06:00, but just like 06.

How can I solve this problem?

Mr. T
  • 11,960
  • 10
  • 32
  • 54
김영서
  • 29
  • 3
  • 1
    You need to update this data frame to take only hour part instead of complete datetime. – Ankit Jaiswal Jul 09 '18 at 06:50
  • Is your index a `datetime64` type or an `object` type? – Scratch'N'Purr Jul 09 '18 at 06:57
  • 1
    There were plenty of questions asked here about this problem before. https://stackoverflow.com/questions/26700598/matplotlib-showing-x-tick-labels-overlapping-despite-best-efforts https://stackoverflow.com/questions/13515471/matplotlib-how-to-prevent-x-axis-labels-from-overlapping-each-other https://stackoverflow.com/questions/49231052/datetime-x-axis-matplotlib-labels-causing-uncontrolled-overlap – Georgy Jul 09 '18 at 07:16
  • 1
    More possible duplicates: https://stackoverflow.com/questions/14946371/editing-the-date-formatting-of-x-axis-tick-labels-in-matplotlib https://stackoverflow.com/questions/44213781/pandas-dataframe-line-plot-display-date-on-xaxis – Mr. T Jul 09 '18 at 07:45

1 Answers1

0

One way to approach it is to create a new column that has the data index reformatted to suit your needs.

Another way is to use matplotlib.xticks() to define custom x axis labels

#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()
Mr. T
  • 11,960
  • 10
  • 32
  • 54
Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35