Matplotlib draws two functions

Tip: This article is completed using python's matplotlib library, drawing two functions with the x-axis interval in [-5,5], adding text, and moving the central axis for beautification

topic

Generate images in the -5<=x<=5 interval of two arbitrary functions on one coordinate axis

Implementation steps

1. Import related libraries and function packages, and configure Chinese labels and negative sign display

import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['simhei']   #用于正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False     #用于正常显示负号

2. Create data and windows, define the x variable range and two functions y1, y2

#创建数据,定义 x 变量的范围 (-5,5) 数量 50
x = np.linspace(-5, 5, 50)
y1 = x**2
y2 = x**3
#创建figure窗口并指定大小
plt.figure(num=1,figsize=(8,5))

3. Draw the function image, set the coordinate axis range, name, scale

# Figure 并指定大小
plt.figure(num=1, figsize=(8,5))
# 绘制 y1=x^2和y2=x^3的图像,设置 color 为 red,线宽度是 1,线的样式是 --和-
plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--')
plt.plot(x,y2,color='blue',linewidth=1.0,linestyle='-')
# 设置 x,y 轴的范围
plt.xlim(-5,5)
plt.ylim(-5,5)
# 设置坐标轴刻度线
# Tick X和Y 范围 (-5,5),Tick Label刻度数量 11 个
new_ticks = np.linspace(-5,5,11)
plt.xticks(new_ticks)
plt.yticks(new_ticks)

4. Move the coordinate axis and draw the cross coordinate system, which can express the two functions more intuitively

# 设置坐标轴 gca() 获取坐标轴信息
ax = plt.gca()
# 使用.spines设置边框,去除边框颜色
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
# 移动坐标轴,将 bottom 即是 x 坐标轴设置到 y=0 的位置 
# 将 left 即是 y 坐标轴设置到 x=0 的位置
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))

5. Set the title and function formula, and finally display the image

# 设置标签
ax.set_title('绘制两种函数的折线图',fontsize=14)
plt.text(2.3, 4, 'y1 = x^2',fontsize=14,color='r')
plt.text(0.5, 3, 'y2 = x^3',fontsize=14,color='b')
# 显示图像
plt.show()

Full code:

import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['simhei']   #用于正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False     #用于正常显示负号
# 定义 x 变量的范围 (-3,3) 数量 50
x = np.linspace(-3, 3, 50)
y1 = x**2
y2 = x**3

# Figure 并指定大小
plt.figure(num=1, figsize=(8,5))
# 绘制 y1=x^2和y2=x^3的图像,设置 color 为 red,线宽度是 1,线的样式是 --和-
plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--')
plt.plot(x,y2,color='blue',linewidth=1.0,linestyle='-')
# 设置 x,y 轴的范围以及 label 标注
plt.xlim(-5,5)
plt.ylim(-5,5)

# 设置坐标轴刻度线
# Tick X和Y 范围 (-5,5) Tick Label刻度数量 11 个
new_ticks = np.linspace(-5,5,11)
plt.xticks(new_ticks)
plt.yticks(new_ticks)
# Tick Y 范围(-2.2,-1,1,1.5,2.4) ,Tick Label (-2.2, -1, 1, 1.5, 2.4) 别名(下面的英文)
# 设置坐标轴 gca() 获取坐标轴信息
ax = plt.gca()
# 使用.spines设置边框:x轴;将右边颜色设置为 none。
# 使用.set_position设置边框位置:y=0的位置;(位置所有属性:outward,axes,data)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')

# 移动坐标轴
# 将 bottom 即是 x 坐标轴设置到 y=0 的位置。
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
# 将 left 即是 y 坐标轴设置到 x=0 的位置。
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))

# 设置标签
ax.set_title('绘制两种函数的折线图',fontsize=14)
plt.text(2.3, 4, 'y1 = x^2',fontsize=14,color='r')
plt.text(0.2, 3, 'y2 = x^3',fontsize=14,color='b')
# 显示图像
plt.show()

operation result:
insert image description here

Summarize

1. Chinese characters and negative signs cannot be displayed in the canvas
Solution:
plt.rcParams() function, through the rc configuration file to customize various default properties of the graphics

plt.rcParams['font.sans-serif'] = ['simhei']   #用于正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False     #用于正常显示负号

2. The coordinate axis cannot display complete scales, only even scales are displayed instead of odd scales.
Solution:
Set integer coordinates between (-5, 5) according to requirements, and the number of scales is 11

new_ticks = np.linspace(-5,5,11)
plt.xticks(new_ticks)

3. How to move the coordinate axis
Solution:
ax.spines['bottom'] gets the bottom axis, and sets the position of the bottom axis through the set_position method.
For example: ax.spines['bottom'].set_position(('data',0 ))
This line of code means to set the bottom axis to move to the 0 coordinate position of the vertical axis, and the method of setting left is the same

Guess you like

Origin blog.csdn.net/weixin_51380854/article/details/129810974