Issue
I am trying to plot a column as the x axis and another column as the y axis but get the above error. I can see the data and can access it using head() or sum().
Here is the code itself:
dengue_by_region = ph_dengue_cases2016_2020.groupby('Region')
dengue_by_region.head()
x = dengue_by_region['Dengue_Cases'].sum()
y = dengue_by_region['Region']
plt.bar(x, y)
I am unsure why I am unable to using x on the x axis and y on the y without it bringing up the error. Any help would be very much appreciated. I am using Jupyter Notebook if that helps.
Solution
plt.bar(x, y)
function makes a bar plot with x
as labels at the bottom, and y
as values, both must be either a scalar or list-like. E.g., like in picture:
Now I'm not sure of what types exactly are x
and y
in your code, but I guess that at least one of them is not scalar or list-like.
I presume that what you are trying to achieve is a bar plot with Regions as x-labels, and Sum as y-labels. And for that it appears you don't need to change much, as @MichaelDelgado pointed in comments:
# groupby will return series,
# with "Regions" as index (x-labels for the plot)
# and sum by regions as values (y-labels for the plot)
x = ph_dengue_cases2016_2020.groupby('Region')['Dengue_Cases'].sum()
plt.bar(x.index, x)
plt.show()
Or, you can make it all much simpler by using what pandas
provides you for the plotting:
# here I start from clean plot, just in case
plt.clf()
cases_by_region = ph_dengue_cases2016_2020.groupby('Region')['Dengue_Cases'].sum()
cases_by_region.plot.bar()
plt.show()
Answered By - qaziqarta
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.