Python的散点图竟然能画这么好看
你以为的散点图长这样:
其实散点图还可以长这样:
气泡图
大雁南飞图
看起来是不是即高大上又美观,下面就带着大家一起学习一下如何用pyecharts画出漂亮的散点图
一、最基本的散点图
from pyecharts import options as opts from pyecharts.charts import Scatter x=['一月','二月','三月','四月','五月','六月','七月','八月','九月'] y=[8,5,3,4,8,2,2,5,1] c = ( Scatter() .add_xaxis(x) .add_yaxis("", y) .set_global_opts(title_opts=opts.TitleOpts(title="base") ))c.render_notebook()
这就是最基本的散点图,只需输入横、纵两个列表即可输出图形
二、显示分割线的散点图
from pyecharts import options as opts from pyecharts.charts import Scatter x=['一月','二月','三月','四月','五月','六月','七月','八月','九月'] y=[8,5,3,4,8,2,2,5,1] c = ( Scatter() .add_xaxis(x) .add_yaxis("", y) .set_global_opts( title_opts=opts.TitleOpts(title="base"), xaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)), yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)) ))c.render_notebook()
可以根据自己的需要设置横纵分割线
三、设置多维度数据
from pyecharts import options as opts from pyecharts.charts import Scatter from pyecharts.commons.utils import JsCode x=['一月','二月','三月','四月','五月','六月','七月','八月','九月'] y=[8,5,3,4,8,2,2,5,1] c = ( Scatter() .add_xaxis(x) .add_yaxis("", y, label_opts=opts.LabelOpts( formatter=JsCode( "function(params){return params.value[1] +' : '+ params.name;}" ) ),) .set_global_opts( title_opts=opts.TitleOpts(title="Scatter-多维度数据"), visualmap_opts=opts.VisualMapOpts( type_="color", max_=8, dimension=1 ), ))c.render_notebook()
主要是通过插入js代码的方式来实现
四、气泡图
把点状图分段,设置不同的颜色,并设置点的半径随着数据的变化而变化,散点图就变成了气泡图
from pyecharts import options as opts from pyecharts.charts import Scatter x=['一月','二月','三月','四月','五月','六月','七月','八月','九月'] y=[8,5,3,4,8,2,2,5,1] c = ( Scatter() .add_xaxis(x) .add_yaxis("", y) .set_global_opts( title_opts=opts.TitleOpts(title="气泡图"), visualmap_opts=opts.VisualMapOpts( range_opacity=0.45, type_="size", max_=10, is_piecewise=True, dimension=0, pieces=[ {"lte": 2, "color": "green"}, {"gt": 2, "lte": 4, "color": "red"}, {"gt": 4, "lte": 6, "color": "yellow"}, {"gt": 6, "lte": 8, "color": "red"}, {"gt": 8, "color": "green"}, ], pos_right=0, pos_bottom=100) ))c.render_notebook()
参数介绍:
- range_opacity:设置透明度type:size,用形状大小代表数据大小
- is_piecewise:是否分段pieces:具体分段范围
五、设置散点的形状、大小和颜色
通过改变散点的形状、大小和颜色,可以把散点图画的像大雁南飞一样
from pyecharts import options as opts from pyecharts.charts import Scatter x=['一月','二月','三月','四月','五月','六月','七月','八月','九月'] y=[8,5,3,4,8,2,2,5,1] c = ( Scatter() .add_xaxis(x) .add_yaxis(series_name="", y_axis=y, symbol='arrow', symbol_size=[40,20], symbol_rotate=-45) .set_global_opts( title_opts=opts.TitleOpts(title="base"), visualmap_opts=opts.VisualMapOpts(max_=10), ))c.render_notebook()关键参数:symbol:设置形状symbol_size:设置形状大小ymbol_rotate:设置形状角度
相关推荐
chaigang 2020-06-05
jiahaohappy 2020-05-11
sxyhetao 2020-05-01
georgeandgeorge 2020-03-28
zhangxiaojiakele 2020-02-24
大史哥哥 2020-02-23
Norsaa 2019-12-29
maybeyoucan 2019-11-04
CloudXli 2019-10-29
星辰大海的路上 2019-06-18
znancy 2019-04-02
Cyuhong 2019-03-19
diyanpython 2019-03-19
牧灵泉的崖巢 2019-05-09
sesiria 2019-04-11
飞天喜欢你 2017-06-30
石山下 2016-03-21
paopao00 2019-06-27
crbrave 2019-06-16