如何在 Matplotlib 中绘制位于图形中间的轴线?
要绘制位于图形中部的轴线,我们可以执行以下步骤:-
使用 numpy 创建x 和sqr 数据点。
使用figure()方法创建新图形或激活现有图形。
将轴线添加到图形中,作为子图排列的一部分。
设置左右两侧轴线的颜色。
设置上右两侧轴线的颜色。
使用 plot()方法绘制 x 和 sqr,其中标签为y=x2且颜色为红色。
使用legend() 方法放置图例。将位置设置为右上角。
要显示图形,请使用show()方法。
示例
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.arange(-10., 10., 0.2)
sqr = np.square(x)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
plt.plot(x, sqr, label="y=x^2", c='red')
plt.legend(loc=1)
plt.show()输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP