我有一个带有嵌入式图形的程序,该程序还显示了带有自定义刻度标签的色条。但是,自动调整图的大小会切除部分刻度标签。如果我可以简单地手动调整窗口大小以便再次显示所有内容,这还不错。但是,当您调整窗口的大小时,图形会以这样一种方式拉伸:左侧会留出很大的空白空间,而右侧的刻度标签仍然无法完全看到,除非您将窗口不必要地扩大。我该如何改变这种行为?下面是一个例子。如您所见,我已经尝试了一些方法,但没有一个起作用。
import numpy as np
import sys
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import \
FigureCanvasQTAgg as FigureCanvas
import matplotlib.ticker as mticker
from PyQt5.QtWidgets import QApplication, QDialog, QGridLayout
from PyQt5.QtCore import QSize
class MathTextSciFormatter(mticker.Formatter):
def __init__(self, fmt="%1.1e"):
self.fmt = fmt
def __call__(self, x, pos=None):
s = self.fmt % x
decimal_point = '.'
positive_sign = '+'
tup = s.split('e')
significand = tup[0].rstrip(decimal_point)
sign = tup[1][0].replace(positive_sign, '')
exponent = tup[1][1:].lstrip('0')
if exponent:
exponent = '10^{%s%s}' % (sign, exponent)
if significand and exponent:
s = r'%s{\times}%s' % (significand, exponent)
else:
s = r'%s%s' % (significand, exponent)
return "${}$ W/m${{}}^2$".format(s)
class Test(QDialog):
def __init__(self):
super().__init__()
self.lay = QGridLayout(self)
self.fig, self.ax = plt.subplots()
# self.ax.set_anchor('SW')
# self.ax.set_position([0, 0, 0, 0])
# self.fig.tight_layout()
# self.ax.margins(0, 0)
self.canvas = FigureCanvas(self.fig)
self.lay.addWidget(self.canvas)
self.resize(QSize(380, 290))
self.setMaximumHeight(290)
self.image = np.reshape(np.random.randint(0, 1000, 10000), (100, 100))
self.image_artist = self.ax.imshow(self.image)
self.colorbar = self.fig.colorbar(self.image_artist)
self.colorbar.ax.yaxis.set_major_formatter(
MathTextSciFormatter()
)
if __name__ == '__main__':
app = QApplication(sys.argv)
test = Test()
test.show()
sys.exit(app.exec_())
正如ImportanceOfBeingErnest在评论中指出的那样,利用constrained_layout
可以达到目的。
import numpy as np
import sys
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import \
FigureCanvasQTAgg as FigureCanvas
import matplotlib.ticker as mticker
from PyQt5.QtWidgets import QApplication, QDialog, QGridLayout
from PyQt5.QtCore import QSize
class MathTextSciFormatter(mticker.Formatter):
def __init__(self, fmt="%1.1e"):
self.fmt = fmt
def __call__(self, x, pos=None):
s = self.fmt % x
decimal_point = '.'
positive_sign = '+'
tup = s.split('e')
significand = tup[0].rstrip(decimal_point)
sign = tup[1][0].replace(positive_sign, '')
exponent = tup[1][1:].lstrip('0')
if exponent:
exponent = '10^{%s%s}' % (sign, exponent)
if significand and exponent:
s = r'%s{\times}%s' % (significand, exponent)
else:
s = r'%s%s' % (significand, exponent)
return "${}$ W/m${{}}^2$".format(s)
class Test(QDialog):
def __init__(self):
super().__init__()
self.lay = QGridLayout(self)
self.fig, self.ax = plt.subplots(constrained_layout=True)
# self.ax.set_anchor('SW')
# self.ax.set_position([0, 0, 0, 0])
# self.fig.tight_layout()
# self.ax.margins(0, 0)
self.canvas = FigureCanvas(self.fig)
self.lay.addWidget(self.canvas)
self.resize(QSize(380, 290))
self.setMaximumHeight(290)
self.image = np.reshape(np.random.randint(0, 1000, 10000), (100, 100))
self.image_artist = self.ax.imshow(self.image)
self.colorbar = self.fig.colorbar(self.image_artist)
self.colorbar.ax.yaxis.set_major_formatter(
MathTextSciFormatter()
)
if __name__ == '__main__':
app = QApplication(sys.argv)
test = Test()
test.show()
sys.exit(app.exec_())