【python視覺化系列】關於 pyecharts 的 'No coordinate is specified for' 報錯解決
阿新 • • 發佈:2019-02-06
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 <module> 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