The battle among 4 Python Data Visualization Tools
Differences among Pyecharts, Plotly, Matplotlib, and Seaborn and applying on Stock Market Data
Data visualization is crucial in analytics because it allows users or clients to view vast amounts of data while also extracting important insights that can help the business go forward.
There are various Python ways to choose from, including Matplotlib, Seaborn, and others. However, there are just a few that use a python method and include an interactive plot. We’ll use several examples in this post to explain the differences between Matplotlib, Seaborn, Plotly, and Pyecharts.
Table of Contents: [Go to the desired content by clicking the buttons below]
· Visualization Tools based on Python
· Introduction
∘ Matplotlib
∘ Plotly
∘ PyeCharts
· Battle Start
∘ Bar Chart
∘ Round 1: Matplotlib Vs Pyecharts
∘ Round 2: Seaborn vs Plotly
∘ Final Round: Plotly vs Pyecharts
∘ Dynamic Scatter Chart
∘ Liquid Level Chart
· Conclusion
· References:
Visualization Tools based on Python
In this section, we’ll go over some of the most well-known Python-based data visualization tools.
Let’s get some data ready for the upcoming visualization first. The following codes were reused from my previous postings; if you want to understand them, please have a look at Stock Market Analysis with Python, Plotly, Dash, and PowerBI.
from datetime import datetime
from datetime import timedelta
import pandas_datareader as data_reader
import pandas as pdstocktickers = ['GOOG', 'AAPL', 'FB']
stock = []#Extracting 14 days stock data
start = (datetime.now()-timedelta(days=14)).strftime('%Y-%m-%d')
end = (datetime.now()).strftime('%Y-%m-%d')for stockticker in stocktickers:
data = data_reader.DataReader(stockticker, 'yahoo', start, end)
data['stock_ticker'] = stockticker
stock.append(data)stockDF = pd.concat(stock)
Introduction
Matplotlib
Matplotlib is a 2-D charting and data visualization package written in Python. It is the most popular and commonly used charting package in the Python community. It has an interactive environment that may be used across multiple platforms. Matplotlib is a Python library that can be used in a variety of places, including Python scripts, the Python and IPython shells, Jupyter notebooks, web application servers, and more.
Plots can be embedded in applications using Tkinter, GTK+, wxPython, Qt, and other GUI toolkits. With Matplotlib, we can create plots, bar charts, pie charts, histograms, scatterplots, error charts, power spectra, stemplots, and any other type of visualization chart.
Plotly
Plotly is a free graphing library that allows you to make data visualizations. Plotly is a web-based data visualization application based on the Plotly JavaScript library plotly.py/plotly.js.
It may be used to build web-based data visualizations that can be stored as separate HTML files or displayed in Jupyter notebooks or web apps using Dash. In Plotly, you can create scatter plots, histograms, line charts, bar charts, pie charts, error bars, box plots, multiple axes, sparklines, dendrograms, 3-D charts, and other charts kinds.
PyeCharts
PyeCharts is a Python class library for creating Echarts charts. Baidu’s open-source data visualization JS framework Echarts. Pyecharts is docked with Python, making it easy to use the data generating map directly in Python. Echarts provides outstanding graphical visual effects, and Pyecharts is docked with Python, making it easy to utilize the data generation map directly in Python.
Pyecharts work in our Jupyter Notebook in a similar way to how we use visualization packages in Python. PyeCharts has a lot of customizing choices, so you can make the chart you want quickly.
Battle Start
Bar Chart
To illustrate the stock market daily volume, we’ll create a simple Multibar chart.
Round 1: Matplotlib Vs Pyecharts
Matplotlib
import matplotlib.pyplot as plt
import numpy as npdate = stockDF.index.unique().tolist()
GOOG = stockDF[stockDF['stock_ticker']=='GOOG']['Volume'].tolist()
AAPL = stockDF[stockDF['stock_ticker']=='AAPL']['Volume'].tolist()
FB = stockDF[stockDF['stock_ticker']=='FB']['Volume'].tolist()
datedata = [x.strftime("%Y-%m-%d") for x in date]barWidth = 0.25
fig = plt.subplots(figsize =(12, 8))br1 = np.arange(len(datedata))
br2 = [x + barWidth for x in br1]
br3 = [x + barWidth for x in br2]plt.bar(br1, GOOG, color ='r', width = barWidth,
edgecolor ='grey', label ='GOOG')
plt.bar(br2, AAPL, color ='g', width = barWidth,
edgecolor ='grey', label ='AAPL')
plt.bar(br3, FB, color ='b', width = barWidth,
edgecolor ='grey', label ='FB')plt.xlabel('Date', fontweight ='bold', fontsize = 15)
plt.ylabel('Volume', fontweight ='bold', fontsize = 15)
plt.xticks([r + barWidth for r in range(len(datedata))],
datedata)
plt.legend()
plt.show()
Pyecharts
from pyecharts.charts import Bar
from pyecharts import options as optsdate = stockDF.index.unique().tolist()
datedata = [x.strftime("%Y-%m-%d") for x in date]GOOG = stockDF[stockDF['stock_ticker']=='GOOG']['Volume'].tolist()
AAPL = stockDF[stockDF['stock_ticker']=='AAPL']['Volume'].tolist()
FB = stockDF[stockDF['stock_ticker']=='FB']['Volume'].tolist()bar = (Bar()
.add_xaxis(date)
.add_yaxis('GOOG', GOOG)
.add_yaxis('AAPL', AAPL)
.add_yaxis('FB', FB)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False),
markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max", name="Maximum "),
opts.MarkPointItem(type_="min", name="Minimum")]))
.set_global_opts(title_opts=opts.TitleOpts(title="Stock Market", subtitle="Daily Volume"),
datazoom_opts=opts.DataZoomOpts(range_start=0, range_end=100),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),
)
)
bar.render_notebook()
Results:
Pyecharts is clearly the winner in the first round. Pyecharts’ style is also more lovely and bright by default, whereas Matplotlib’s was significantly inferior. Furthermore, Pyechart is a user interactive graph, which means that users can interact with the chart, whereas Matplotlib is just a picture.
Pyecharts Interactive Example
The user can conceal or show the data by clicking the legend, and dragging the time range slider to show a specific period range, as seen in the figure above.
Round 2: Seaborn vs Plotly
Seaborn
import matplotlib.pyplot as plt
import seaborn as snsstockDFclone = stockDF.reset_index()
sns.barplot(x = "Date", y = "Volume", hue = "stock_ticker", data = stockDFclone)
plt.show()
Plotly
import plotly.express as pxstockDFclone['Date2'] = stockDFclone['Date'].apply(lambda x : x.strftime("%Y-%B-%d"))
fig = px.histogram(stockDFclone, x="Date2", y="Volume",
color='stock_ticker', barmode='group',histfunc=None,
height=400)
fig.update_yaxes(title_text='Volume')fig.show()
Results:
Plotly is clearly the winner of round two. Plotly is superior to Seaborn for a number of reasons. I believe you’ve seen that Seaborn is nearly identical to Matplotlib. True, it’s a graph visualization library based on matplotlib, which is a more powerful API package based on matplotlib that simplifies drawing.
Plotly was used to facilitate user interactive operation. This is the image generated by the default style, and the color matching is decent.
Plotly
Plotly, as you can see, supports tooltips, zoom in, zoom out, Box select, download as png, and data comparison on hover.
A histogram in Plotly is an aggregated bar chart with multiple aggregation functions (e.g. total, average, count…) that may be used to represent data on categorical, date, and linear axes. Keep in mind that if you want to show all columns in a Plotly multi-columns bar chart, you must change the x-axis to a string and the date format must be 2022-Jan-01; if the format is 2022–01–01, otherwise Plotly will convert to date and aggregate the value.
Final Round: Plotly vs Pyecharts
Plotly
fig = px.line(stockDFclone, x="Date", y="Volume", color='stock_ticker')
fig.show()
Pyecharts
from pyecharts.charts import Line
from pyecharts import options as optsdate = stockDF.index.unique().tolist()
GOOG = stockDF[stockDF['stock_ticker']=='GOOG']['Volume'].tolist()
AAPL = stockDF[stockDF['stock_ticker']=='AAPL']['Volume'].tolist()
FB = stockDF[stockDF['stock_ticker']=='FB']['Volume'].tolist()
datedata = [x.strftime("%Y-%m-%d") for x in date]line = (Line()
.add_xaxis(date)
.add_yaxis('GOOG', GOOG,is_smooth=True)
.add_yaxis('AAPL', AAPL,is_smooth=True)
.add_yaxis('FB', FB,is_smooth=True)
.set_series_opts(label_opts=opts.LabelOpts(is_show=True))
.set_global_opts(
datazoom_opts=[opts.DataZoomOpts(range_start=0, range_end=100)],
legend_opts=opts.LegendOpts(is_show=False),
)
)
line.render_notebook()
Results:
Because they are both user-interactive procedures. As a result, let’s make a comparison using four criteria.
- Code Volume: Plotly is easy to use and has less code, but Pyecharts is more complex to use and has a medium amount of code.
- Customization: Plotly supports slightly fewer visualization effects, whereas Pyecharts allows you more customization options like as dynamic scatter plots with animation effects, Liquid Level Chart shapes, and so on.
- Colorful: Pyecharts are more colorful and attractive, whereas Plotly is substantially less so.
- Tool-tips Customization: Pyecharts includes support for the javascript callback function and the echarts event handler, as well as other ECharts features. Inject new vigor into the project’s development. Whereas Plotly has slightly limited custom tooltip features.
As a result, Pyecharts outperform Plotly in terms of visualization. Let’s look at some more Pyecharts charts.
Dynamic Scatter Chart
As we are all familiar with scatter plots, the dynamic scatters plot is simply an extra animation with various points with distinct symbolic representations.
from pyecharts.charts import EffectScatter
from pyecharts.globals import SymbolTypec = (
EffectScatter(opts.InitOpts(width='900px', height='500px'))
.add_xaxis(datedata)
.add_yaxis("GOOG", GOOG,is_selected = True,symbol_size = 20,
symbol=SymbolType.DIAMOND)
.add_yaxis("AAPL", AAPL,is_selected = True,symbol_size = 20, symbol=SymbolType.ARROW)
.add_yaxis("FB", FB,is_selected = True,symbol_size = 20)
.set_global_opts(title_opts=opts.TitleOpts(title="Stock Market",
subtitle="Daily Volume"),
visualmap_opts=opts.VisualMapOpts(pos_left="right", type_="color",
max_=2000, min_=0, pos_bottom=50)
)
)
c.render_notebook()
Liquid Level Chart
A liquid chart is typically used to depict data in percentages. The shape of a liquid chart can be customized in Pyechart.
from pyecharts.charts import Liquid
shape='''path://M1925 12789 c-424 -41 -843 -196 -1152 -426 -311 -232 -527 -570
-588 -917 -8 -46 -30 -134 -49 -197 -223 -740 -168 -1447 164 -2110 146 -291
342 -576 651 -950 313 -378 495 -643 634 -919 l65 -128 -15 -98 c-32 -222 -39
-337 -39 -649 0 -311 5 -394 40 -650 l17 -120 -28 -55 c-82 -164 -233 -356
-464 -593 -188 -194 -263 -279 -366 -418 -388 -521 -629 -1114 -695 -1709 -21
-188 -12 -493 20 -693 81 -509 367 -1003 778 -1349 280 -235 658 -452 1032
-593 238 -90 460 -148 710 -186 193 -30 579 -33 770 -6 550 77 1070 301 1563
671 587 442 1130 1103 1481 1805 157 312 259 608 345 998 321 1450 153 3343
-446 5008 -337 937 -815 1842 -1344 2545 -742 984 -1575 1568 -2444 1714 -202
34 -452 44 -640 25z
'''
DOD = stockDF[stockDF['stock_ticker']=='GOOG']['Open'].tolist()[-2:]
DODVALUE = (DOD[1] - DOD[0]) / DOD[0]
L = Liquid()
L.add("Highest Price", [DODVALUE], is_outline_show=False, shape=shape)
L.set_global_opts(title_opts=opts.TitleOpts(title="Daily Price Changes (%)"))L.render_notebook()
Conclusion
Data visualization facilitates the human brain’s ability to absorb and extract information. The basic purpose of data visualization is to make finding patterns, trends, and outliers in massive datasets easier. During the years of the big data trend, data visualization has become increasingly significant; it may assist business stakeholders in quickly understanding their business and making data-driven decisions. As a result, selecting a suitable Visualization Tool and integrating it into your website becomes increasingly vital.
References:
Schedule a DDIChat Session in Data Science / AI / ML / DL:
Apply to be a DDIChat Expert here.
Work with DDI: https://datadriveninvestor.com/collaborate
Subscribe to DDIntel here.