matplotlib difficult to show all x tick labels

Question:

I have a data series

hour
0     35845
1     18921
2     14484
3     12504
4     13862
5     14958
6     23328
7     33580
8     66878
9     65291
10    61785
11    64781
12    68799
13    72486
14    83230
15    75487
16    88231
17    85383
18    75525
19    61739
20    51696
21    43215
22    38539
23    30797
dtype: int64

I want to plot the data series and show all the x tick labels, which range from 0 to 23.
The code for basic plot is

import matplotlib as plt
import seaborn as sb

fig, ax = plt.subplots()
plt.title("Collision by hour")
sb.lineplot(x = df_hour.index,
            y = df_hour.values)

Which gives me a plot with only 5 x_tick labels:

Basic Plot

I had tried:

import matplotlib.pyplot as plt
import seaborn as sb
import import matplotlib.dates as mdate

fig, ax = plt.subplots()
plt.title("Collision by hour")

locator = mdate.AutoDateLocator(minticks=12, maxticks=24)
formatter = mdate.ConciseDateFormatter(locator)
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)

ax.plot(df_hour.index, df_hour.values)

Result with missing odd hour and extra start and end limit:

Plot with missing odd hour and extra limit

I had tried

import matplotlib.pyplot as plt
import seaborn as sb
import import matplotlib.dates as mdate

fig, ax = plt.subplots()
plt.title("Collision by hour")

hour_locator = mdate.HourLocator(interval = 1)
hour_formatter = mdate.DateFormatter("%H")

ax.xaxis.set_major_locator(hour_locator)
ax.xaxis.set_major_formatter(hour_formatter)

ax.plot(df_hour.index, df_hour.values)

Result with unreadable x_tick label:

Plot with wired line in x label

I had tried

fig, ax = plt.subplots()
plt.title("Collision by hour")

ax.set_xticklabels(df["hour"])
ax.plot(df_hour.index, df_hour.values)

Result with only right x_tick label:

Plot with only right x label

I had tried

fig, ax = plt.subplots()
plt.title("Collision by hour")

ax.set_xticklabels([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23])
ax.plot(df_hour.index, df_hour.values)

Result with only left x_tick label:

Plot with left label

Don’t know what else can be try.

Update:
The panda series was generate from the code df_hour = df.groupby("hour").size(), where df is a data frame containing a Int64 column call "hour", values range from 0 to 23. This made the data sereis had different structure ("hour" column name and no column name for the second column)

Asked By: xiaochuan fang

||

Answers:

Since you’re using the index as the x axis, just set xticks to be the index as well.

# btw seaborn is normally abbreviated sns
fig = sb.lineplot(x=df_hour.index, y=df_hour.values)
fig.set_xticks(df_hour.index)
fig.set_title("Collision by hour")
fig.set_xlabel('hour')
fig.set_ylabel('value')
plt.show()

Output


Data Used, if your data doesn’t look like this, you’ll have to make some minor changes:

data = {0: 35845, 1: 18921, 2: 14484, 3: 12504, 4: 13862, 5: 14958, 6: 23328, 7: 33580, 8: 66878, 9: 65291, 10: 61785, 11: 64781, 12: 68799, 13: 72486, 14: 83230, 15: 75487, 16: 88231, 17: 85383, 18: 75525, 19: 61739, 20: 51696, 21: 43215, 22: 38539, 23: 30797}

df_hour = pd.Series(data, name='hour')
Answered By: BeRT2me

As mentioned in the comment to the question the ‘trick’ is to explicit set the ticks with ax.set_xticks([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]) what can be also done using the hour column of the dataframe ax.set_xticks(df.hour).

Below the entire code and the image of created plot:

data = [
[0   ,  35845], 
[1   ,  18921], 
[2   ,  14484], 
[3   ,  12504], 
[4   ,  13862], 
[5   ,  14958], 
[6   ,  23328], 
[7   ,  33580], 
[8   ,  66878], 
[9   ,  65291], 
[10  ,  61785], 
[11  ,  64781], 
[12  ,  68799], 
[13  ,  72486], 
[14  ,  83230], 
[15  ,  75487], 
[16  ,  88231], 
[17  ,  85383], 
[18  ,  75525], 
[19  ,  61739], 
[20  ,  51696], 
[21  ,  43215], 
[22  ,  38539], 
[23  ,  30797], ]
import pandas as pd
df = pd.DataFrame(data, columns =['hour', 'collisions'])
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
plt.title("Collision by hour")
ax.plot(df.hour, df.collisions)
# ax.set_xticks([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23])
ax.set_xticks(df.hour)
plt.show()

giving:

Plot

Answered By: Claudio