Home > OS >  Pandas bar plot x axis stuck on wrong column
Pandas bar plot x axis stuck on wrong column

Time:01-26

I want to plot a bar plot from pandas dataframe and have a reasonable amount of xticks.

My dataframe (data) looks like this:

0  Channel  Counts  Energy [keV]
0        1       0         -0.02
0        2       0          0.01
0        3       0          0.04 
...
2     2044       2         58.81
1     2045       1         58.83
[2048 rows x 4 columns]

I'm plotting the bar plot with x = data["Energy [keV]"] and height = data["Counts"] and whatever I try I get either:

  • All 2048 energy values with labels which become a jumbled mess
  • First 50 (for example) energy values jumbled together at the beginning of the x-axis

Basically the plot is plotting the position of the bars right (using the energy values) and when I try to manipulate ticks, or add a vertical line it always uses 0 to 2048 x axis.

What I get is that: enter image description here

While the actual data looks like this:

enter image description here

You of course can try fix it somehow by Plot

or:

markerline, stemlines, baseline = ax4.stem(data["Energy [keV]"], data["Counts"], markerfmt=' ')
plt.setp(stemlines, 'linewidth', 0.4)
ax4.set_xlim(data["Energy [keV]"].min(), data["Energy [keV]"].max())
ax4.set_ylim(0, data["Counts"].max()*1.1)
ax4.axvline(x=25.0, color = 'darkred')

plot 2

  •  Tags:  
  • Related