把pyecharts从0.5版升级以后,报错如下:
lmportError: cannot import name 'Geo' from 'pyecharts‘
参考这个:python画图时,from pyecharts import Geo时出错_cannot import name 'geo' from 'pyecharts-优快云博客
改成:
from pyecharts.charts import Geo
但是又报了如题错误:
> geo = Geo(title, subtitle, title_color="#000000",
title_pos="center", width=1200,
height=600, background_color='#fff')
E TypeError: Geo.__init__() got an unexpected keyword argument 'title_color'
cpca/drawer.py:123: TypeError
解决:使用兼容性写法来初始化Geo类
def init_geo(title, subtitle, title_color, background_color, title_pos='center', width=1200, height=600):
# 兼容新旧版本的pyecharts
try:
from pyecharts.charts import Geo
from pyecharts.options import InitOpts, TitleOpts
from pyecharts.options.series_options import TextStyleOpts
geo = Geo(
init_opts=InitOpts(
width=width,
height=height,
bg_color=background_color,
)
)
geo.set_global_opts(
TitleOpts(
title=title,
subtitle=subtitle,
text_align=title_pos,
title_textstyle_opts=TextStyleOpts(color=title_color),
)
)
return geo
except ImportError:
from pyecharts import Geo
return Geo(
title,
subtitle,
title_color=title_color,
background_color=background_color,
title_pos=title_pos,
width=width,
height=height,
)
def echarts_draw(...):
...
geo = init_geo(title, subtitle, title_color="#fff", background_color='#404a59')
geo._coordinates = coordinates
def echarts_cate_draw(...):
...
geo = init_geo(title, subtitle, title_color="#000000", background_color='#fff')
geo._coordinates = coordinates
...