使用以下代码将 x 轴设置在 x=5 处:

import matplotlib.pyplot as plt

# 生成数据
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

# 创建图形和坐标轴
fig, ax = plt.subplots()

# 设置x轴的范围和位置
ax.set_xlim(0, 10)
ax.spines['bottom'].set_position(('data', 5))

# 绘制曲线
ax.plot(x, y)

# 显示图形
plt.show()

这个代码将生成一个包含曲线的图形,并将 x 轴设置在 x=5 处。'ax.spines['bottom'].set_position(('data', 5))' 这一行代码将 x 轴的位置设置为数据坐标系中的 5。'ax.set_xlim(0, 10)' 则设置了 x 轴的范围。

Matplotlib 设置 X 轴位置:将 X 轴放在 X=5 处

原文地址: https://www.cveoy.top/t/topic/m1pR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录