簡體   English   中英

修改matplotlib中默認xtick標簽的方向

[英]Modify the orientation of default xtick labels in matplotlib

我已經從數據框中在matplotlib中創建了一個圖形。 我的數據框已自動使用索引名稱作為xlabel填充圖形,並將xtick標簽設置為每個索引的值(0-16)。

但是,標簽彼此重疊,我似乎無法弄清楚如何抓住它們旋轉它們,以便它們可讀。

我已經嘗試過旋轉功能,但沒有任何變化。 到目前為止,我唯一能做的就是設置plt.xticks([])並使xticks消失。 因此,似乎plt.xticks的方向正確,但是似乎沒有任何變化。

df['Count'].plot(kind = 'bar')
df['Duration'].plot(secondary_y = True, kind = 'line', color = 'red')
width = .5

ax = plt.gca()
plt.xlim([-width, len(df['Count'])-width])
plt.title('Title')
plt.ylabel('Duration (min)')
plt.xticks(rotation = 45)

這給了我(array([0,1,... 16]),),然后在不更改xticks方向的情況下打印了圖形。

xtick標簽是自動設置的(顯然),我不確定如何抓取它們。

嘗試將旋轉角度的值作為參數rot=30傳遞給第二個繪圖。 這將以指定角度旋轉x軸刻度。 使用負值順時針旋轉它們


樣本完整答案

import pandas as pd

df = pd.DataFrame({'lab':['A', 'B', 'C'], 'val':[10, 30, 20]})

df['val'].plot(kind = 'bar')
df['val'].plot(secondary_y = True, kind = 'line', color = 'red', rot=30)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM