gpt4 book ai didi

pandas - Matplotlib::不显示所有 x 轴数据框变量

转载 作者:行者123 更新时间:2023-12-01 11:30:23 26 4
gpt4 key购买 nike

我有一个头部的数据框:

    m_srcaddr       total_fwd_size  total_rev_size
0 10.19.139.141 2479.335 175.000
1 10.19.139.143 888.141 92.442
2 10.19.139.144 1044.360 214.592
3 10.19.139.145 7.205 3.274
4 10.19.139.146 2756.958 294.006
.....

我试图将 m_srcaddr 绘制为 x 轴,将其他 2 列绘制为 y 轴。
df.plot(x = 'm_srcaddr')
plt.xticks(rotation=90) #import matplotlib.pyplot as plt

绘图将 x 轴(m_srcaddr)显示为 5 的间隔,例如
10.19.139.141, 10.19.139.147, 10.19.139.152 ...

我正在尝试使用 xticks 来显示所有 x 轴数据,但由于 m_srcaddr 不是整数而失败。

最佳答案

只需更改您的 xticks 拨电至:

plt.xticks(df.index, df['m_srcaddr'], rotation=90)

第一个参数给出了放置刻度的位置( df.index 在这里相当于 np.arange(len(df)) ,但如果您的索引不是均匀分布的,请使用 arange )。第二个参数给出刻度标签。

matplotlib.pyplot.xticks( *args, **kwargs )

Get or set the x-limits of the current tick locations and labels.

# return locs, labels where locs is an array of tick locations and
# labels is an array of tick labels.
locs, labels = xticks()

# set the locations of the xticks
xticks( arange(6) )

# set the locations and labels of the xticks
xticks( arange(5), ('Tom', 'Dick', 'Harry', 'Sally', 'Sue') )

The keyword args, if any, are Text properties. For example, to rotate long labels:

xticks( arange(12), calendar.month_name[1:13], rotation=17 )

关于pandas - Matplotlib::不显示所有 x 轴数据框变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32572419/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com