Daily Dose of Data Science – Day 5 – Attractive data visualizations in Python using Matplotlib styles and Plotly

Daily Dose of Data Science – Day 5 – Attractive data visualizations in Python using Matplotlib styles and Plotly

Its time to take your Daily Dose of Data Science! Today it will be a very light dose on an important topic of attractive data visualization in Python using Matplotlib styles and Plotly. Let’s dive in to know more!

Data visualization, story telling and reporting are important skills for any data scientist. It is an art which makes the scientific process more effective, explainable and attractive! Most data scientists are well aware of the two important frameworks of Matplotlib and Seaborn in Python for effective data visualization. But today, I will share few tips to make the matplotlib visualizations more attractive by using custom styling and then I will discuss about the Plotly framework for interactive plots in python like powerful reporting platforms like PowerBi and Tableau.

Matplotlib Styling

Default visualization template from matplotlib can actually be customized using style sheets and rcParams. The style package adds support for easy-to-switch plotting “styles” with the same parameters as a matplotlib rc file. You can either use the pre-defined styles provided by Matplotlib. For example, the pre-defined style called “ggplot”, which emulates the aesthetics of ggplot (a popular plotting package for R). The example usage is given as follows:

import numpy as np
import matplotlib.pyplot as plt

plt.style.use('ggplot')

Take a look at the other default styling options available in Matplotlib from the reference style sheet.

Axes title
Axes title
Axes title
Axes title

You can also go for your custom styling, by defining your own style function. Use them by calling style.use with the path or URL to the style sheet. For example, you might want to create ./images/presentation.mplstyle with the following:

axes.titlesize : 24
axes.labelsize : 20
lines.linewidth : 3
lines.markersize : 10
xtick.labelsize : 16
ytick.labelsize : 16

For using your custom style just use the following code statements:

import matplotlib.pyplot as plt
plt.style.use('./images/presentation.mplstyle')

Please take a look at the original matplotlib documentations to know more in details: https://matplotlib.org/stable/tutorials/introductory/customizing.html.

Use Plotly for interactive data visualization

Plotly in python is an MIT licensed data visualization framework that allows interactive data visualization, with powerful tooltips. Explore the original plotly github reporsitory to find out more about this project.

Installing Plotly is very simple like other python packages and can be done using the simple pip installer.

pip install plotly

Using plotly is also very simple. Please take a look at this tutorial document from plotly to get step by step execution details. Use the following import statements to import the offline plotly libraries in your python code:

# work with plotly
import plotly.offline as py 
py.init_notebook_mode(connected=True) # this code, allow us to work with offline plotly version
import plotly.graph_objs as go # it's like "plt" of matplot
import plotly.tools as tls # It's useful to we get some tools of plotly

Then on your own data, use plotly to have interactive visualizations in the following way:

fig = go.Figure(data=data, layout=layout)

py.iplot(fig, filename='your_interactive_plot')
Jupyter Notebook Tutorial | Python | Plotly

Plotly is a very powerful data visualization framework, which goes hand in hand with Jupyter notebooks and very effective for interactive reporting!

That’s all for today! Keep following https://aditya-bhattacharya.net/ for another daily dose of data science and please feel free to like, share, comment and subscribe to my posts if you find it helpful!

Tags: , , , ,

Leave a Reply

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