Kashish Rastogi — December 13, 2021
Beginner Data Visualization Libraries Python
This article was published as a part of the Data Science Blogathon
PyeChart Data

Introduction

If you want to make charts like the above one so let’s get to know about the amazing Pyecharts library. Generally, the bar chart which we use of matplotlib library is static and there are very few parameters to tweak to make it more appealing to the audience but if I say with less line of code we can create eye-pleasing charts. Yes!!!!!! so let’s see how it is done.

The Pyecharts library uses Echarts to generate charts. Pyecharts library provides the interface between Python and Echarts. The Pyecharts work usually like we use visualization library in Python or R in our Jupyter Notebook. Pyecharts have flexible configuration options, so you can easily match the desired chart you want to make. Detailed documentation and samples to help developers get started the faster project

This Pyecharts library supports the chained calls and we can easily integrate them into Flask, Django Web framework, and other mainstream. Let’s look at some examples, and many more can be found at Pyecharts Gallery!

 

Installing

Installing the Pyechart library, with the pip command

pip install pyecharts==1.7.1

Importing Library

from pyecharts import options as opts
from pyecharts.charts import Liquid
from pyecharts.globals import SymbolType
from pyecharts.charts import Bar, Line

Parameters for the chart

How to create a pyechart chart? Let’s see the parameters to make an interactive chart.

  • For showing data on the x-axis: add_xaxis()
  • For showing data on the y-axis: add_yaxis()
  • set_global_ops gives a lot of room where we can have the title and subtitle to the chart.
  • render() create a file named render.html in the root directory defaultly, which supports path parameter and set the location the file save in, for instance, render(r”e:my_first_chart.html”), open file with your browser.
  • In order to have toolbox buttons on the chart, you just need to give a bit more configuration. Then you can click the image download button.
  • init_opts helps to set the various theme palate.

Benefits of PyeChart

  • The pyechart data is a dynamic chart and whenever the mouse is hovering on the chart, the number shows up with the transparent background. The charts are very eye-catching. Less code is needed to make pleasant charts.
  • The library does provide 3D visuals which are great
  • Support for javascript and python.
  • Creative theme palate

Color palate example. Let’s first see the dark mode palate for the bar chart.

from pyecharts.charts import Bar
bar = (
 Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK))
 .add_xaxis(['JAN','FEB','MAR','APR'])
 .add_yaxis('Temperature Max', [-7,-6,10,15])
 .set_global_opts(title_opts=opts.TitleOpts(title="Temperrature throught out the year", subtitle="2021"))
                )
bar.render_notebook()
Temperature throughout the year

We saw the common dark mode theme let’s check some other interesting themes also. Well, I do prefer this purple palate! I have never seen such a good purple check box palate theme for the chart.

from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType
bar = (
 Bar(init_opts=opts.InitOpts(theme=ThemeType.PURPLE_PASSION))
   .add_xaxis(['JAN','FEB','MAR','APR'])
   .add_yaxis('Temperature Max', [7,6,15,10])
  .set_global_opts(title_opts=opts.TitleOpts(title="Temperature of India",
                                              subtitle="2021 Year"))
)
bar.render_notebook()
Bar Graph representation

There are some other palates to try them out I am listing them WHITE, LIGHT, DARK, CHALK, ESSOS, INFOGRAPHIC, MACARONS, ROMA, ROMANTIC, SHINE, VINTAGE, WALDEN

Bar chart

Let’s try out the pyechart library by starting with the bar chart. Here, I have taken demo data of the Temperature of India for 4 months Jan, Feb, Mar, Apr.

from pyecharts.charts import Bar
bar = (
Bar()
.add_xaxis([‘JAN’,’FEB’,’MAR’,’APR’])
.add_yaxis(‘Temperature Max’, [-7,-6,10,15])
.set_global_opts(title_opts=opts.TitleOpts(title=”Temperature of India”,
subtitle=”2021 Year”))
)
bar.render_notebook()
PyeChart Data in Bar Graph representation

This bar chart shows the one parameter Temp Max but if you want another parameter too like temp min then it is simple just add another y-axis. Let’s see how it is done.

bar = (
Bar(init_opts=opts.InitOpts())
.add_xaxis([‘JAN’,’FEB’,’MAR’,’APR’])
.add_yaxis(‘Temperature Max’, [-7,-6,10,15])
.add_yaxis(‘Temperature Min’, [-1,0,5,12])
.set_global_opts(title_opts=opts.TitleOpts(title=”Temperature of India”,
subtitle=”2021 Year”))
)
bar.render_notebook()

 

Temperature in Bar Graph

We saw how to make a simple bar chart let’s add some specific details so when our audience sees the charts they don’t fail to miss out on the details.

from pyecharts.charts import Bar
bar = (
 Bar(init_opts=opts.InitOpts())
 .add_xaxis(['JAN','FEB','MAR','APR'])
 .add_yaxis('Temperature Max', [-7,-6,10,15])
 .add_yaxis('Temperature Min', [-1,0,5,12])
 .set_series_opts(
        label_opts=opts.LabelOpts(is_show=False),
        markpoint_opts=opts.MarkPointOpts(
        data=[opts.MarkPointItem(type_="max", name="Highest Temp"),
              opts.MarkPointItem(type_="min", name="Lowest Temp")]
															),
           )
   )
