Reputation: 1888
All I'd like to do is make a simple barchart of the populations of the countries of the world.
Ideally, the x-axis would have country names, small font, but slanting diagonally; the y-axis would be logarithmic.
Here's what I'm doing so far:
import pandas as pd
import numpy as np
import matplotlib as plt
import matplotlib.pyplot as plt
cols = ['Rank', 'Country', 'UN_Continental_region', 'UN_Statistical_region', 'Population', 'Population2015', 'percent_change']
pop_list = pd.read_table('country.dat', names=cols)
pop_list['Population'].plot().hist(alpha=0.5)
plt.show()
The plot().hist line gives a TypeError: hist() missing 1 required positional argument: 'x' error, but then a the plt.show() doe make a line plot of the populations.
What's going on???!!
The full code can be found here: https://github.com/d80b2t/python/blob/master/wealth_and_population/population_barchart_forStackOverflow.ipynb
Upvotes: 0
Views: 2437
Reputation: 86320
The reason you get a TypeError
is that you are calling plot().hist()
when you should be calling plot.hist()
(note the lack of parentheses after plot
).
The DataFrame.plot
object, without parentheses, is a pandas plot API instance, which has a hist()
method that references the associated dataframe. When you call that plot API object itself with DataFrame.plot()
, it returns a matplotlib axis instance, which has a hist()
method that requires an array as the first argument (it's the normal ax.hist()
method that you might be familiar with from matplotlib).
The reason the plot still shows up is that when you do pop_list.plot()
, it creates a line graph. It's not until you call the hist()
method of that axis that you get the error... you've already created the plot!
So, to get rid of the type error, use
pop_list.plot.hist()
But fixing that will give you a histogram of populations, and it sounds like you want a bar chart, not a histogram. To create a bar chart, you can use pop_list.plot.bar
. This is something like what you want:
pop_list.plot.bar('Country', 'Population')
But there are so many countries in your data that the chart is too busy to be very useful.
Upvotes: 1