Techno Blender
Digitally Yours.

Upgrade Your Data Visualisations: 4 Python Libraries to Enhance Your Matplotlib Charts | by Andy McDonald | Apr, 2023

0 222


Example line plot with gradient fill generated by the CyberPunk matplotlib theme. Image by the author.

Matplotlib is a widely used data visualisation Python library, and is often come across early in the data science and python learning journey. However, over the years, it has gained a reputation for creating plain-looking figures, and it can be awkward to use, especially for newcomers.

In my last few articles, I have shown how we can turn the basic plots that matplotlib creates into something that is more eye-catching and aesthetically pleasing to look at. This has ranged from extending the plotting code by writing several additional lines of code to just adding a few lines.

In this article, I will share with you four matplolib theme libraries that can easily take your matplotlib figures from boring to interesting. Each theme can be applied using two lines of code — an import statement and a use statement from matplotlib.

Even though these libraries are great for creating stylish plots, you need to be aware of your intended audience and different visual issues people may have, including colour blindness.

Let’s jump into it!

Before we start using the matplotlib styles, we first need to create some dummy data so that we have something to display.

This can easily be done like so:

import numpy as np

# Generate x values
x = np.linspace(0, 10, 20)

# Generate y values
y = np.sin(x)
y2 = np.cos(x)

Cyberpunk is a popular science fiction subgenre known for its dystopian, technologically advanced worlds and countercultural attitude. Often scenes are depicted in a futuristic style featuring neon lights and bright, bold colours.

Image generated by the author using Midjourney. (a bustling dystopian cyberpunk-themed futuristic city street featuring places with lots of street vendors. Shops and area surrounded with bright neon lights. Rainy and dark atmosphere. Photorealistic. — ar 3:2)

Sometimes when creating plots for a poster or infographic, you need that extra spark to draw in the reader. This is where I feel the CyberPunk styling comes into its own. However, you do have to be aware that it may not be seen as professional when creating figures for publications, and it may not be suitable for readers with colour vision problems.

We can easily apply this styling to our plots using the CyberPunk theme.

Example of a CyberPunk-themed Matplotlib figure. Image by dhaitz and from the CyberPunk Readme.

To get started with this theme, we can install it like so:

pip install mplcyberpunk

To use the CyberPunk theme, all we have to do is call upon plt.style.use() and pass in the parameter cypberpunk.

In order to make our points glow, we need to make a call to the make_scatter_glow() function. This appears to be required after each call to plt.scatter().

import mplcyberpunk

plt.style.use('cyberpunk')
plt.figure(figsize = (8,8))

plt.scatter(x, y, marker = 'o')
mplcyberpunk.make_scatter_glow()

plt.scatter(x, y2, marker = 'o', c='lime')
mplcyberpunk.make_scatter_glow()

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

plt.show()

When we run the above code, we get back the following plot.

Scatter plot after applying the CyberPunk theme. Image by the author.

We can also apply the CyberPunk theme to line plots. To make the lines glow, we can make a single call to mplcyberpunk.make_lines_glow()

plt.style.use('cyberpunk')
plt.figure(figsize = (8,8))

plt.plot(x, y, marker = 'o')
plt.plot(x, y2, marker = 'o', c='lime')

mplcyberpunk.make_lines_glow()

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

plt.show()

When run, the following plot is returned with neon-like lines.

Matplotlib chart after applying the CyberPunk theme. Image by the author.

We can take the above plot one step further and include a gradient fill between the lines and the zero point.

plt.style.use('cyberpunk')
plt.figure(figsize = (8,8))

plt.plot(x, y, marker = 'o')
plt.plot(x, y2, marker = 'o', c='lime')

mplcyberpunk.add_gradient_fill(alpha_gradientglow=0.5, gradient_start='zero')

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

plt.show()

This creates a very interesting effect like so.

Matplotlib chart after applying glowing gradient filled using the CyberPunk theme. Image by the author.

There are several other options for the CypberPunk library, and it is worth checking out its repository.

The matplotx library provides an easy way to stylise your matplotlib figures instantly. The library contains several themes that can easily be accessed and applied to any matplotlib figure.

