SoFunction
Updated on 2025-03-03

Summary of how to create custom visual charts using Matplotlib

Practical way to create custom visual charts with Matplotlib

Matplotlib is one of the most popular drawing libraries in Python, providing rich functionality and flexibility to enable users to create various types of visual charts. However, sometimes we need to go a step further and create custom visual charts based on specific needs. This article will explain how to use the various features and techniques in Matplotlib to create custom visual charts.

Import the necessary libraries

First, we need to import the Matplotlib library and other libraries that may be needed, such as NumPy to generate data:

import  as plt
import numpy as np

Create basic charts

We start with the most basic charts, such as simple line charts. Here we create a sine wave graph:

x = (0, 2*, 100)
y = (x)

(x, y)
('Sine Wave')
('x')
('sin(x)')
(True)
()

This example shows how to create a simple line chart using Matplotlib, and adds titles, axis labels, and grid lines.

Custom line styles and colors

Matplotlib allows us to customize the style and colors of lines. Here is an example showing how to draw multiple lines and specify different line styles and colors:

x = (0, 2*, 100)
y1 = (x)
y2 = (x)

(x, y1, linestyle='-', color='blue', label='sin(x)')
(x, y2, linestyle='--', color='red', label='cos(x)')
('Sine and Cosine Waves')
('x')
('y')
()
()

Add comments and tags

Sometimes we need to add comments or mark certain important points in the chart. Here is an example showing how to add comments and markers to a chart:

x = (0, 2*, 100)
y = (x)

(x, y)
('Sine Wave')
('x')
('sin(x)')
(True)

# Add comment(/2, 1, 'Maximum', fontsize=10, ha='center')
('Zero', xy=(, 0), xytext=(+0.5, 0.5),
             arrowprops=dict(facecolor='black', shrink=0.05))

()

Customize the chart layout

Sometimes we need to customize the layout of the chart, such as changing the arrangement of sub-graphs. Here is an example showing how to create a custom layout with multiple subgraphs:

x = (0, 2*, 100)
y1 = (x)
y2 = (x)

fig, axs = (2, 1, figsize=(8, 6))

axs[0].plot(x, y1, color='blue')
axs[0].set_title('Sine Wave')
axs[0].set_xlabel('x')
axs[0].set_ylabel('sin(x)')
axs[0].grid(True)

axs[1].plot(x, y2, color='red')
axs[1].set_title('Cosine Wave')
axs[1].set_xlabel('x')
axs[1].set_ylabel('cos(x)')
axs[1].grid(True)

plt.tight_layout()
()

Add legend and color bars

When creating multi-series or multi-category charts, adding legends can help viewers better understand the data. The color bars can provide additional information, especially when drawing two-dimensional data. Here is an example showing how to add legend and color bars:

x = (0, 2*, 100)
y1 = (x)
y2 = (x)

(x, y1, color='blue', label='sin(x)')
(x, y2, color='red', label='cos(x)')
('Sine and Cosine Waves')
('x')
('y')
()

# Add color barsscalar_map = (cmap='viridis')
scalar_map.set_array(x)
(scalar_map, label='x')

()

Create a custom chart type

In addition to common line and scatter plots, Matplotlib allows users to create custom chart types. Here is an example showing how to create a graph with a filled area:

x = (0, 2*, 100)
y = (x)

plt.fill_between(x, y, color='skyblue', alpha=0.4)
(x, y, color='blue')
('Filled Sine Wave')
('x')
('sin(x)')
(True)

()

Beautify charts with style sheets

Matplotlib provides a range of style sheets that allow users to easily beautify their charts. Here is an example showing how to beautify a chart using a built-in stylesheet:

('seaborn-darkgrid')

x = (0, 2*, 100)
y = (x)

(x, y)
('Styled Sine Wave')
('x')
('sin(x)')

()

Add graphics and text comments

Adding graphical and text annotations to a chart can help highlight important data points or provide additional information. Here is an example showing how to add graphs and text comments to a chart:

x = (0, 2*, 100)
y = (x)

