chaigang 2020-09-10
你以为的散点图长这样:
其实散点图还可以长这样:
看起来是不是即高大上又美观,下面就带着大家一起学习一下如何用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()
参数介绍:
五、设置散点的形状、大小和颜色
通过改变散点的形状、大小和颜色,可以把散点图画的像大雁南飞一样
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:设置形状角度