When working with plots and figures in dark-themed Jupyter Notebooks or VSCode, having bright white figures appear can be jarring to the eye.

In order to reduce this impact, we can make the figures dark-themed. However, this does require several lines of code to get the styling correct.

Matplotx makes this process much easier, as you will see below. And as it contains multiple themes, I can easily find one that matches my VSCode theme.

Matplotx can be installed into your Python environment by opening up a terminal/command prompt and running the following command.

pip install matplotx

Once the library has been installed, we can simply use a with statement, to call upon plt.style.context and pass in matplotx.styles . From here, we can choose one of the many available themes.

In this example, I have selected the very popular Dracula theme


import matplotx

with plt.style.context(matplotx.styles.dracula):
plt.scatter(x, y, c=y2)

plt.colorbar(label='Y2')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

When run, we get back the following

Scatter plot after applying the matplotx, dracula theme. Image by the author.

There are a whole host of different styles within matplotx:

Some of the styles available from matplotx. Image captured from matplotx GitHub repository

Let’s check out our line plot with the Pitaya Smoothie theme.

As there are multiple sub-themes for this, we need to access them using square brackets. In this example, we have a dark theme, so we need to pass in dark in order to access it.

import matplotlib.pyplot as plt
import matplotx

with plt.style.context(matplotx.styles.pitaya_smoothie['dark']):
plt.plot(x, y, marker='o')
plt.plot(x, y2, marker='o')

plt.xlabel('X')
plt.ylabel('Y')
plt.show()

When we run the above code, we get back the following line plot in our chosen theme.

Matplotlib line plot after applying thePitaya Smoothie dark theme from matplotx. Image by the author

There are also numerous light themes. For example, Pitaya Smoothie has one and can be accessed like so:

with plt.style.context(matplotx.styles.pitaya_smoothie['light']):
Matplotlib line plot after applying the Pitaya Smoothie light theme from matplotx. Image by the author

If you want to see more on this library, check out my earlier article:

Or, if you want to grab the theme and try it out yourself, check out the link below:

QuantumBlack Labs is a company founded in 2012 that helps other companies use data to make better decisions. They use advanced technology like machine learning and artificial intelligence to analyse complex data sets across a range of industries like healthcare, finance, and transportation.

A few years ago, they released their own styles on GitHub.

Examples of applying the Quantum Black Labs style library to matplotlib figures. Image created by Quantum Black Labs.

To use the theme, we can install the style library like so:

pip install qbstyles

Once installed, we need to add the following line to import it.

from qbstyles import mpl_style

If you are working in Jupyter Notebooks, you will need to use separate cells between the above line and the ones below.

If we want to use the dark theme, we call upon the following:

mpl_style(dark=True)

Or, if we want to use a light theme:

mpl_style(dark=False)

We can then begin the code for creating our scatter plot

plt.scatter(x, y, c=y2)

plt.colorbar(label='Y2')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

We will get back one of the following plots, depending on whether you selected a light or a dark theme.

Matplotlib scatter plots after applying the Quantum Black Labs theme (light mode on the left, dark mode on the right). Image by the author

Let’s see how the dark theme looks on a line plot.

mpl_style(dark=True)

plt.plot(x, y, marker='o')
plt.plot(x, y2, marker='o')

plt.xlabel('X')
plt.ylabel('Y')
plt.show()

When we run the above, we get back the following plot.

Matplotlib line plot after applying the Quantum Labs Dark theme. Image by the author.

The styling of plots with this library is a little toned down from the plots generated in Matplotx, however, it does make them feel slightly more professional, especially with the light-styled plots. I wouldn’t have any hesitation in including these in a professional presentation or in training course material.

You can check out the theme in the link below:

When writing scientific journal or conference articles, creating clear, simple, and easily interpretable figures is essential. Some journals, such as Nature, require a set style, while in other cases, it is considered nice to have and prevents having stylised graphics that make it difficult for the reader to interpret.

This is where the SciencePlots library comes in.

It is used to generate graphs using common scientific journal styles, making creating figures much easier.

One thing that is great with this library is it makes the figures suitable for printing out in black and white — which is still a common practice amongst researchers. This will result in the lines being easily distinguishable from each other by changing the line styles or making sure there are different shapes on a scatter plot for categorical data.

