简体   繁体   中英

python plot frequency with continuous integer labeled x-axis

I'm trying to make a frequency plot with special x-axis request based on the following pandas table.

     pos               freq
0     67                  1
1    285                  5
2    288                  1
3    397                  4
4    592                  1
5    640                  3

Since my frequency itself is for integers (pos column), I hope to have x-axis just with normal continuous numeric labels. However, with simple bar plot:

plot_pos=pos_freq.plot(kind='bar',x='pos')

I will only get x-axis labeled with 67, 285, 288, 397, 592, 640. Instead, I would like the x-axis be a series of continuous integers/intervals, eg 60, 65, 70, 75, ....645, 650; and that the frequency bars are still shown at the correct positions.

I'm not sure if bar plot is a good choice at this point, and I'm new to plotting in python. (I could do it in excel; Or I can probably add another x-axis...) Any suggestion for a better method is appreciated!

又快又脏:

pos_freq.reindex(range(pos_freq['pos'].min(), pos_freq['pos'].max())).plot(kind='bar')

With matplotlib ,

import matplotlib.pyplot as plt
plt.bar(pos_freq['pos'], pos_freq['freq'])
plt.show()

I couldn't get the x labels with pandas dataframe.plot() so I used matplotlib directly

import matplotlib.pyplot as plt
ticks = range(0,700,5)  #this could whatever interval you wanted
fig, ax = plt.subplots()
ax.bar(pos_freq["pos"],pos_freq["freq"])
ax.set_xticks(ticks)

and you might want to make this a little bigger too with all those extra ticks

fig.set_size_inches(30,10.5)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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