matplotlib.figure.add_subplot.vlines方向
matplotlib.figure.add_subplot.vlines orientation
我的问题涉及vlines的方向。
在图片中,我在刻度线 2 - 6 上画了粗线。
我的目标是将这些条形图直接绘制在每个刻度的右侧。
我需要这样做,因为当我用鼠标单击图表时,1 到 2 之间的任何值都等于 1。2 到 3 之间的任何值都等于 2。
因此,当有人在第 2 个刻度栏上单击栏时,如果他们单击栏的左侧,则等同于错误的刻度栏。
我希望我的问题很清楚,感谢任何帮助。
听起来您不想要 vlines
,而是想要矩形。
vlines
(或任何线条)的粗细由其笔划粗细控制,以磅为单位。这并不直接对应于图中 "data" 坐标中的位置。
例如,我们可以使用 ax.bar
来创建覆盖您想要的位置的矩形:
import numpy as np
import matplotlib.pyplot as plt
# Data similar to yours
green = np.array([2.5, 1, 0.9, 0.7, 0.4])
red = green
blue = np.array([3.8, 2, 1.8, 1.5, 0.6])
x = np.arange(2, 7)
# Plot bars where the left edge will be at each x-value
fig, ax = plt.subplots()
ax.bar(x, green, width=1, color='green')
ax.bar(x, red, width=1, bottom=green, color='red')
ax.bar(x, blue, width=1, bottom=green+red, color='blue')
ax.set(xlim=[0.5, 7.5])
plt.show()
我的问题涉及vlines的方向。
在图片中,我在刻度线 2 - 6 上画了粗线。 我的目标是将这些条形图直接绘制在每个刻度的右侧。
我需要这样做,因为当我用鼠标单击图表时,1 到 2 之间的任何值都等于 1。2 到 3 之间的任何值都等于 2。
因此,当有人在第 2 个刻度栏上单击栏时,如果他们单击栏的左侧,则等同于错误的刻度栏。
我希望我的问题很清楚,感谢任何帮助。
听起来您不想要 vlines
,而是想要矩形。
vlines
(或任何线条)的粗细由其笔划粗细控制,以磅为单位。这并不直接对应于图中 "data" 坐标中的位置。
例如,我们可以使用 ax.bar
来创建覆盖您想要的位置的矩形:
import numpy as np
import matplotlib.pyplot as plt
# Data similar to yours
green = np.array([2.5, 1, 0.9, 0.7, 0.4])
red = green
blue = np.array([3.8, 2, 1.8, 1.5, 0.6])
x = np.arange(2, 7)
# Plot bars where the left edge will be at each x-value
fig, ax = plt.subplots()
ax.bar(x, green, width=1, color='green')
ax.bar(x, red, width=1, bottom=green, color='red')
ax.bar(x, blue, width=1, bottom=green+red, color='blue')
ax.set(xlim=[0.5, 7.5])
plt.show()