If you want to explore more of the styles that are available within SciencePlots, I recommend checking out the Wiki Page on the library’s GitHub page:

Running the SciencePlots styling library requires LaTeX to be installed on your computer. You can find more details about LaTex and how to install here.

If running on Google Colab, you can run the following code in a cell in order to install LaTeX.

!sudo apt-get install dvipng texlive-latex-extra texlive-fonts-recommended texlive-latex-recommended cm-super
!pip install SciencePlots

Once the library and LaTeX have been set up, you can create your first scientific plot using the following code.

import scienceplots

with plt.style.context(['science', 'high-vis']):
plt.figure(figsize = (6,6))
plt.plot(x, y, marker='o', label='Line 1')
plt.plot(x, y2, marker='o', label='Line 2')

plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.show()

When we run the above code, we get the following plot, which is ideal for including in a journal publication. It is simple and easy to distinguish between the different lines.

SciencePlots theme applied to a line plot, providing high-visibility and suitable for inclusion in scientific journals. Image by the author.

Let’s see what the IEEE style is like. We can create one by modifying the following line:

with plt.style.context(['science', 'ieee']):

It’s a little different to the one above, and the colours have changed, but still a very good-looking scientific journal plot.

SciencePlots theme applied to a line plot using IEEE styling. Image by the author.

Over the years, I have authored numerous technical papers for conferences and journals. These papers have been heavy on the charts, and these have often been created in Excel or with several lines of Python plotting code. This can be time-consuming, especially in getting the sizing, colours and line styles correct.

Going forward, for any of my future papers, I will be looking at the SciencePlots theme as a starting point.

If you are interested in giving it a go, you can grab the SciencePlots theme from the link below:

Within this article, we have seen four very useful matplotlib python libraries that can take your boring matplotlib figure to the next level. Each of these libraries provides unique styling to your plots.

Even though these styles provide a nice creative visualisation, you do need to consider your audience, especially those with colour blindness and other eye-related problems.

Remember, the whole point of creating awesome data visuals is to make your info easy to understand and share your story in the best way possible.

But, sometimes, having some fun with data and going all out with a CyberPunk theme is nice.


Example line plot with gradient fill generated by the CyberPunk matplotlib theme. Image by the author.

Matplotlib is a widely used data visualisation Python library, and is often come across early in the data science and python learning journey. However, over the years, it has gained a reputation for creating plain-looking figures, and it can be awkward to use, especially for newcomers.

In my last few articles, I have shown how we can turn the basic plots that matplotlib creates into something that is more eye-catching and aesthetically pleasing to look at. This has ranged from extending the plotting code by writing several additional lines of code to just adding a few lines.

In this article, I will share with you four matplolib theme libraries that can easily take your matplotlib figures from boring to interesting. Each theme can be applied using two lines of code — an import statement and a use statement from matplotlib.

Even though these libraries are great for creating stylish plots, you need to be aware of your intended audience and different visual issues people may have, including colour blindness.

Let’s jump into it!

Before we start using the matplotlib styles, we first need to create some dummy data so that we have something to display.

This can easily be done like so:

import numpy as np

# Generate x values
x = np.linspace(0, 10, 20)

# Generate y values
y = np.sin(x)
y2 = np.cos(x)

Cyberpunk is a popular science fiction subgenre known for its dystopian, technologically advanced worlds and countercultural attitude. Often scenes are depicted in a futuristic style featuring neon lights and bright, bold colours.

Image generated by the author using Midjourney. (a bustling dystopian cyberpunk-themed futuristic city street featuring places with lots of street vendors. Shops and area surrounded with bright neon lights. Rainy and dark atmosphere. Photorealistic. — ar 3:2)

Sometimes when creating plots for a poster or infographic, you need that extra spark to draw in the reader. This is where I feel the CyberPunk styling comes into its own. However, you do have to be aware that it may not be seen as professional when creating figures for publications, and it may not be suitable for readers with colour vision problems.

We can easily apply this styling to our plots using the CyberPunk theme.

Example of a CyberPunk-themed Matplotlib figure. Image by dhaitz and from the CyberPunk Readme.