(x, y)
('Sine Wave with Annotations')
('x')
('sin(x)')
(True)

# Add arrow comment('Peak', xy=(/2, 1), xytext=(/2+0.5, 0.5),
             arrowprops=dict(facecolor='black', shrink=0.05))

# Add a circular mark([/2], [1], color='red')
(/2+0.2, 0.9, 'Max', fontsize=10)

()

Create 3D charts

Matplotlib supports not only two-dimensional charts, but also three-dimensional charts. Here is an example showing how to create a simple three-dimensional scatter plot:

from mpl_toolkits.mplot3d import Axes3D

fig = ()
ax = fig.add_subplot(111, projection='3d')

x = .standard_normal(100)
y = .standard_normal(100)
z = .standard_normal(100)

(x, y, z, c=z, cmap='viridis')
ax.set_title('3D Scatter Plot')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

()

Use animation functions

Matplotlib also provides rich animation features that allow users to create various types of dynamic charts. Here is an example showing how to create a simple animation effect:

import  as animation

fig, ax = ()

x = (0, 2*, 100)
y = (x)
line, = (x, y)

def animate(i):
    line.set_ydata((x + i/10.0))
    return line,

ani = (fig, animate, frames=100, interval=50)
('Animated Sine Wave')
('x')
('sin(x)')
()

Create complex layouts using subgraph grids

Matplotlib provides flexible subgraph grid functionality to create complex chart layouts. Here is an example showing how to create a complex layout with multiple subgraphs using a subgraph grid:

x = (0, 2*, 100)
y1 = (x)
y2 = (x)
y3 = (x)

fig, axs = (2, 2, figsize=(10, 8))

axs[0, 0].plot(x, y1, color='blue')
axs[0, 0].set_title('Sine Wave')
axs[0, 0].set_xlabel('x')
axs[0, 0].set_ylabel('sin(x)')
axs[0, 0].grid(True)

axs[0, 1].plot(x, y2, color='red')
axs[0, 1].set_title('Cosine Wave')
axs[0, 1].set_xlabel('x')
axs[0, 1].set_ylabel('cos(x)')
axs[0, 1].grid(True)

axs[1, 0].plot(x, y3, color='green')
axs[1, 0].set_title('Tangent Wave')
axs[1, 0].set_xlabel('x')
axs[1, 0].set_ylabel('tan(x)')
axs[1, 0].grid(True)

#Blank sub-pictureaxs[1, 1].axis('off')

plt.tight_layout()
()

Create object-oriented charts

In addition to using a simple pyplot interface, Matplotlib also supports object-oriented methods to create charts. Here is an example showing how to create a simple line chart using an object-oriented approach:

fig, ax = ()

x = (0, 2*, 100)
y = (x)

(x, y)
ax.set_title('Object-oriented Sine Wave')
ax.set_xlabel('x')
ax.set_ylabel('sin(x)')
(True)

()

Summarize

In this article, we dive into how to create custom visual charts using Matplotlib, a powerful Python drawing library. We start with the basic line chart and introduce a variety of practical methods and techniques step by step.

First, we learned how to create basic charts, including line charts and scatter charts, and added elements such as titles, labels, and grid lines. Next, we explore how to customize line styles, colors, and how to add legends and color bars to better display the data.

Second, we describe how to add comments and marks to the chart to highlight important data points or provide additional information. We then learned how to create custom chart types, such as charts with filled areas.

Further, we explore how to beautify the chart, including using style sheets and custom colors, etc. We also show how to create 3D charts and use animation features to make the chart more vivid and appealing.

Finally, we introduce the use of subgraph grids to create complex layouts and object-oriented methods to create charts. These methods allow us to organize and present data more flexibly, thus meeting a variety of visualization needs.

To sum up, Matplotlib provides rich functionality and flexibility, allowing users to easily create a variety of custom visual charts. Through this article, readers can be more proficient in using Matplotlib for data visualization and create more attractive and informative charts.

The above is the detailed summary of the method of creating custom visual charts using Matplotlib. For more information about creating visual charts by Matplotlib, please pay attention to my other related articles!