簡體   English   中英

matplotlib 難以顯示所有 x 刻度標簽

[英]matplotlib difficult to show all x tick labels

我有一個數據系列

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

我想 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)

這給了我一個只有 5 個 x_tick 標簽的 plot:

基本情節

我試過:

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)

結果缺少奇數小時和額外的開始和結束限制:

缺少奇數小時和額外限制的繪圖

我試過

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)

結果不可讀 x_tick label:

在 x 標簽中用有線線繪制

我試過

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

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

結果只有正確的 x_tick label:

僅使用右 x 標簽繪圖

我試過

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)

結果只剩下 x_tick label:

帶有左標簽的繪圖

不知道還有什么可以嘗試的。

正如對問題的評論中提到的, “技巧”是用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)的小時列來完成。

在整個代碼和創建的 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()

給予:

陰謀

由於您將索引用作 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