4 Python Libraries to Beautify Your Matplotlib Charts!

66472dbcb98a35ac10e9f5c3c1b55f29.gif

Hello everyone, I am Little F~

Matplotlib is a widely used Python data visualization library, and I believe many people have used it.

But sometimes I always feel that the charts made by Matplotlib are not very good-looking and unsightly. 

Today, Xiao F will share with you four Python libraries for beautifying Matplotlib charts, which can easily make your Matplotlib charts look better~

0f12d08228dc17f3f8a182dea9dd41ea.png

And each method can be implemented with two lines of code, an import statement and a Matplotlib use statement. 

While these libraries are great for creating cool diagrams, you still need to understand your target audience and possible visual issues such as color blindness.

Let us take a look below!

Before we start exploring Matplotlib charts, let's create some dummy data that will be used to generate the charts. 

A few lines of code are easy to get done.

import numpy as np 

# 生成 x 值
x = np.linspace( 0 , 10 , 20 ) 

# 生成 y 值
y = np.sin(x) 
y2 = np.cos(x)

01 Cyberpunk style - mplcyberpunk

Cyberpunk is a subgenre of science fiction known for its dystopias, technologically advanced worlds, and countercultural attitudes.

Scenes are often depicted in a futuristic style, often including elements such as neon lights and bright, vibrant colors.

9bf04916a69b0e0a667542eba1d9872a.png

For example, when creating a poster or an infographic, you need additional elements to attract readers. This is where the cyberpunk style can come into play.

However, be aware that it may be considered unprofessional when creating diagrams for publication or used in papers, and may not be suitable for readers with visual impairments.

ab68711b270010107c3d23e2c0a11afe.png

To start using this theme, the mplcyberpunk library needs to be installed first.

# 安装
pip install mplcyberpunk

To use the cyberpunk theme, just call plt.style.use() and pass in the parameter cyberpunk.

Use plt.scatter() to draw a scatterplot. To make our scatter glow, we also need to call the make_scatter_glow() function.

import mplcyberpunk
import matplotlib.pyplot as plt

# 使用赛博朋克风样式
plt.style.use('cyberpunk')
plt.figure(figsize=(8, 8))

# 散点图1
plt.scatter(x, y, marker='o')
mplcyberpunk.make_scatter_glow()

# 散点图2
plt.scatter(x, y2, marker='o', c='lime')
mplcyberpunk.make_scatter_glow()

# 坐标轴名称
plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# 显示
plt.show()

When running the above code, you can get the following figure.

f7f602a381a7c9b0fce74ff7ddbeb902.png

Apply a cyberpunk theme to a line chart. To make the lines glow, we can call 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 the run completes, it will return a graph with neon lines.

250bb316f374c2cbcb7b522796abd465.png

We can also take the chart effect a step further and add a gradient fill between the line and the x-axis.

# 使用赛博朋克风样式
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.

46dd31555e375835e636c81fe43ea9c0.png

Of course, this cyberpunk library has other beautification parameters, you can go to GitHub to check and use.

97237766f80a5a9ba06a2123fbe0e01f.png

Address: https://github.com/dhaitz/mplcyberpunk

02 Matplotx

The matplotx library provides an easy way to quickly beautify matplotlib charts.

It contains several easy-to-use themes that can be applied to any matplotlib chart. 

Bright white charts can be a little harsh when working with charts in a dark-themed Jupyter Notebook or VSCode. 

To reduce this effect, we can set the style to a dark theme. However, this requires several lines of code to achieve. 

As you'll see next, Matplotx makes this process much easier. Since it contains multiple themes, we can easily find one that matches the VSCode theme. 

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

# 安装
pip install matplotx

After installation, you can use the with statement to call plt.style.context and pass in matplotx.styles.

Here we can choose from the numerous available themes. Such as 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()

Run the code to get the following figure.

67aa7c3995e06e0e6067b9fdde3f05e1.png

Among them, there are many different styles in matplotx, the details are as follows.

