简体   繁体   English

matplotlib 难以显示所有 x 刻度标签

[英]matplotlib difficult to show all x tick labels

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我想 plot 数据系列并显示所有 x 刻度标签,范围从 0 到 23。基本 plot 的代码是

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:这给了我一个只有 5 个 x_tick 标签的 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:结果缺少奇数小时和额外的开始和结束限制:

缺少奇数小时和额外限制的绘图

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:结果不可读 x_tick label:

在 x 标签中用有线线绘制

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:结果只有正确的 x_tick label:

仅使用右 x 标签绘图

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:结果只剩下 x_tick label:

带有左标签的绘图

Don't know what else can be try.不知道还有什么可以尝试的。

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) .正如对问题的评论中提到的, “技巧”是用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])也可以使用 dataframe ax.set_xticks(df.hour)的小时列来完成。

Below the entire code and the image of created plot:在整个代码和创建的 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:给予:

阴谋

Since you're using the index as the x axis, just set xticks to be the index as well.由于您将索引用作 x 轴,因此只需将 xticks 也设置为索引。

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

输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM