如何在 matplotlib 中向上移动整个图表?
How to move a whole diagram up in matplotlib?
当更改为不同的子图时,如何在 matplot 中移动整个图表。
问题是我使用按钮在不同图表之间切换,但 x_labels 与按钮重叠。那么有没有一种方法可以在更改图表时简单地将图表向上移动一点,以便按钮和 x_labels 不会相互重叠?
或者是否有其他(更好的)解决方案来解决这个问题?
这是一张图片,您可以在其中看到我的问题:
下面是一些示例代码:
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
import datetime
class Index(object):
def start(self, event=None):
ax.clear()
ax.set_title("Start")
x_values = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
y_values = [10, 20, 15, 1, 5]
ax.set_xticklabels(x_values, rotation=25)
print(ax.get_position())
ax.bar(x_values, y_values)
plt.draw()
def next(self, event):
ax.clear()
ax.set_title("Next")
x_values = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
y_values = [20, 15, 10, 5, 1]
ax.set_xticklabels(x_values, rotation=25)
print(ax.get_position())
ax.bar(x_values, y_values)
plt.draw()
def prev(self, event):
ax.clear()
ax.set_title("Previous")
x_values = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
y_values = [1, 5, 10, 15, 20]
ax.set_xticklabels(x_values, rotation=25)
print(ax.get_position())
ax.bar(x_values, y_values)
plt.draw()
ax = plt.gca()
callback = Index()
callback.start()
axprev = plt.axes([0.59, 0.002, 0.1, 0.075])
bprev = Button(axprev, 'Previous')
bprev.on_clicked(callback.prev)
axstart = plt.axes([0.7, 0.002, 0.1, 0.075])
bstart = Button(axstart, 'Start')
bstart.on_clicked(callback.start)
axnext = plt.axes([0.81, 0.002, 0.1, 0.075])
bnext = Button(axnext, 'Next')
bnext.on_clicked(callback.next)
plt.show()
一种快速而简单的方法是使用 subplots_adjust
plt.subplots_adjust(bottom=0.2)
它很脏,因为您必须手动 fiddle 值(例如 0.2
)
直到你找到一个看起来正确的数字。
但是,似乎需要一些摆弄来定位 axprev
、axstart
和 axnext
。
更好的方法是使用 matplotlib.gridspec.GridSpec
:
布局轴
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(10, 40)
ax = plt.subplot(gs[:7, :])
axprev = plt.subplot(gs[9:, 18:24])
axstart = plt.subplot(gs[9:, 26:32])
axnext = plt.subplot(gs[9:, 34:40])
例如,
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from matplotlib.widgets import Button
import datetime
class Index(object):
def start(self, event=None):
ax.clear()
ax.set_title("Start")
x_values = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
y_values = [10, 20, 15, 1, 5]
ax.set_xticklabels(x_values, rotation=25)
print(ax.get_position())
ax.bar(x_values, y_values)
plt.draw()
def next(self, event):
ax.clear()
ax.set_title("Next")
x_values = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
y_values = [20, 15, 10, 5, 1]
ax.set_xticklabels(x_values, rotation=25)
print(ax.get_position())
ax.bar(x_values, y_values)
plt.draw()
def prev(self, event):
ax.clear()
ax.set_title("Previous")
x_values = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
y_values = [1, 5, 10, 15, 20]
ax.set_xticklabels(x_values, rotation=25)
print(ax.get_position())
ax.bar(x_values, y_values)
plt.draw()
gs = gridspec.GridSpec(10, 40)
ax = plt.subplot(gs[:7, :])
axprev = plt.subplot(gs[9:, 18:24])
axstart = plt.subplot(gs[9:, 26:32])
axnext = plt.subplot(gs[9:, 34:40])
callback = Index()
callback.start()
bprev = Button(axprev, 'Previous')
bprev.on_clicked(callback.prev)
bstart = Button(axstart, 'Start')
bstart.on_clicked(callback.start)
bnext = Button(axnext, 'Next')
bnext.on_clicked(callback.next)
plt.show()
当更改为不同的子图时,如何在 matplot 中移动整个图表。
问题是我使用按钮在不同图表之间切换,但 x_labels 与按钮重叠。那么有没有一种方法可以在更改图表时简单地将图表向上移动一点,以便按钮和 x_labels 不会相互重叠?
或者是否有其他(更好的)解决方案来解决这个问题?
这是一张图片,您可以在其中看到我的问题:
下面是一些示例代码:
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
import datetime
class Index(object):
def start(self, event=None):
ax.clear()
ax.set_title("Start")
x_values = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
y_values = [10, 20, 15, 1, 5]
ax.set_xticklabels(x_values, rotation=25)
print(ax.get_position())
ax.bar(x_values, y_values)
plt.draw()
def next(self, event):
ax.clear()
ax.set_title("Next")
x_values = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
y_values = [20, 15, 10, 5, 1]
ax.set_xticklabels(x_values, rotation=25)
print(ax.get_position())
ax.bar(x_values, y_values)
plt.draw()
def prev(self, event):
ax.clear()
ax.set_title("Previous")
x_values = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
y_values = [1, 5, 10, 15, 20]
ax.set_xticklabels(x_values, rotation=25)
print(ax.get_position())
ax.bar(x_values, y_values)
plt.draw()
ax = plt.gca()
callback = Index()
callback.start()
axprev = plt.axes([0.59, 0.002, 0.1, 0.075])
bprev = Button(axprev, 'Previous')
bprev.on_clicked(callback.prev)
axstart = plt.axes([0.7, 0.002, 0.1, 0.075])
bstart = Button(axstart, 'Start')
bstart.on_clicked(callback.start)
axnext = plt.axes([0.81, 0.002, 0.1, 0.075])
bnext = Button(axnext, 'Next')
bnext.on_clicked(callback.next)
plt.show()
一种快速而简单的方法是使用 subplots_adjust
plt.subplots_adjust(bottom=0.2)
它很脏,因为您必须手动 fiddle 值(例如 0.2
)
直到你找到一个看起来正确的数字。
但是,似乎需要一些摆弄来定位 axprev
、axstart
和 axnext
。
更好的方法是使用 matplotlib.gridspec.GridSpec
:
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(10, 40)
ax = plt.subplot(gs[:7, :])
axprev = plt.subplot(gs[9:, 18:24])
axstart = plt.subplot(gs[9:, 26:32])
axnext = plt.subplot(gs[9:, 34:40])
例如,
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from matplotlib.widgets import Button
import datetime
class Index(object):
def start(self, event=None):
ax.clear()
ax.set_title("Start")
x_values = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
y_values = [10, 20, 15, 1, 5]
ax.set_xticklabels(x_values, rotation=25)
print(ax.get_position())
ax.bar(x_values, y_values)
plt.draw()
def next(self, event):
ax.clear()
ax.set_title("Next")
x_values = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
y_values = [20, 15, 10, 5, 1]
ax.set_xticklabels(x_values, rotation=25)
print(ax.get_position())
ax.bar(x_values, y_values)
plt.draw()
def prev(self, event):
ax.clear()
ax.set_title("Previous")
x_values = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
y_values = [1, 5, 10, 15, 20]
ax.set_xticklabels(x_values, rotation=25)
print(ax.get_position())
ax.bar(x_values, y_values)
plt.draw()
gs = gridspec.GridSpec(10, 40)
ax = plt.subplot(gs[:7, :])
axprev = plt.subplot(gs[9:, 18:24])
axstart = plt.subplot(gs[9:, 26:32])
axnext = plt.subplot(gs[9:, 34:40])
callback = Index()
callback.start()
bprev = Button(axprev, 'Previous')
bprev.on_clicked(callback.prev)
bstart = Button(axstart, 'Start')
bstart.on_clicked(callback.start)
bnext = Button(axnext, 'Next')
bnext.on_clicked(callback.next)
plt.show()