To get started with this theme, we can install it like so:

pip install mplcyberpunk

To use the CyberPunk theme, all we have to do is call upon plt.style.use() and pass in the parameter cypberpunk.

In order to make our points glow, we need to make a call to the make_scatter_glow() function. This appears to be required after each call to plt.scatter().

import mplcyberpunk

plt.style.use('cyberpunk')
plt.figure(figsize = (8,8))

plt.scatter(x, y, marker = 'o')
mplcyberpunk.make_scatter_glow()

plt.scatter(x, y2, marker = 'o', c='lime')
mplcyberpunk.make_scatter_glow()

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

plt.show()

When we run the above code, we get back the following plot.

Scatter plot after applying the CyberPunk theme. Image by the author.

We can also apply the CyberPunk theme to line plots. To make the lines glow, we can make a single call to mplcyberpunk.make_lines_glow()

plt.style.use('cyberpunk')
plt.figure(figsize = (8,8))

plt.plot(x, y, marker = 'o')
plt.plot(x, y2, marker = 'o', c='lime')

mplcyberpunk.make_lines_glow()

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

plt.show()

When run, the following plot is returned with neon-like lines.

Matplotlib chart after applying the CyberPunk theme. Image by the author.

We can take the above plot one step further and include a gradient fill between the lines and the zero point.

plt.style.use('cyberpunk')
plt.figure(figsize = (8,8))

plt.plot(x, y, marker = 'o')
plt.plot(x, y2, marker = 'o', c='lime')

mplcyberpunk.add_gradient_fill(alpha_gradientglow=0.5, gradient_start='zero')

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

plt.show()

This creates a very interesting effect like so.

Matplotlib chart after applying glowing gradient filled using the CyberPunk theme. Image by the author.

There are several other options for the CypberPunk library, and it is worth checking out its repository.

The matplotx library provides an easy way to stylise your matplotlib figures instantly. The library contains several themes that can easily be accessed and applied to any matplotlib figure.

When working with plots and figures in dark-themed Jupyter Notebooks or VSCode, having bright white figures appear can be jarring to the eye.

In order to reduce this impact, we can make the figures dark-themed. However, this does require several lines of code to get the styling correct.

Matplotx makes this process much easier, as you will see below. And as it contains multiple themes, I can easily find one that matches my VSCode theme.

Matplotx can be installed into your Python environment by opening up a terminal/command prompt and running the following command.

pip install matplotx

Once the library has been installed, we can simply use a with statement, to call upon plt.style.context and pass in matplotx.styles . From here, we can choose one of the many available themes.

In this example, I have selected the very popular Dracula theme


import matplotx

with plt.style.context(matplotx.styles.dracula):
plt.scatter(x, y, c=y2)

plt.colorbar(label='Y2')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

When run, we get back the following

Scatter plot after applying the matplotx, dracula theme. Image by the author.

There are a whole host of different styles within matplotx:

Some of the styles available from matplotx. Image captured from matplotx GitHub repository

Let’s check out our line plot with the Pitaya Smoothie theme.

As there are multiple sub-themes for this, we need to access them using square brackets. In this example, we have a dark theme, so we need to pass in dark in order to access it.

import matplotlib.pyplot as plt
import matplotx

with plt.style.context(matplotx.styles.pitaya_smoothie['dark']):
plt.plot(x, y, marker='o')
plt.plot(x, y2, marker='o')

plt.xlabel('X')
plt.ylabel('Y')
plt.show()

When we run the above code, we get back the following line plot in our chosen theme.

Matplotlib line plot after applying thePitaya Smoothie dark theme from matplotx. Image by the author

There are also numerous light themes. For example, Pitaya Smoothie has one and can be accessed like so:

with plt.style.context(matplotx.styles.pitaya_smoothie['light']):
Matplotlib line plot after applying the Pitaya Smoothie light theme from matplotx. Image by the author

If you want to see more on this library, check out my earlier article:

Or, if you want to grab the theme and try it out yourself, check out the link below:

QuantumBlack Labs is a company founded in 2012 that helps other companies use data to make better decisions. They use advanced technology like machine learning and artificial intelligence to analyse complex data sets across a range of industries like healthcare, finance, and transportation.