bar.render_notebook()
Maximum temperature depiction in bar graph

 

If we want to download the charts we just need to add ToolboxOpts to the chart and it will show a download option on the chart.

(Bar()
.add_xaxis([‘JAN’,’FEB’,’MAR’,’APR’])
.add_yaxis(‘Temperature Max’, [7,6,15,10])
.set_global_opts(title_opts=opts.TitleOpts(title=”Temperature of India”,
subtitle=”2021 Year”),
toolbox_opts=opts.ToolboxOpts())
.render_notebook()
)

 

Bar Chart | Temperature of India

Want to see a unique way to represent the value in percentage pyecharts data that have liquid charts. In liquid charts, we need to give the value which we want to see on the chart in decimal format. This chart is a unique chart that will not only show the value but also the background has water which is running all the time. I haven’t seen this type of chart till now.

To make this type of chart just run the below line of code

c = (
    Liquid()
    .add("Completion", [0.67])
    .set_global_opts(title_opts=opts.TitleOpts(pos_left="center"))
)
c.render_notebook()
67% of showing PyeChart

We can modify the chart by color and outline parameter. like if we want to choose any other color than the default blue. We just need to give a color name in the color parameter and for the outline, if we want to hide it then just need to make is_outline_show=False

c = (
Liquid()
.add(‘Completion’, [0.6], is_outline_show=False)
.set_global_opts(title_opts=opts.TitleOpts(title=’% of sales target achieved’,
pos_left=’center’))
)
c.render_notebook()

 

% of sales achieved

For changing the color of the circle just give a color name to the color parameter.

c = (
Liquid()
.add(‘Completion’, [0.6], is_outline_show=False,color=[‘#7733FF’])
.set_global_opts(title_opts=opts.TitleOpts(title=’% of sales target achieved’,
pos_left=’center’))
)
c.render_notebook()

 

PyeChart of Sales Target

Scatter Chart

The scatter chart which we are going to see is not a normal bubble chart the scatter chart will have a ripple effect. The scatter chart with ripple effects of each dot and also have a slide bar from where we can slide to filter the data. Isn’t that amazing?

from pyecharts.charts import EffectScatter
from pyecharts.globals import SymbolType
from pyecharts import options as opts
v1 = [24,50,105,205,389,673,1073,1578]
x = [“2013″,”2014″,”2015″,”2016″,”2017″,”2018″,”2019″,”2020”]
c = (
EffectScatter()
.add_xaxis(x)
.add_yaxis(“Temp”, v1,is_selected = True,symbol_size = 20,
symbol=SymbolType.DIAMOND)
.set_global_opts(title_opts=opts.TitleOpts(title=”Temperature of India”,
subtitle=”2013-2020 Year”))
)
c.render_notebook()

Graph of temperature | PyeChart Data

We saw how to make a scatter plot with a ripple effect let’s add two y-axes and compare the data.

v1 = [24,50,105,205,389,673,1073,1578]
v2 = [3,5,8,12,16,23,40,56,72]
x = [“2013″,”2014″,”2015″,”2016″,”2017″,”2018″,”2019″,”2020”]
c = (
EffectScatter()
.add_xaxis(x)
.add_yaxis(“Temp1”, v1, is_selected = True,symbol_size = 20,
symbol=SymbolType.DIAMOND)
.add_yaxis(“Temp2”, v2, is_selected = True,symbol_size = 20,
symbol=SymbolType.DIAMOND)
.set_global_opts(title_opts=opts.TitleOpts(title=”Temperature of India”,
subtitle=”2013-2020 Year”))
)
c.render_notebook()

Temperature of India 2 | PyeChart Data 2

We saw how to have 2 data points for comparison let’s see how we can have a sidebar that will tell us where the data point lies compared to others.

c = (
 EffectScatter(opts.InitOpts(width='900px', height='500px'))
 .add_xaxis(x)
 .add_yaxis("Temp1", v1,is_selected = True,symbol_size = 20, 
            symbol=SymbolType.DIAMOND)
 .add_yaxis("Temp2", v2,is_selected = True,symbol_size = 20)
 .set_global_opts(title_opts=opts.TitleOpts(title="Temperature of India", 
                                            subtitle="2013-2020 Year"),
 visualmap_opts=opts.VisualMapOpts(pos_left="right", type_="color",  
                                   max_=2000, min_=0, pos_bottom=50) 
 )
 )
c.render_notebook()
Temperature of India | PyeChart Data

End Note

Here we saw what is Pyechart Data and what are the benefits of pyecharts. As there are dynamic which helps the audience to engage with them with additional functionality that it is in scatter chart we have a ripple effect.

Author’s Contact

You can contact me through the below-mentioned social media platforms.

LinkedIn | Kaggle | Medium | Analytics Vidhya

The media shown in this article is not owned by Analytics Vidhya and are used at the Author’s discretion. 

About the Author

Kashish Rastogi

A student who is learning and sharing with a storyteller to make your life easy.

Our Top Authors

Download Analytics Vidhya App for the Latest blog/Article

Leave a Reply Your email address will not be published. Required fields are marked *