在 matplotlib 中更改 x 轴的值

Posted

技术标签:

【中文标题】在 matplotlib 中更改 x 轴的值【英文标题】:Change values of x-axis in matplotlib 【发布时间】:2022-01-12 19:57:28 【问题描述】:
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax1 = fig.add_axes([0, 0, 1, 1])
input = np.arange(11,21,1)
sin = np.sin(input)
ax1.plot(sin)
plt.show()

我有上面的代码,它显示了给定输入从 11 到 20 的正确 sin 值的图,但 x 轴刻度显示的值从 0 到 10。我希望它是从 11 到 20。我该怎么做这样做还是我做错了什么?

(实际上,如果我将图形从 0 绘制到 21 并且当我将图形从 11 截断到 21 时,它会起作用。 有了这个:

np.arange(0, 21, 1)

ax1.set_xlim([11, 21])

这是唯一的方法吗?

【问题讨论】:

您需要ax1.plot(input, sin)。但是你绝对应该选择一个不同的变量名以避免隐藏内置的input 函数。 感谢您的回复!是的,我会小心使用关键字作为变量 【参考方案1】:

ax1.plot(input, sin)

同时使用input 命名变量是一种不好的做法,因为它是一个关键字

【讨论】:

感谢您的回答。是的,输入关键字,下次我会记住的

以上是关于在 matplotlib 中更改 x 轴的值的主要内容,如果未能解决你的问题,请参考以下文章

Matplotlib:更改轴的颜色

python --Matplotlib详解

如何使用 matplotlib 更改 y 轴的比例? [复制]

matplotlib 散点图

Python 的 Matplotlib 绘图顺序错误

如何更改 matplotlib 中的 x 轴以便没有空格?