首页 > 解决方案 > 如何在 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()

标签: pythonmatplotlibdiagramsubplot

解决方案


一种快速而肮脏的方法是使用subplots_adjust

plt.subplots_adjust(bottom=0.2)

在此处输入图像描述

这很脏,因为您必须手动调整值(例如0.2),直到找到看起来正确的数字。

但是,似乎需要一些摆弄来定位axprev,axstartaxnext


更好的方法是使用以下方式布局轴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()

在此处输入图像描述


推荐阅读