Andrew R. - 1 year ago 71

Python Question

I am plotting a pandas series in python using the matplotlib

`plot`

`series.plot()`

`series`

`(1, 1.1] 0.000000`

(1.1, 1.2] 0.000000

(1.2, 1.3] 0.000000

(1.3, 1.4] 0.003115

(1.4, 1.5] 0.002625

(1.5, 1.6] 0.000000

(1.6, 1.7] 0.001464

... ...

(4.9, 5.0] 0.737385

Name: series, dtype: float64

A plot of my dataframe is shown here (the red line in this case). I want the x-axis to go from 1.0 to 5.0 instead of showing the bin categories. How can I achieve this? Using

`plt.xticks`

Answer Source

Where does the series come from? Usually, I have a big dataframe of continuous-valued columns `X`

and `Y`

, and then define `X-bins`

s according to `X`

values. After that, group by `X-bins`

to calculate average (or weighted average, or sum, or any other aggregation function) of `Y`

to get the series like yours. If that's the case, I would suggest calculate mean of `X`

in this process and plot against average `X`

instead of `X-bins`

. This will be more precise than taking left/right end points or mid-points of the bins for plotting. E.g.,

```
df['Xbin'] = pd.cut(df['X'], np.arange(0,10,0.1))
```

Instead of

```
ser = df.groupby('Xbin')['Y'].mean()
ser.plot()
```

do

```
df2 = df.groupby('Xbin')[['X','Y']].mean()
df2.plot(x='X', y='Y')
```