A few years ago, they released their own styles on GitHub.

Examples of applying the Quantum Black Labs style library to matplotlib figures. Image created by Quantum Black Labs.

To use the theme, we can install the style library like so:

pip install qbstyles

Once installed, we need to add the following line to import it.

from qbstyles import mpl_style

If you are working in Jupyter Notebooks, you will need to use separate cells between the above line and the ones below.

If we want to use the dark theme, we call upon the following:

mpl_style(dark=True)

Or, if we want to use a light theme:

mpl_style(dark=False)

We can then begin the code for creating our scatter plot

plt.scatter(x, y, c=y2)

plt.colorbar(label='Y2')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

We will get back one of the following plots, depending on whether you selected a light or a dark theme.

Matplotlib scatter plots after applying the Quantum Black Labs theme (light mode on the left, dark mode on the right). Image by the author

Let’s see how the dark theme looks on a line plot.

mpl_style(dark=True)

plt.plot(x, y, marker='o')
plt.plot(x, y2, marker='o')

plt.xlabel('X')
plt.ylabel('Y')
plt.show()

When we run the above, we get back the following plot.

Matplotlib line plot after applying the Quantum Labs Dark theme. Image by the author.

The styling of plots with this library is a little toned down from the plots generated in Matplotx, however, it does make them feel slightly more professional, especially with the light-styled plots. I wouldn’t have any hesitation in including these in a professional presentation or in training course material.

You can check out the theme in the link below:

When writing scientific journal or conference articles, creating clear, simple, and easily interpretable figures is essential. Some journals, such as Nature, require a set style, while in other cases, it is considered nice to have and prevents having stylised graphics that make it difficult for the reader to interpret.

This is where the SciencePlots library comes in.

It is used to generate graphs using common scientific journal styles, making creating figures much easier.

One thing that is great with this library is it makes the figures suitable for printing out in black and white — which is still a common practice amongst researchers. This will result in the lines being easily distinguishable from each other by changing the line styles or making sure there are different shapes on a scatter plot for categorical data.

If you want to explore more of the styles that are available within SciencePlots, I recommend checking out the Wiki Page on the library’s GitHub page:

Running the SciencePlots styling library requires LaTeX to be installed on your computer. You can find more details about LaTex and how to install here.

If running on Google Colab, you can run the following code in a cell in order to install LaTeX.

!sudo apt-get install dvipng texlive-latex-extra texlive-fonts-recommended texlive-latex-recommended cm-super
!pip install SciencePlots

Once the library and LaTeX have been set up, you can create your first scientific plot using the following code.

import scienceplots

with plt.style.context(['science', 'high-vis']):
plt.figure(figsize = (6,6))
plt.plot(x, y, marker='o', label='Line 1')
plt.plot(x, y2, marker='o', label='Line 2')

plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.show()

When we run the above code, we get the following plot, which is ideal for including in a journal publication. It is simple and easy to distinguish between the different lines.

SciencePlots theme applied to a line plot, providing high-visibility and suitable for inclusion in scientific journals. Image by the author.

Let’s see what the IEEE style is like. We can create one by modifying the following line:

with plt.style.context(['science', 'ieee']):

It’s a little different to the one above, and the colours have changed, but still a very good-looking scientific journal plot.

SciencePlots theme applied to a line plot using IEEE styling. Image by the author.

Over the years, I have authored numerous technical papers for conferences and journals. These papers have been heavy on the charts, and these have often been created in Excel or with several lines of Python plotting code. This can be time-consuming, especially in getting the sizing, colours and line styles correct.

Going forward, for any of my future papers, I will be looking at the SciencePlots theme as a starting point.

If you are interested in giving it a go, you can grab the SciencePlots theme from the link below:

Within this article, we have seen four very useful matplotlib python libraries that can take your boring matplotlib figure to the next level. Each of these libraries provides unique styling to your plots.

Even though these styles provide a nice creative visualisation, you do need to consider your audience, especially those with colour blindness and other eye-related problems.

Remember, the whole point of creating awesome data visuals is to make your info easy to understand and share your story in the best way possible.

But, sometimes, having some fun with data and going all out with a CyberPunk theme is nice.

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment