关于np.array([0,100])用作绘图X轴的疑问:是生成序列还是仅含两值?
Clarifying
np.array([0,100]) for Plotting X-Axis Hey there! Let me clear up this confusion for you right away:
np.array([0,100]) only contains two distinct values: 0 and 100 — it does NOT generate a continuous sequence of numbers from 0 to 100.
Why would someone use this for an X-axis?
This is typically used when you only need to plot data at those two specific X positions. For example:
- You have two discrete data points (like measurements taken at 0 and 100 units of some variable)
- You want to draw a straight line connecting two points, where the X coordinates are exactly 0 and 100
Here’s a quick code example to illustrate:
import numpy as np import matplotlib.pyplot as plt # X-axis has just two values: 0 and 100 x = np.array([0, 100]) # Corresponding Y-values y = np.array([10, 30]) # This will draw a line from (0,10) to (100,30) plt.plot(x, y) plt.show()
If you wanted a sequence from 0 to 100...
You’d use functions like np.arange() or np.linspace() instead:
np.arange(0, 101)generates integers from 0 to 100 (step size of 1 by default)np.linspace(0, 100, num=101)creates 101 evenly spaced points between 0 and 100 (inclusive)
内容的提问来源于stack exchange,提问作者Pranav_m7
相关产品推荐
相关产品推荐

