简体   繁体   English

使用 pandas.DataFrame.plot() 时如何显示所有 xticks?

[英]How to display all the xticks when using pandas.DataFrame.plot()?

I'm struggling to get all the ticks in the horizontal axis when plotting a pandas dataframe.在绘制 pandas dataframe 时,我正在努力获得水平轴上的所有刻度。 This is my MWE:这是我的 MWE:

import pandas as pd

letters = 'ABCDEFGHIJKL'
data = [{'Letter': letter, 'Number': number} 
        for number, letter in enumerate(letters)]

df = pd.DataFrame(data)
df.set_index('Letter', inplace=True)

The dataframe looks like this: dataframe 看起来像这样:

In [484]: df
Out[484]: 
        Number
Letter        
A            0
B            1
C            2
D            3
E            4
F            5
G            6
H            7
I            8
J            9
K           10
L           11

When I plot the dataframe only half of the xticks are displayed:当我 plot dataframe 只显示一半的 xticks 时:

df.plot(kind='line', use_index=True)

阴谋

How can I get all the xticks (from A through L )?我怎样才能得到所有的 xticks(从AL )?

In order to force ticks you need to explicitly set xticks and xtickslabels为了强制刻度,您需要显式设置xticksxtickslabels

ax = df.plot(kind='line', use_index=True)
ax.set_xticks(range(len(df.index)))
ax.set_xticklabels(df.index)

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

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