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:
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:
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:
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:
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:
Don't know what else can be try.
CodePudding user response:
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:
CodePudding user response:
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()