目录
0、安装:pip install pygal_maps_world
此时可以访问pygal.maps.world模块
1、指定代码绘制国家
# # 导入需要的包
# import numpy as np
# import matplotlib.pyplot as plt
# from mpl_toolkits.basemap import Basemap
# # conda install basemap
# # pip install -U matplotlib==3.2
# # pip install packagename
# #export PROJ_LIB=/opt/conda/share/proj
# import os
# import conda
#
# conda_file_dir = conda.__file__
# conda_dir = conda_file_dir.split('lib')[0]
# proj_lib = os.path.join(os.path.join(conda_dir, 'share'), 'proj')
# os.environ["PROJ_LIB"] = proj_lib
#
#
#
# # 初始化图形
#
# 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()
#
#
# pip install pyecharts echarts-countries-pypkg echarts-china-provinces-pypkg echarts-china-cities-pypkg echarts-china-counties-pypkg echarts-china-misc-pypkg echarts-united-kingdom-pypkg
# 导入需要的包
import pygal.maps.world
worldmap_chart = pygal.maps.world.World()
worldmap_chart.title = 'Some countries'
worldmap_chart.add('F countries', ['fr', 'fi'])
worldmap_chart.add('M countries', ['ma', 'mc', 'md', 'me', 'mg',
'mk', 'ml', 'mm', 'mn', 'mo',
'mr', 'mt', 'mu', 'mv', 'mw',
'mx', 'my', 'mz'])
worldmap_chart.add('U countries', ['ua', 'ug', 'us', 'uy', 'uz'])
worldmap_chart.render_to_file('bar_chart.svg')
生成文件:
在火狐浏览器中打开显示即可:
2、指定某个国家的标签值
import pygal.maps.world
worldmap_chart = pygal.maps.world.World()
worldmap_chart.title = 'Minimum deaths by capital punishement (source: Amnesty International)'
worldmap_chart.add('In 2012', {
'af': 14,
'bd': 1,
'by': 3,
'cn': 1000,
'gm': 9,
'in': 1,
'ir': 314,
'iq': 129,
'jp': 7,
'kp': 6,
'pk': 1,
'ps': 6,
'sa': 79,
'so': 6,
'sd': 5,
'tw': 1000,
'ae': 1,
'us': 43,
'ye': 28
})
worldmap_chart.render_to_file('bar_chart.svg')
生成结果:
3、获取大洲区域地图
import pygal.maps.world
supra = pygal.maps.world.SupranationalWorld()
supra.add('Asia', [('asia', 1)])
supra.add('Europe', [('europe', 1)])
supra.add('Africa', [('africa', 1)])
supra.add('North america', [('north_america', 1)])
supra.add('South america', [('south_america', 1)])
supra.add('Oceania', [('oceania', 1)])
supra.add('Antartica', [('antartica', 1)])
supra.render_to_file('bar_chart.svg')
4、雷达图
import pygal
radar_chart = pygal.Radar()
radar_chart.title = 'V8 benchmark results'
radar_chart.x_labels = ['Richards', 'DeltaBlue', 'Crypto', 'RayTrace', 'EarleyBoyer', 'RegExp', 'Splay', 'NavierStokes']
radar_chart.add('Chrome', [6395, 8212, 7520, 7218, 12464, 1660, 2123, 8607])
radar_chart.add('Firefox', [7473, 8099, 11700, 2651, 6361, 1044, 3797, 9450])
radar_chart.add('Opera', [3472, 2933, 4203, 5229, 5810, 1828, 9013, 4669])
radar_chart.add('IE', [43, 41, 59, 79, 144, 136, 34, 102])
radar_chart.render_to_file('bar_chart.svg')
5、基本饼图(Pie)
import pygal
pie_chart = pygal.Pie()
pie_chart.title = 'Browser usage in February 2012 (in %)'
pie_chart.add('IE', 19.5)
pie_chart.add('Firefox', 36.6)
pie_chart.add('Chrome', 36.3)
pie_chart.add('Safari', 4.5)
pie_chart.add('Opera', 2.3)
pie_chart.render_to_file('bar_chart.svg')
6、条形图
'''绘制竖向堆叠条形图 多图横向排列'''
bar_chart = pygal.HorizontalStackedBar()
# 条形图标题
bar_chart.title = "Remarquable sequences"
# x轴标题
bar_chart.x_labels = map(str, range(11))
bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])
bar_chart.add('Padovan', [1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12])
bar_chart.render_to_file('bar_chart.svg')
import pygal
line_chart = pygal.Bar()
line_chart.title = 'Browser usage evolution (in %)'
line_chart.x_labels = map(str, range(2002, 2013))
line_chart.add('Firefox', [None, None, 0, 16.6, 25, 31, 36.4, 45.5, 46.3, 42.8, 37.1])
line_chart.add('Chrome', [None, None, None, None, None, None, 0, 3.9, 10.8, 23.8, 35.3])
line_chart.add('IE', [85.8, 84.6, 84.7, 74.5, 66, 58.6, 54.7, 44.8, 36.2, 26.6, 20.1])
line_chart.add('Others', [14.2, 15.4, 15.3, 8.9, 9, 10.4, 8.9, 5.8, 6.7, 6.8, 7.5])
line_chart.render_to_file('bar_chart.svg')
6、词云图
from pyecharts import Line
from pyecharts import Pie
from pyecharts import WordCloud
from pyecharts import EffectScatter, Overlap
# 词云图
name = [
'Though', 'the answer', 'this question',
'may at first', 'seem to border', 'on the',
'absurd', 'reflection', 'will show', 'that there',
'is a', 'good deal', 'more in', 'it than meets', 'the eye'
]
value = [10000, 6189, 4556, 2356, 2233,
1895, 1456, 1255, 981, 875,
542, 462, 361, 265, 125]
worldcloud = WordCloud(width=1300, height=620)
worldcloud.add('词云', name, value, word_size_range=[20, 100])
worldcloud.render('worldcloud.html')
参考:https://www.jb51.net/article/180024.htm
7、柱状图
参考:https://www.jb51.net/article/180024.htm
from pyecharts import Bar
# 基本柱状图
bar = Bar("基本柱状图", "副标题")
bar.use_theme('dark') # 暗黑色主题
bar.add('真实成本', # label
["1月", "2月", "3月", "4月", "5月", "6月"], # 横坐标
[5, 20, 36, 10, 75, 90], # 纵坐标
is_more_utils=True) # 设置最右侧工具栏
# bar.show_config() # 调试输出pyecharts的js的配置信息
bar.render('bar_demo.html') # 生成html文件
8、折线柱状图
from pyecharts import Bar
# 基本柱状图
import math
LISTS = []
ins = []
for i in range(100):
LISTS.append(math.exp(2* math.sqrt(i*math.atan(i))))
ins.append(i)
bar = Bar("基本柱状图", "副标题")
bar.use_theme('dark') # 暗黑色主题
bar.add('真实成本', # label
ins, # 横坐标
LISTS, # 纵坐标
is_more_utils=True) # 设置最右侧工具栏
# bar.show_config() # 调试输出pyecharts的js的配置信息
bar.render('bar_demo.html') # 生成html文件
附件
引用参考文章:
https://www.cnblogs.com/keqipu/p/7283991.html
https://www.cnblogs.com/keqipu/p/7283439.html
https://www.cnblogs.com/keqipu/p/7281599.html
Python数据可视化之基于pyecharts实现的地理图表的绘制
https://www.jb51.net/article/214614.htm