1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Python科学绘图 南丁格尔图/玫瑰图

Python科学绘图 南丁格尔图/玫瑰图

时间:2018-10-02 08:48:36

相关推荐

Python科学绘图 南丁格尔图/玫瑰图

文章目录

Github/GItee仓库地址注意生成数据乱序数据渲染图片为png或jpeg顺序数据

Github/GItee仓库地址

ScienceGallery-GithubScienceGallery-Gitee

注意

数据传入到 pyecharts 的时候,需要自行将数据格式转换成上述 Python 原生的数据格式。方法:

# for int[int(x) for x in your_numpy_array_or_something_else]# for float[float(x) for x in your_numpy_array_or_something_else]# for str[str(x) for x in your_numpy_array_or_something_else]

便捷方法:pandas.Series.tolist()

生成数据

import numpy as np# 生成数据x_data = [chr(i) for i in range(65, 91)] # lable,26个字母[str(i) for i in x_data]y_data = np.random.uniform(5,200,26) # data[float(i) for i in y_data]# 将数据转换为列表加元组的格式([(key1, value1), (key2, value2)])data=[list(z) for z in zip(x_data, y_data)]

乱序数据

南丁格尔图,通过半径区分数据大小,有’radius’和’area’两种模式。 radius:扇区圆心角展现数据的百分比,半径展现数据的大小area:所有扇区圆心角相同,仅通过半径展现数据大小

from pyecharts.charts import Piefrom pyecharts import options as optsfrom pyecharts.globals import ThemeType# 创建饼形图并初始化配置项fig = Pie(init_opts=opts.InitOpts(width='1200px', height='600px', theme=ThemeType.ESSOS, bg_color='white'))# 为饼形图添加数据fig.add(series_name="States", # 系列名称data_pair=data, # 系列数据, 格式为 [(key1, value1), (key2, value2)]radius=["15%", "80%"], #内外半径center=["25%", "50%"], # 位置 1rosetype='radius', # 按圆心角区分color='auto')fig.add(series_name="States", # 系列名称data_pair=data, # 系列数据, 格式为 [(key1, value1), (key2, value2)]radius=["15%", "80%"], #内外半径center=["75%", "50%"], # 位置 2rosetype='area', # 按半径展示color='auto')# 设置全局配置项fig.set_global_opts(title_opts=opts.TitleOpts(title="两种南丁格尔图"),legend_opts=opts.LegendOpts(is_show=False), # 不显示Legend图例visualmap_opts=opts.VisualMapOpts # 视觉映射(is_show=False, # 不显示映射配置type_='size', # 映射过渡类型,选择size,不选择无法设置颜色))# 设置系列配置项fig.set_series_opts(label_opts=opts.LabelOpts(is_show=True, # 显示标签position='left', # 标签位置font_style='oblique', # 字体风格font_weight='bold', # 字体粗细font_family='Arial', # 字体系列margin=15,font_size=11) # 字体大小)# 渲染图片至html# fig.render('rosetype.html')# Jupyternotebook渲染图片fig.render_notebook()

渲染图片为png或jpeg

snapshot-selenium 是 pyecharts + selenium 渲染图片的扩展使用 selenium 需要配置 browser driver,这部分可以参考 selenium-python 相关介绍推荐使用 Chrome 浏览器,可以开启 headless 模式。目前支持 Chrome, Safari。

from pyecharts.render import make_snapshotfrom snapshot_selenium import snapshot# 使用chromemake_snapshot(engine=snapshot,file_name=fig.render('rosetype.html'),output_name='C:\Git Code\ScienceGallery\Picture\Rosetype.png',is_remove_html=True)

顺序数据

# 数据排序data.sort(key=lambda x: x[1])

from pyecharts.charts import Piefrom pyecharts import options as optsfrom pyecharts.globals import ThemeType# 创建饼形图并初始化配置项fig = Pie(init_opts=opts.InitOpts(width='1200px', height='600px', theme=ThemeType.ESSOS, bg_color='white'))# 为饼形图添加数据fig.add(series_name="States", # 系列名称data_pair=data, # 系列数据, 格式为 [(key1, value1), (key2, value2)]radius=["15%", "80%"], #内外半径center=["25%", "50%"], # 位置 1rosetype='radius', # 按圆心角区分is_clockwise=False, # 饼图的扇区是否是顺时针排布color='auto')fig.add(series_name="States", # 系列名称data_pair=data, # 系列数据, 格式为 [(key1, value1), (key2, value2)]radius=["15%", "80%"], #内外半径center=["75%", "50%"], # 位置 2rosetype='area', # 按半径展示is_clockwise=False, # 饼图的扇区是否是顺时针排布color='auto')# 设置全局配置项fig.set_global_opts(title_opts=opts.TitleOpts(title="两种南丁格尔图"),legend_opts=opts.LegendOpts(is_show=False), # 不显示Legend图例visualmap_opts=opts.VisualMapOpts # 视觉映射(is_show=False, # 不显示映射配置type_='size', # 映射过渡类型,选择size,不选择无法设置颜色))# 设置系列配置项fig.set_series_opts(label_opts=opts.LabelOpts(is_show=True, # 显示标签position='left', # 标签位置font_style='oblique', # 字体风格font_weight='bold', # 字体粗细font_family='Arial', # 字体系列margin=15,font_size=11) # 字体大小)# 渲染图片至html# fig.render('rosetype.html')# Jupyternotebook渲染图片fig.render_notebook()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。