3

I have dataframe 'df' with 2 columns Date/Time and Value:

import numpy as np
import pandas as pd
df = pd.DataFrame(pd.date_range('2012-06-01','2012-06-04', freq='H'), columns=['date/time'])
data = np.random.randint(5, 100, 73)
temp= pd.DataFrame(data, columns=['Value'])
df['Value']=temp
df.head()

When I draw a line plot with this data by:

lines=df.plot.line(x='date/time', y='Value')

I get this graph: enter image description here

I am happy with the graph but I want my x-axis to display only the month names. Also in my actual graph, I have a lot of dates with varying months, so when displaying it the x-axis labels should not overlap. Can anyone help me with this, please?

I already looked at stuff like .xaxis.set_major_locator but I can't get it to work.

1 Answers1

2

I had to change your code a little because it used a hourly range over 3 days, which results in all the months being June. Also, your temp line is unnecessary because you could equivalently create and directly assign the random integers to df['Value'] without first making a temp dataframe.

import numpy as np
import pandas as pd
df = pd.DataFrame(pd.date_range('2011-06-01','2012-06-04', freq='M'), columns=['date/time'])
df['Value'] = np.random.randint(5, 100, len(df.index))

fig = plt.figure()
ax = fig.add_subplot(111)

months_list = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
### Iterate over the TimeStamp objects, calling the month format of each
months = [months_list[dt.month - 1] for dt in df['date/time']]

ax.plot(months, df['Value'])
plt.show()

For the part about avoiding overlapping tick labels, you could do a basic rotation of the labels:

ax.tick_params(axis='x', labelrotation=30)

or refer to these answers that show two different ways to do this:

  1. Adds a label pad to alternate tick labels
  2. Fine-tune rotation and positions of tick labels

Here's the outcome: Basic rotation

Benedictanjw
  • 828
  • 1
  • 8
  • 19
  • The problem in this is that you made the date/time column to dates only when making the data frame. Also when I run the code it says " 'str' object has no attribute 'month' " for line months = [months_list[dt.month - 1] for dt in df['date/time']] – A pessimistic optimist Oct 15 '20 at 06:32
  • 1
    The data/time column is indeed made up of dates (actually TimeStamp), as it was in your code as well when you created it using pd.date_range. This is what allows us to call the .month method on it. Did you somehow format the values of the date/time column as string? Because that would cause the error you mentioned – Benedictanjw Oct 15 '20 at 06:51
  • Thank you it worked. All this time my date/time has been in string format. This is probably why I couldn't get any other solutions to work. – A pessimistic optimist Oct 15 '20 at 08:20