参考1
已安装anaconda的情况,运行一下代码
conda install basemap
# 导入需要的包
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
# 初始化图形
plt.figure(figsize=(8, 8))
# 底图:圆形, lat_0:纬度;lon_o: 经度, (113,29)是武汉
m = Basemap(projection='ortho', resolution=None, lat_0=29, lon_0=113)
# 底色
m.bluemarble(scale=0.5)
# m.shadedrelief(scale=0.5)浮雕图像
plt.show()
# 以下三行是为了让matplot能显示中文
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False
fig = plt.figure(figsize=(8, 8))
# 注意几个新增的参数, width和height是用来控制放大尺度的
# 分别代表投影的宽度和高度(8E6代表 8x10^6米)
m = Basemap(projection='lcc', resolution=None,
width=8E6, height=8E6,
lat_0=23, lon_0=113,)
m.shadedrelief(scale=0.5)
# 这里的经纬度是:(经度, 纬度)
x, y = m(113, 23)
plt.plot(x, y, 'o', markersize=5)
plt.text(x, y, '深圳', fontsize=36, color="black")
plt.show()
# 导入需要的包
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from itertools import chain
def draw_map(m, scale=0.2):
# 绘制带阴影的浮雕图像
m.shadedrelief(scale=scale)
# 根据经纬度切割,每13度一条线
lats = m.drawparallels(np.linspace(-90, 90, 13))
lons = m.drawmeridians(np.linspace(-180, 180, 13))
# 集合所有线条
lat_lines = chain(*(tup[1][0] for tup in lats.items()))
lon_lines = chain(*(tup[1][0] for tup in lons.items()))
all_lines = chain(lat_lines, lon_lines)
# 循环画线
for line in all_lines:
line.set(linestyle='-', alpha=0.3, color='w')
fig = plt.figure(figsize=(8, 6), edgecolor='w')
m = Basemap(projection='cyl', resolution=None,
llcrnrlat=-90, urcrnrlat=90,
llcrnrlon=-180, urcrnrlon=180,)
draw_map(m)
plt.show()
plt.show()
前添加一下代码def draw_point(m, x, y, name):
# 这里的经纬度是:(经度, 纬度)
x, y = m(x, y)
plt.plot(x, y, 'ok', markersize=5)
plt.text(x, y, name, fontsize=12, color="black")
locations = {
'泰姬陵': (17, 78),
'吉萨金字塔群': (29, 31),
'英国的巨石阵': (51, 1),
'巴黎圣母院': (48, 2),
'卢浮宫': (48, 2),
'红场和克里姆林': (55, 37),
# ...
}
for loc in locations:
print(locations[loc])
draw_point(m, locations[loc][1], locations[loc][0], loc)
下载链接
注意:pyechartsde 0.5.x与1.x.x 变动较大。这里下载的是最新版本,图片右上角绿色链接可下载旧版本。
版本变动说明
# 0.5.x版本
from pyecharts import Bar
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
bar = Bar("柱状图数据堆叠示例")
bar.add("商家A", attr, v1, is_stack=True)
bar.add("商家B", attr, v2, is_stack=True)
bar.render()
# 1.x.x版本
import pyecharts.options as opts
from pyecharts.charts import Bar
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
bar = (
Bar()
.add_xaxis(attr)
.add_yaxis("商家A", v1, stack="stack1")
.add_yaxis("商家B", v2, stack="stack1")
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="柱状图数据堆叠示例"))
)
bar.render()
/
1.x.x用法说明参考
/
from pyecharts import options as opts
from pyecharts.charts import Map
import random
province = ['广东', '湖北', '湖南', '四川', '重庆', '黑龙江', '浙江', '山西', '河北', '安徽', '河南', '山东', '西藏']
data = [(i, random.randint(50, 150)) for i in province]
_map = (
Map()
.add("销售额", data, "china")
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-基本示例"),
legend_opts=opts.LegendOpts(is_show=False),
visualmap_opts=opts.VisualMapOpts(max_=200, is_piecewise=True),
)
)
_map.render_notebook()