Visualization is of great importance in python! It’s very easy to learn several modules including matplotlib and seaborn, however, when you try to draw a picture in python without using the help of AI assistance, it may be like this:
''' Author: Xiyuan Yang xiyuan_yang@outlook.com Date: 2025-03-14 17:37:11 LastEditors: Xiyuan Yang xiyuan_yang@outlook.com LastEditTime: 2025-03-14 17:37:16 FilePath: /visualzation/src/ugly_paint.py Description: Do you code and make progress today? Copyright (c) 2025 by Xiyuan Yang, All Rights Reserved. ''' import matplotlib as mlp mlp.use("Agg")
import matplotlib.pyplot as plt
# Simple data x = [1, 2, 3, 4, 5] y = [1, 4, 2, 3, 5]
# Basic plot with default settings plt.plot(x, y)
# Set title and labels with minimal styling plt.title('Ugly Plot') plt.xlabel('X Axis') plt.ylabel('Y Axis')
# Display the plot plt.savefig("figure/ugly plot.pdf")
You can make the pictures more advanced by adding several commands and customizations.
''' Author: Xiyuan Yang xiyuan_yang@outlook.com Date: 2025-03-14 14:53:43 LastEditors: Xiyuan Yang xiyuan_yang@outlook.com LastEditTime: 2025-03-14 17:38:23 FilePath: /visualzation/src/advanced.py Description: Do you code and make progress today? Copyright (c) 2025 by Xiyuan Yang, All Rights Reserved. ''' import matplotlib as mlp mlp.use("Agg")
import matplotlib.pyplot as plt import numpy as np np.random.seed(2024)
# Generating data X = np.linspace(0, 10 ,5000)
# Define the linear relation # The relation: y = k * x + b k = 2.0 b = 2.0
# Generating the true value of Y Y_true = k * X + b
Actually, this is what I can draw independently… Python gives users great freedom to customize their visualization, which at the same time increasing the learning cost for beginners. It’s unwise for Python beginners to learn how to draw a great picture by mastering all commands in matplotlib! It’s time-consuming and meaningless. Thus, I would like to introduce my drawing template in this blog, you can just clone and use it, saving lots of time for thinking more meaningful tasks!
Basic Principles
The basic principles:
Only introduce the 20% most basic yet most useful commands.
Giving
Templates
Preliminaries: Attention for Linux Users
In lack of QT platform in my WSL system, I have to make several modifications like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
import matplotlib as mpl mpl.use('Agg') #... #... import matplotlib.plt as plt # forbid scientific notation np.set_printoptions(suppress=True)
# Then showing the figure by saving the figures.
plt.savefig("save/img.png")
Templates: fonts
The part below is cloned from my github repo.
In [ ]:
1 2 3 4 5
# Preliminaries import numpy as np import matplotlib.pyplot as plt from scipy.stats import norm, entropy from matplotlib.font_manager import FontProperties
All the operations below are for Linux (or WSL) users, if you have Windows operating systems, I strongly recommend you to install WSL in your computer!
Firstly, use this command to check default type in your system.
In [ ]:
1 2
# Print the default font family print(plt.rcParams['font.family'])
Secondly, you can use the command below to check all fonts available in your operating system.
In [ ]:
1 2 3 4 5 6 7 8
import matplotlib.font_manager as fm
# Find all system fonts font_paths = fm.findSystemFonts(fontpaths=None)
# Print the list of available font paths for font in font_paths: print(font)
My output: [‘sans-serif’]
You can search all the fonts you like, but it’s time-consuming. In this tutorial, I only like to introduce two types of fonts that it’s most frequently used by myself: georgia (English) and songti (Chinese).
All the demonstrations are based on these two fonts.
In [ ]:
1 2 3
# Check whether the fonts available is included in the list: !fc-list | grep -i "songti" !fc-list | grep -i "georgia"
If you can see some outputs like the contents shown below (my contents):