Reputation: 135
I am in my initial days of learning python (2 months with no past coding experience)
I have a dataframe as follows -
| | Vehicle class | Year | Base_Tot_Sec_Vol | Base_OFW_Volume |
|---:|--------------:|-----:|-----------------:|-----------------|
| 0 | 2W | 2018 | 7.107110e+07 | 0.000000e+00 |
| 1 | 2W | 2020 | 7.218810e+07 | 0.000000e+00 |
| 2 | 2W | 2030 | 7.524556e+07 | 0.000000e+00 |
| 3 | BC | 2018 | 6.935553e+07 | 1.522439e+07 |
| 4 | BC | 2020 | 5.570438e+07 | 1.222779e+07 |
| 5 | BC | 2030 | 3.955456e+07 | 8.682708e+06 |
| 6 | CV | 2018 | 4.480700e+08 | 9.835683e+07 |
| 7 | CV | 2020 | 3.597294e+08 | 7.896498e+07 |
| 8 | CV | 2030 | 2.820582e+08 | 6.191522e+07 |
| 9 | PV | 2018 | 4.208246e+08 | 0.000000e+00 |
| 10 | PV | 2020 | 4.315216e+08 | 0.000000e+00 |
| 11 | PV | 2030 | 3.045124e+08 | 0.000000e+00 |
I wanted to plot a graph with year as x-axis and y-axis as Base_Tot_Sec_Vol.
When I run my code as below -
import pandas as pd
import matplotlib .pyplot as plt
fig,axes = plt.subplots()
width = 0.35
axes.bar(df['Year'].unique(),df[df['Vehicle class'] == '2W']['Base_Tot_Sec_Vol'], width,label = '2W')
axes.legend(loc=0)
plt.show()
The output is shown as below -
I am unable to understand why the 'Year' column which only contains 2018, 2020 and 2030 is showing additional values in the chart. I tried changing the Year column which is of dtype = int to dtype = category using df['Year'].astype('category') but it did not produce any change.
Please help.
Thanks in advance.
Upvotes: 0
Views: 118
Reputation: 150825
Try using string
instead:
df['Year'] = df['Year'].astype(str)
df[df['Vehicle class'] == '2W'].plot(x='Year', y='Base_Tot_Sec_Vol', kind='bar')
Upvotes: 1