Reputation: 51
I am a newbie and trying to use dictionaries and plot graphs with python. I am trying to create a simple line graph with country names on x axis and population on y axis. However I get an error for this code:
#!/usr/bin/python
#
#Dictionary example
#
import sys
import matplotlib.pyplot as plt
import numpy as np
if __name__ == '__main__':
country_names = [ 'Germany', 'Italy', 'Netherlands', 'France'] #list of countries
capital_list =['Berlin', 'Rome', 'Amsterdam', 'Paris']#list of capitals to be mapped to countries
population_list= ['3.5','2.1', '0.3', '2.8'] #list of population of induvidual countries, all figures in Million
language_list=['German','Italian','Dutch','French'] #list of languages to be mapped to countries
dictionary_list = [ {'COUNTRY': country, 'CAPITAL': capital, 'POPULATION':population, 'LANGUAGE': lang} for country, capital, population, lang in zip(country_names, capital_list, population_list, language_list)]
#print(dictionary_list)
x = [d['CAPITAL'] for d in dictionary_list]
print x
y = [d['POPULATION'] for d in dictionary_list ]
print y
# create plot space upon which to plot the data
fig, ax = plt.subplots()
# add the x-axis and the y-axis to the plot
ax.plot(x, y);
Here is the error:
['Berlin', 'Rome', 'Amsterdam', 'Paris']
['3.5', '2.1', '0.3', '2.8']
Traceback (most recent call last):
File "graph.py", line 29, in <module>
ax.plot(x, y);
File "/usr/lib/python2.7/dist-packages/matplotlib/__init__.py", line 1814, in inner
return func(ax, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_axes.py", line 1425, in plot
self.add_line(line)
File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 1708, in add_line
self._update_line_limits(line)
File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 1730, in _update_line_limits
path = line.get_path()
File "/usr/lib/python2.7/dist-packages/matplotlib/lines.py", line 925, in get_path
self.recache()
File "/usr/lib/python2.7/dist-packages/matplotlib/lines.py", line 612, in recache
x = np.asarray(xconv, np.float_)
File "/usr/lib/python2.7/dist-packages/numpy/core/numeric.py", line 482, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: could not convert string to float: Berlin
Why can't x-axis and y-axis have different data types? Why do I have to convert x-axis to float?
Please help.
Thanks
Upvotes: 0
Views: 41
Reputation: 39042
It seems you are using python 2.7
. Your code works fine for me on python 3.6.5
. Nevertheless, the problem you are facing can be solved by explicitly converting the population_list
to a list of floats before creating the dictionary as
population_list= map(float, ['3.5','2.1', '0.3', '2.8'])
The above generator expression will work for you as you are looping over it. If you want to see the actual list, convert it back to a list as
population_list= list(map(float, ['3.5','2.1', '0.3', '2.8']))
By the way, I do not see a need of dictionary here. You can directly plot the lists after converting the population to float.
Upvotes: 1