Python 作为matplotlib图的简单字典

Python 作为matplotlib图的简单字典,python,matplotlib,Python,Matplotlib,我是一个新手,尝试用python使用字典和绘图。 我试图创建一个简单的线图,在x轴上显示国家名称,在y轴上显示人口。但是,我得到一个错误的代码: #!/usr/bin/python # #Dictionary example # import sys import matplotlib.pyplot as plt import numpy as np if __name__ == '__main__': country_names = [ 'Germany', 'Italy', 'N

我是一个新手,尝试用python使用字典和绘图。 我试图创建一个简单的线图,在x轴上显示国家名称,在y轴上显示人口。但是,我得到一个错误的代码:

#!/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);
以下是错误:

['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
[‘柏林’、‘罗马’、‘阿姆斯特丹’、‘巴黎’]
['3.5', '2.1', '0.3', '2.8']
回溯(最近一次呼叫最后一次):
文件“graph.py”,第29行,在
ax.图(x,y);
文件“/usr/lib/python2.7/dist-packages/matplotlib/_-init__;u.py”,第1814行,在内部
返回函数(ax,*args,**kwargs)
文件“/usr/lib/python2.7/dist packages/matplotlib/axes/_axes.py”,第1425行,在绘图中
自我添加_行(行)
文件“/usr/lib/python2.7/dist packages/matplotlib/axes/_base.py”,第1708行,添加行
自我更新行限制(行)
文件“/usr/lib/python2.7/dist packages/matplotlib/axes/_base.py”,第1730行,在“更新”行中
path=line.get_path()
文件“/usr/lib/python2.7/dist packages/matplotlib/lines.py”,第925行,在get\u路径中
self.recache()
文件“/usr/lib/python2.7/dist packages/matplotlib/lines.py”,第612行,在recache中
x=np.asarray(xconv,np.float_ux)
asarray中的文件“/usr/lib/python2.7/dist packages/numpy/core/numeric.py”,第482行
返回数组(a,数据类型,copy=False,order=order)
ValueError:无法将字符串转换为浮点:0
为什么x轴和y轴不能有不同的数据类型?为什么必须将x轴转换为浮动

请帮忙


谢谢

您似乎正在使用
Python2.7
。您的代码在python 3.6.5上运行良好。然而,您所面临的问题可以通过在创建字典之前将
总体列表
显式转换为浮动列表来解决

population_list= map(float, ['3.5','2.1', '0.3', '2.8'])
当您在上面循环时,上面的生成器表达式将适用于您。如果要查看实际列表,请将其转换回列表,如下所示:

population_list= list(map(float, ['3.5','2.1', '0.3', '2.8']))
顺便说一下,我看这里不需要字典。将填充转换为浮动后,可以直接绘制列表


太好了。非常感谢你的回答。W.r.t使用字典,这更像是一个学习练习,我需要将国家数据表提取到列表中,然后对其进行一些操作