Reputation: 327
I produce a bar graph with the following code:
Temp_Counts = Counter(weatherDFConcat['TEMPBIN_CON'])
df = pd.DataFrame.from_dict(Temp_Counts, orient = 'index')
df.plot(kind = 'bar')
It produces the following bar chart:
I would like to change the order the columns are listed. I've tried a few different things that aren't seeming to work. The DataFrame I am working with looks like this:
I am creating a barplot of the last column in the dataframe pictured. Any direction or ideas of alternate methods of creating a barplot that would allow me to change the order of the axis would be greatly appreciated. As it exists its difficult to interpret.
Upvotes: 4
Views: 28690
Reputation: 862406
I think you can use value_counts
with sort_index
and last Series.plot.bar
:
weatherDFConcat['TEMPBIN_CON'].value_counts().sort_index().plot.bar()
Or if you want use your solution:
Temp_Counts = Counter(weatherDFConcat['TEMPBIN_CON'])
df = pd.DataFrame.from_dict(Temp_Counts, orient = 'index').sort_index()
df.plot(kind = 'bar')
Sample:
import matplotlib.pyplot as plt
weatherDFConcat = pd.DataFrame({'TEMPBIN_CON':['30 degrees or more', '-15 to -18 degrees', '-3 to 0 degrees', '15 to 18 degrees', '-15 to -18 degrees'
]})
print (weatherDFConcat)
TEMPBIN_CON
0 30 degrees or more
1 -15 to -18 degrees
2 -3 to 0 degrees
3 15 to 18 degrees
4 -15 to -18 degrees
weatherDFConcat['TEMPBIN_CON'].value_counts().sort_index().plot.bar()
plt.show()
Upvotes: 4
Reputation: 10544
I would suggest that you split the TEMPBIN_CON
in two: LOWER_TEMP
and HIGHER_TEMP
. Then, you sort the DataFrame by these columns using:
sorted = df.sort_values(by=['LOWER_TEMP', 'HIGHER_TEMP'])
and then you do the plot as you have already done:
sorted.plot(kind='bar')
Upvotes: 2