Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

pandas - Matplotlib:: Not Showing all x-axis data frame variable

I have a data frame with head as:

    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
.....

I am trying to plot m_srcaddr as x-axis and other 2 columns as y axis.

df.plot(x = 'm_srcaddr')
plt.xticks(rotation=90)   #import matplotlib.pyplot as plt

plot is showing x axis (m_srcaddr) as interval of 5 like

10.19.139.141, 10.19.139.147, 10.19.139.152 ...

I am trying to use the xticks to show all x-axis data but its failing as m_srcaddr not integer.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just change your xticks call to:

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

The first argument gives the locations to put the ticks (df.index here is equivalent to np.arange(len(df)), but if your index isn't evenly spaced use the arange). The second argument gives the tick labels.


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 )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...