在给定的 y 范围内绘制 matplotlib 中的垂直线
Plot vertical lines in matplotlib within a given y range
我正在使用以下内容:
fig, ax = plt.subplots(figsize=(20, 10))
ax.set_ylim(bottom=0, top=10)
for i in range(4):
ax.axvline(x=i, ymin=5, ymax=9, color="red", linewidth=40)
给出:
我希望从 y = 5
到 y = 9
的每个点都有一条垂直线。
您应该使用 matplotlib.pyplot.vlines
,正如 BigBen 在评论中所建议的:
for i in range(4):
ax.vlines(x=i, ymin=5, ymax=9, color="red", linewidth=40)
如果您查看 axvline 的参数,您会发现 ymin 和 ymax 从 0 变为 1。完整 ylimit 的一小部分。
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.axvline.html
因此您需要 .5 到 .9 之类的东西或计算适当的分数。
fig, ax = plt.subplots(figsize=(20, 10))
ax.set_ylim(bottom=0, top=10)
for i in range(4):
ax.axvline(x=i, ymin=.5, ymax=.9, color="red", linewidth=40)
输出:
我正在使用以下内容:
fig, ax = plt.subplots(figsize=(20, 10))
ax.set_ylim(bottom=0, top=10)
for i in range(4):
ax.axvline(x=i, ymin=5, ymax=9, color="red", linewidth=40)
给出:
我希望从 y = 5
到 y = 9
的每个点都有一条垂直线。
您应该使用 matplotlib.pyplot.vlines
,正如 BigBen 在评论中所建议的:
for i in range(4):
ax.vlines(x=i, ymin=5, ymax=9, color="red", linewidth=40)
如果您查看 axvline 的参数,您会发现 ymin 和 ymax 从 0 变为 1。完整 ylimit 的一小部分。 https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.axvline.html
因此您需要 .5 到 .9 之类的东西或计算适当的分数。
fig, ax = plt.subplots(figsize=(20, 10))
ax.set_ylim(bottom=0, top=10)
for i in range(4):
ax.axvline(x=i, ymin=.5, ymax=.9, color="red", linewidth=40)
输出: