Matplotlib:第一个轴对象位于何处?
Matplotlib: Where is the first axes object located?
通常当我制作一个图时,我只是调用 fig,ax = plt.subplots()
而不指定在图中放置轴对象的位置。但是当我想放置图例或如下例所示的复选框时,相对坐标是相对于图形的。所以经常我的盒子到处都是。
在这种情况下,我想将方框放在我已经在 ax1
中的图表旁边,但我不知道 ax1
的位置。
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons
x = range(0,10)
y = range(0,10)
fig, ax1 = plt.subplots()
ax1.plot(x,y, color = "blue")
ax1.set_title('This is my graph')
#create the check box [left/bottom/width/height]
ax2 = plt.axes([0.95, 0.975, 0.05, 0.025])
check_box = CheckButtons(ax2, ['On',], [False,])
plt.show()
matplotlib 是否将第一个 ax1
对象放在一个恒定的位置,以便可以确定相对于它放置图例或框的位置?
或者是否必须以不同的方式解决这个问题?
感谢您的帮助!
我会同时创建两个轴,如下所示。注意:gridspec_kw={'width_ratios': [4, 1]}
允许您指定第一个轴相对于第二个轴大多少。
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons
x = range(0,10)
y = range(0,10)
fig, (ax1, ax2) = plt.subplots(1, 2, gridspec_kw={'width_ratios': [4, 1]})
ax1.plot(x,y, color = "blue")
ax1.set_title('This is my graph')
#create the check box [left/bottom/width/height]
ax2.axis(False)
ax2.set_aspect("equal")
check_box = CheckButtons(ax2, ['On',], [False,])
plt.tight_layout()
plt.show()
通常当我制作一个图时,我只是调用 fig,ax = plt.subplots()
而不指定在图中放置轴对象的位置。但是当我想放置图例或如下例所示的复选框时,相对坐标是相对于图形的。所以经常我的盒子到处都是。
在这种情况下,我想将方框放在我已经在 ax1
中的图表旁边,但我不知道 ax1
的位置。
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons
x = range(0,10)
y = range(0,10)
fig, ax1 = plt.subplots()
ax1.plot(x,y, color = "blue")
ax1.set_title('This is my graph')
#create the check box [left/bottom/width/height]
ax2 = plt.axes([0.95, 0.975, 0.05, 0.025])
check_box = CheckButtons(ax2, ['On',], [False,])
plt.show()
matplotlib 是否将第一个 ax1
对象放在一个恒定的位置,以便可以确定相对于它放置图例或框的位置?
或者是否必须以不同的方式解决这个问题?
感谢您的帮助!
我会同时创建两个轴,如下所示。注意:gridspec_kw={'width_ratios': [4, 1]}
允许您指定第一个轴相对于第二个轴大多少。
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons
x = range(0,10)
y = range(0,10)
fig, (ax1, ax2) = plt.subplots(1, 2, gridspec_kw={'width_ratios': [4, 1]})
ax1.plot(x,y, color = "blue")
ax1.set_title('This is my graph')
#create the check box [left/bottom/width/height]
ax2.axis(False)
ax2.set_aspect("equal")
check_box = CheckButtons(ax2, ['On',], [False,])
plt.tight_layout()
plt.show()