python - 如何在 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)
这很脏,因为您必须手动调整值(例如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()
推荐阅读
- c++ - How does std::greater<> wоrk in a set?
- python - 我的 OpenCV 实时网络摄像头演示没有显示准确的情绪
- video - 如果其中一个视频带有 FFmpeg 过滤器,如何合并两个视频?
- asp.net - 通过代码优先方法创建数据库的问题
- r - ggplot2与ifelse的颜色关联问题
- gitlab - 使用通配符证书与 helm 一起安装的 gitlab 注册表返回 x509
- c# - 如何在按钮文本上显示文本文件中的文本
- java - 使用插入排序时如何将更大的单词放在末尾?
- javascript - JavaScript 功能仅适用于一个 div,而不适用于整个文档
- python - 我想在我在 Reason 中写的数字上赚 5%