Как переместить всю диаграмму вверх в matplotlib? - PullRequest
0 голосов
/ 30 ноября 2018

Как мне переместить всю диаграмму в matplot при переходе на другой подплот.

Проблема в том, что я использую кнопки для переключения между различными диаграммами, но x_labels перекрываются с кнопками.Так есть ли способ просто немного изменить диаграммы при смене диаграмм, чтобы кнопки и x_labels не перекрывали друг друга?

Или есть другое (лучшее) решение, чтобы это исправить?

Вот изображение, на котором вы можете увидеть мою проблему: enter image description here

Вот пример кода:

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()

1 Ответ

0 голосов
/ 30 ноября 2018

Быстрый и грязный способ - использовать subplots_adjust

plt.subplots_adjust(bottom=0.2)

enter image description here

Это грязно, потому что вы должны вручную поиграться сзначение (например, 0.2) до тех пор, пока вы не найдете число, которое выглядит правильно.

Но тогда, кажется, что для позиционирования axprev, axstart и axnext.

* 1017 потребовалось некоторое возмущение.*

Лучшим способом было бы расположить оси, используя 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()

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...