只想绘制一个包含 50 个(实际上是 51 个)元素的列表:从 0 到 50 的列表索引应该表示 x Axis 上从 0 到 10 米的米,而每个元素的索引增加 0.2 米。 示例:
list = [2.5, 3, 1.5, ... , 7, 9]
len(list)
>>50
我想要绘制从 0 到 10 米的 x Axis ,即 (x,y)==(0, 2.5), (0.2, 3), (0.4, 1.5), ..., (9.8, 7), (10, 9)
相反,该列表显然是在 0 到 50 的 x 尺度上绘制的。 知道如何解决问题吗?谢谢!
请您参考如下方法:
我会避免将列表对象命名为 list。它混淆了 namespace 。但是尝试类似的东西
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
x = np.arange(0, 10, 0.2)
y = [2.5, 3, 1.5, ... , 7, 9]
ax.plot(x, y)
plt.show()
它使用 np.arange 在 x Axis 上创建一个点列表,这些点出现在 0.2 的倍数处,matplotlib 将在此处绘制 y 值。 Numpy 是一个用于轻松创建和操作向量、矩阵和数组的库,尤其是当它们非常大时。
编辑:
fig.add_subplot(N_row,N_col,plot_number) 是使用 matplotlib 绘图的面向对象方法。如果您想将多个子图添加到同一个图形,这很有用。例如,
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
将两个子图添加到同一个图形 fig。它们将排成两排,一个在另一个之上。 ax2 是底部的子图。看看这个 relevant post了解更多信息。
要更改实际的 x 刻度和刻度标签,请使用类似的东西
ax.set_xticks(np.arange(0, 10, 0.5))
ax.set_xticklabels(np.arange(0, 10, 0.5))
# This second line is kind of redundant but it's useful if you want
# to format the ticks different than just plain floats.
