如何使python中的绘图从X轴的x值开始?

问题描述 投票:0回答:2

我是matplotlib的新手,我有一个我想绘制的功能。目前我有下一个代码:

import matplotlib.pyplot as plt
plt.plot(f)

问题是我希望函数从X轴的值10开始绘制,而不是从0开始绘制。我该怎么做?

python matplotlib plot axis
2个回答
0
投票

这是一个如何实现这一目标的例子。

我们传递x轴值作为我们想要绘制的范围,如10到15之间,

x = numpy.linspace(10,15,100) #hundred points between 10 and 15
y = numpy.sin(x)/x            #function

现在我们将xlim设置为我们想要从0,15显示图形的范围

plt.plot(x,y) 
plt.xlim((0,15))
plt.show() 

plot starts from 10 while the x-axis starts from 0


0
投票

根据plot documentation,您可以在第一个参数中指定x坐标(可选):

plot([x], y, [fmt], data=None, **kwargs)

如果您正在使用numpy绘制函数,则执行以下操作:

import matplotlib.pyplot as plt
import numpy as np

# Data for plotting
x = np.arange(10.0, 20.0, 0.1)
f = your_function(x)

fig, ax = plt.subplots()
ax.plot(x, f)
plt.show()

在这里查看更多示例:matplotlib.org/gallery/index.html

© www.soinside.com 2019 - 2024. All rights reserved.