cf7773543d97ec63b724189d63c778df.png

Let's create a line chart using the Pitaya Smoothie theme.

Since there are multiple subtopics, we need to access them using square brackets.

In this example, we have a dark theme, so we need to pass in the "dark" parameter to use it.

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 will get the line chart below.

066c4c9adcc047095f2867dc6e2e58dd.png

Of course there are also many light themes. For example Pitaya Smoothie has one that can be used like this.

with plt.style.context(matplotx.styles.pitaya_smoothie['light']):

90238c4f974b0ccb59d948949d5343fb.png

If you want to know more about this library, you can check out its GitHub.

Address: https://github.com/nschloe/matplotx

03 Quantum Black Style

QuantumBlack Labs is a company founded in 2012 to help other companies use data to make better decisions.

They use advanced techniques such as machine learning and artificial intelligence to analyze complex data sets across a range of industries including healthcare, finance and transportation.

A few years ago, they released their style library on GitHub.

address:

https://github.com/quantumblacklabs/qbstyles

To use this theme, we need to install this Python library.

# 安装
pip install qbstyles

After installation, you can use it~

from qbstyles import mpl_style

# 深色主题开
mpl_style(dark=True)

# 深色主题关
mpl_style(dark=False)

To create a scatterplot, the code is as follows.

from qbstyles import mpl_style

# 深色主题
mpl_style(dark=True)

# 非深色主题
mpl_style(dark=False)

# 散点图
plt.scatter(x, y, c=y2)

# 颜色
plt.colorbar(label='Y2')

# 坐标轴名称
plt.xlabel('X')
plt.ylabel('Y')

# 显示
plt.show()

Returns two chart results as follows, depending on whether you have selected a light or dark theme.

4e7598c66645edca36fa33cd5aa0b857.png

f25cbb1d89a6e378dffebcdbe89a59e4.png

Let's see what a dark theme line chart looks like.

# 深色主题
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 code, we get the graph below.

4374ad4ebd47f579fa33f0340eecd95d.png

The plotting style of this library is slightly toned down compared to plots generated in Matplotx.

However, it does feel a bit more professional, especially with the light-themed drawings. Ideal for use in professional presentations or training course materials.

04 Scientific Charts

When writing a scientific journal or conference article, it is crucial to create a figure that is clear, simple and easy to interpret.

Some journals, such as Nature, require a fixed style to simplify graphics that are difficult for readers to understand. 

This is where the SciencePlots library comes in. It is often used to generate common scientific journal style figures, making creating figures much easier. 

One of the great things about this library is that it makes diagrams suitable for printing in black and white - still a common practice among researchers. This will make the lines easily distinguishable by changing the line style or ensuring that there are different shapes on the scatterplot for categorical data. 

If you want to explore more styles available in SciencePlots, you can visit its GitHub.

address:

https://github.com/garrettj403/SciencePlots/wiki/Gallery

Running the SciencePlots library requires LaTeX to be installed on your computer. You can find more details about LaTex and how to install it at the address below. 

Address: https://www.latex-project.org/get/

After installing the library and setting up LaTeX, you can use the following code to create a scientific graph.

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 graph, which is perfect for a journal publication.

b215b33e8db59483daa329df17e0a9d5.png

Let's see what the IEEE style looks like. We can create one by modifying the parameters.

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

It's a bit different from the one above, and the colors have changed, but still a nice looking graph for a scientific journal.

e99c76ed9a0100a51c34bcc34bc6cbe2.png

This is very helpful for students engaged in academic research.

Take the hassle out of adjusting sizes, colors, and line styles with SciencePlots. 

05 Summary

In this article, we have looked at four very useful matplotlib python libraries that can take your matplotlib charts to the next level.

Each of these libraries provides unique styles for your plots. 

While these styles make for great creative visualizations, you do need to consider your audience.

The whole point of creating great data visuals is to make your message understandable and share your story in the best way possible. 

Guess you like

Origin blog.csdn.net/river_star1/article/details/130191179