Make a beautiful scatterplot in a few lines in Python to make your report outstanding | by Di(Candice) Han | Towards Data Science

Everyone knows what scatter plots look like — several dots scattered in a plane having horizontal and vertical axes. Scatter plots are useful data visualization tools for illustrating a trend.
The scatter plot I am going to introduce in this tutorial is more attractive than the normal one. It is dynamic with ripple effects of each dot and a slide bar which you can slide to filter the data. Below chart is what we are going to make. To create this chart, you just need a few lines of Python codes.
Before you write these few lines of codes, you need to install the visualization package called Pyechart. It is a Python echarts plotting library. It provides more than 30 kinds of charts. The latest version is 1.7.1 and it was released on March 12th 2020. You can pip install it.
pip install pyecharts==1.7.1
After the library is installed, we can start to build the beautiful bar chart:
#import librariesfrom pyecharts.charts import Barfrom pyecharts import options as opts#create dataset for the chart, v1 is the revenue of Shopify in million dollars, x is the yearv1 = [24,50,105,205,389,673,1073,1578]x = [“2012”, “2013”, “2014”, “2015”, “2016”, “2017”,”2018",”2019"]from pyecharts.charts import EffectScatterfrom pyecharts.globals import SymbolTypefrom pyecharts import options as optsc = ( EffectScatter() .add_xaxis(x) .add_yaxis(“Shopify”, v1,is_selected = True,symbol_size = 20, symbol=SymbolType.DIAMOND) )c.render_notebook()
With just three lines of codes you will get a chart like this,
You can add another series of data to compare the revenue performance of two companies. I am going to add another e-commerce company- Alibaba.
v1 = [24,50,105,205,389,673,1073,1578] @Shopify revenuev2 = [3,5,8,12,16,23,40,56,72]. #Alibaba revenuex = [“2012”, “2013”, “2014”, “2015”, “2016”, “2017”,”2018",”2019"]from pyecharts.charts import EffectScatterfrom pyecharts.globals import SymbolTypefrom pyecharts import options as optsc = ( EffectScatter() .add_xaxis(x) .add_yaxis(“Shopify”, v1,is_selected = True,symbol_size = 20, symbol=SymbolType.DIAMOND) .add_yaxis(“Alibaba”, v2,is_selected = True,symbol_size = 20) )c.render_notebook()
Pyechart libary provides many kinds of shapes, such as ‘circle’, ‘rect’, ‘roundRect’, ‘triangle’,’diamond’, ‘pin’, ‘arrow’, and ‘none’. You need to use uppercase to pass those values to the parameter, for example, SymbolType.DIAMOND.
Now let us add the title and subtitle to complete the chart.
v1 = [24,50,105,205,389,673,1073,1578]v2 = [3,5,8,12,16,23,40,56,72]x = [“2012”, “2013”, “2014”, “2015”, “2016”, “2017”,”2018",”2019"]from pyecharts.charts import EffectScatterfrom pyecharts.globals import SymbolTypefrom pyecharts import options as optsc = ( EffectScatter(opts.InitOpts(width = ‘900px’,height = ‘500px’)) .add_xaxis(x) .add_yaxis(“Shopify”, v1,is_selected = True,symbol_size = 20, symbol=SymbolType.DIAMOND) .add_yaxis(“Alibaba”, v2,is_selected = True,symbol_size = 20) .set_global_opts( title_opts=opts.TitleOpts(title=”Revenue ($M) between year 2012 and 2019", subtitle=”Shopify vs Alibaba”,pos_left=”left”), ) )c.render_notebook()
We can further beautify the chart and make the chart more interactive. We will add a slide bar.
To add the slide bar, we need to add the opts.VisualMapopts function inside the global option settings.
v1 = [24,50,105,205,389,673,1073,1578]v2 = [3,5,8,12,16,23,40,56,72]x = [“2012”, “2013”, “2014”, “2015”, “2016”, “2017”,”2018",”2019"]from pyecharts.charts import EffectScatterfrom pyecharts.globals import SymbolTypefrom pyecharts import options as optsc = ( EffectScatter(opts.InitOpts(width = ‘900px’,height = ‘500px’)) .add_xaxis(x) .add_yaxis(“Shopify”, v1,is_selected = True,symbol_size = 20, symbol=SymbolType.DIAMOND) .add_yaxis(“Alibaba”, v2,is_selected = True,symbol_size = 20) .set_global_opts( title_opts=opts.TitleOpts(title=”Revenue between year 2012 and 2019", subtitle=”Shopify vs Alibaba”,pos_left=”left”), visualmap_opts=opts.VisualMapOpts(pos_left=”right”,type_=”color”,  max_=2000, min_=0, pos_bottom=50)  ) )c.render_notebook()
pos_left is to define the location of the sliding bar.
type is to define the type of sliding bar, either ‘color’ or ‘size’.
max_ and min_ to limit the maximum and minimum values of the sliding bar.
pos_bottom is used to define the distance to the bottom.
Pyechart provides options to beautify labels and ripple effects.
With label options, you can define the position, font, size, weight, color, alignment, etc.
With effect options, you can change the brush type, color, symbol, symbol size etc to get the animation effect you would expect.
I added some parameters to the label and effect options as below.
v1 = [24,50,105,205,389,673,1073,1578]v2 = [3,5,8,12,16,23,40,56,72]x = [“2012”, “2013”, “2014”, “2015”, “2016”, “2017”,”2018",”2019"]from pyecharts.charts import EffectScatterfrom pyecharts.globals import SymbolTypefrom pyecharts import options as opts c = ( EffectScatter() .add_xaxis(x) .add_yaxis(“Shopify”, v1,is_selected = True,symbol_size = 15, symbol=SymbolType.DIAMOND) .add_yaxis(“Alibaba”, v2,is_selected = True,symbol_size = 15) .set_global_opts( title_opts=opts.TitleOpts(title=”Revenue between year 2012 and 2019", subtitle=”Shopify vs Alibaba”,pos_left=”left”),  visualmap_opts=opts.VisualMapOpts(pos_left=”right”,type_=”color”, max_=2000, min_=0,pos_bottom=50)) .set_series_opts( label_opts=opts.LabelOpts(is_show=True, #whether the label will be shown position=’right’, #location of the label font_style=’italic’), #font of the label effect_opts = opts.EffectOpts(scale = 3, #scale of ripples period=5, #period of the animation brush_type=’fill’), #either ‘stroke’ or ‘fill’ ))c.render_notebook()
You will have an even more eye-catching chart.
There are numerous options and parameters you can explore and try out to beautify your charts even more.
If you are interested in making other types of beautiful charts, you may want to check out my other posts.
Make beautiful Nightinggale rose chart in python-visualize covid19 death rateMake a beautiful water polo chart in a few lines in PythonMake a beautiful bar chart in just few lines in PythonAnimated bubble chart with Plotly in Python — inspired by professor Hans RoslingMake the cutest chart in Python — visualize your data with hand-drawn chartsDraw a unique barplot using Matplotlib in Python
Make beautiful Nightinggale rose chart in python-visualize covid19 death rate
Make a beautiful water polo chart in a few lines in Python
Make a beautiful bar chart in just few lines in Python
Animated bubble chart with Plotly in Python — inspired by professor Hans Rosling
Make the cutest chart in Python — visualize your data with hand-drawn charts
Draw a unique barplot using Matplotlib in Python