【python可视化系列】关于 pyecharts 的 'No coordinate is specified for' 报错解决

pyecharts 是一个用于生成 Echarts 图表的类库。Echarts 是百度开源的一个数据可视化 JS 库。用 Echarts 生成的图可视化效果非常棒,pyecharts 是为了与 Python 进行对接,方便在 Python 中直接使用数据生成图。

实现地图可视的过程过遇到如下问题:

Traceback (most recent call last):

  File "/Users/a6/PycharmProjects/Attempt_new/try_echarts/echarts.py", line 8, in 
    symbol_size=10, is_visualmap=True)
  File "/Library/Python/2.7/site-packages/pyecharts/charts/geo.py", line 47, in add
    self.__add(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/pyecharts/charts/geo.py", line 103, in __add
    _coordinate = self.get_coordinate(_name, raise_exception=True)
  File "/Library/Python/2.7/site-packages/pyecharts/charts/geo.py", line 42, in get_coordinate
    raise ValueError("No coordinate is specified for {}".format(name))
ValueError: No coordinate is specified for 广州

No coordinate is specified for 译文是“没有指定坐标”,即应该是由于没有找到相关区域的坐标导致。

出错源代码如下:

# -*- coding:utf-8 -*-
from pyecharts import Geo
data = [('广州', 80), ('漳州', 180)]
geo = Geo("全国主要城市空气质量", "data from pm2.5")
attr, value = geo.cast(data)
geo.add("city", attr, value, visual_range=[0, 200], maptype='china', visual_text_color="#fff",
        symbol_size=10, is_visualmap=True)
geo.show_config()
geo.render()

如果你也发生了这样的报错,可以从这两方面查找:
第一、请先检查,是否有 pip 相关的地图信息。

第二、主要是否有交代编码为utf-8。

一、安装python地图包过程

根据 pyecharts使用教程 :

自从 v0.3.2 开始,为了缩减项目本身的体积以及维持 pyecharts 项目的轻量化运行,pyecharts 将不再自带地图 js 文件。如用户需要用到地图图表,可自行安装对应的地图文件包。下面介绍如何安装。

(1)、全球国家地图: echarts-countries-pypkg (1.9MB): 世界地图和 213 个国家,包括中国地图
(2)、中国省级地图: echarts-china-provinces-pypkg (730KB):23 个省,5 个自治区
(3)、中国市级地图: echarts-china-cities-pypkg (3.8MB):370 个中国城市
(4)、中国县区级地图: echarts-china-counties-pypkg (4.1MB):2882 个中国县·区
(5)、中国区域地图: echarts-china-misc-pypkg (148KB):11 个中国区域地图,比如华南、华北

需要这些地图的朋友,可以装 pip 命令行:
$ pip install echarts-countries-pypkg
$ pip install echarts-china-provinces-pypkg
$ pip install echarts-china-cities-pypkg
$ pip install echarts-china-counties-pypkg
$ pip install echarts-china-misc-pypkg
$ pip install echarts-united-kingdom-pypkg
更多的地图数据可查看https://github.com/echarts-maps。

二、关于中文字符问题:主要是否有交代编码为utf-8。

# -*- coding:utf-8 -*-  
import sys 
reload(sys)  
sys.setdefaultencoding('utf8')

在代码开头添加如上几行代码,如果还是不行,请在中文字符前面使用u,例如 u'广州'。

三、示例:

在问题解决了,那下面给出实现地图可视化的相关示例,代码如下:

例子一、

# -*- coding:utf-8 -*-
from pyecharts import Geo
data = [(u'广州', 80), (u'漳州', 180)]
geo = Geo("全国主要城市空气质量", "data from pm2.5")
attr, value = geo.cast(data)
geo.add("city", attr, value, visual_range=[0, 200], maptype='china', visual_text_color="#fff",
        symbol_size=10, is_visualmap=True)
geo.show_config()
geo.render()

结果如下:


例子二:

# -*- coding: utf-8 -*-
from pyecharts import Geo
import sys
reload(sys)
sys.setdefaultencoding('utf8')
data = [
    (u"海门", 9), (u"鄂尔多斯", 12), (u"招远", 12), (u"舟山", 12), (u"齐齐哈尔", 14), (u"盐城", 15),
    (u"赤峰", 16), (u"青岛", 18), (u"乳山", 18), (u"金昌", 19), (u"泉州", 21), (u"莱西", 21),
    (u"日照", 21), (u"胶南", 22), (u"南通", 23), (u"拉萨", 24), (u"云浮", 24), (u"梅州", 25),
    (u"文登", 25), (u"上海", 25), (u"攀枝花", 25), (u"威海", 25), (u"承德", 25), (u"厦门", 26),
    (u"汕尾", 26), (u"潮州", 26), (u"丹东", 27), (u"太仓", 27), (u"曲靖", 27), (u"烟台", 28),
    (u"福州", 29), (u"瓦房店", 30), (u"即墨", 30), (u"抚顺", 31), (u"玉溪", 31), (u"张家口", 31),
    (u"阳泉", 31), (u"莱州", 32), (u"湖州", 32), (u"汕头", 32), (u"昆山", 33), (u"宁波", 33),
    (u"湛江", 33), (u"揭阳", 34), (u"荣成", 34), (u"连云港", 35), (u"葫芦岛", 35), (u"常熟", 36),
    (u"东莞", 36), (u"河源", 36), (u"淮安", 36), (u"泰州", 36), (u"南宁", 37), (u"营口", 37),
    (u"惠州", 37), (u"江阴", 37), (u"蓬莱", 37), (u"韶关", 38), (u"嘉峪关", 38), (u"广州", 38),
    (u"延安", 38), (u"太原", 39), (u"清远", 39), (u"中山", 39), (u"昆明", 39), (u"寿光", 40),
    (u"盘锦", 40), (u"长治", 41), (u"深圳", 41), (u"珠海", 42), (u"宿迁", 43), (u"咸阳", 43),
    (u"铜川", 44), (u"平度", 44), (u"佛山", 44), (u"海口", 44), (u"江门", 45), (u"章丘", 45),
    (u"肇庆", 46), (u"大连", 47), (u"临汾", 47), (u"吴江", 47), (u"石嘴山", 49), (u"沈阳", 50),
    (u"苏州", 50), (u"茂名", 50), (u"嘉兴", 51), (u"长春", 51), (u"胶州", 52), (u"银川", 52),
    (u"张家港", 52), (u"三门峡", 53), (u"锦州", 54), (u"南昌", 54), (u"柳州", 54), (u"三亚", 54),
    (u"自贡", 56), (u"吉林", 56), (u"阳江", 57), (u"泸州", 57), (u"西宁", 57), (u"宜宾", 58),
    (u"呼和浩特", 58), (u"成都", 58), (u"大同", 58), (u"镇江", 59), (u"桂林", 59), (u"张家界", 59),
    (u"宜兴", 59), (u"北海", 60), (u"西安", 61), (u"金坛", 62), (u"东营", 62), (u"牡丹江", 63),
    (u"遵义", 63), (u"绍兴", 63), (u"扬州", 64), (u"常州", 64), (u"潍坊", 65), (u"重庆", 66),
    (u"台州", 67), (u"南京", 67), (u"滨州", 70), (u"贵阳", 71), (u"无锡", 71), (u"本溪", 71),
    (u"克拉玛依", 72), (u"渭南", 72), (u"马鞍山", 72), (u"宝鸡", 72), (u"焦作", 75), (u"句容", 75),
    (u"北京", 79), (u"徐州", 79), (u"衡水", 80), (u"包头", 80), (u"绵阳", 80), (u"乌鲁木齐", 84),
    (u"枣庄", 84), (u"杭州", 84), (u"淄博", 85), (u"鞍山", 86), (u"溧阳", 86), (u"库尔勒", 86),
    (u"安阳", 90), (u"开封", 90), (u"济南", 92), (u"德阳", 93), (u"温州", 95), (u"九江", 96),
    (u"邯郸", 98), (u"临安", 99), (u"兰州", 99), (u"沧州", 100), (u"临沂", 103), (u"南充", 104),
    (u"天津", 105), (u"富阳", 106), (u"泰安", 112), (u"诸暨", 112), (u"郑州", 113), (u"哈尔滨", 114),
    (u"聊城", 116), (u"芜湖", 117), (u"唐山", 119), (u"平顶山", 119), (u"邢台", 119), (u"德州", 120),
    (u"济宁", 120), (u"荆州", 127), (u"宜昌", 130), (u"义乌", 132), (u"丽水", 133), (u"洛阳", 134),
    (u"秦皇岛", 136), (u"株洲", 143), (u"石家庄", 147), (u"莱芜", 148), (u"常德", 152), (u"保定", 153),
    (u"湘潭", 154), (u"金华", 157), (u"岳阳", 169), (u"长沙", 175), (u"衢州", 177), (u"廊坊", 193),
    (u"菏泽", 194), (u"合肥", 229), (u"武汉", 273), (u"大庆", 279)]
geo = Geo("全国主要城市空气质量", "Data - PM2.5 - by yz", title_color="#fff",
          title_pos="center", width=1200,
          height=600, background_color='#404a59')
attr, value = geo.cast(data)
geo.add("", attr, value, visual_range=[0, 200], maptype='china', visual_text_color="#fff",
        symbol_size=10, is_visualmap=True)
geo.show_config()
geo.render("全国主要城市空气质量.html")  # 生成html文件

最后,打开生成的 html 文件可以得到做好的图,结果如下:


参考:https://zhuanlan.zhihu.com/p/37354183

你可能感兴趣的:(Python,数据可视化)