如何避免Matplotlib因Y轴刻度值差异调整坐标轴尺寸?
问题:Matplotlib坐标轴框架尺寸随Y轴内容变化导致X轴对齐失效
我正在撰写LaTeX报告(该问题在Word中同样存在),核心困扰是无法阻止Matplotlib在Y轴标签/刻度变化时调整坐标轴(图内框架)的尺寸。这些图片需单独保存(因此无法使用subplots的sharex参数),它们基于时间维度,在报告中放置时要求X轴刻度、标签及数据保持垂直对齐。但Y轴范围变化时,Matplotlib为容纳负号或不同量级的刻度值,会缩小坐标轴框架尺寸,导致对齐失效。我使用自定义mplstyle脚本,尝试过调整bbox(tight与standard)、子图参数、Y轴标签对齐方式,以及figure.autolayout和figure.constrained_layout选项,但问题仍未解决。
示例代码
import matplotlib.pyplot as plt plt.style.use("personal_repo.styles.mdpi") fig, ax = plt.subplots() ax.plot( [0, 1, 2, 3, 4], [0, 1, 4, 9, 16], ) fig.savefig("example1.png") fig, ax = plt.subplots() ax.plot( [0, 1, 2, 3, 4], [-4, -2, 0, 2, 4], ) fig.savefig("example2.png")
自定义mplstyle配置
lines.linewidth: 0.5 # line width in points font.family: serif font.serif: "Palatino Linotype" font.size: 8.0 text.usetex: True # use latex for all text handling. The following fonts axes.linewidth: 1 # edge line width axes.titlesize: 8.0 # font size of the axes title axes.titlepad: 8.0 # pad between axes and title in points axes.labelsize: 8.0 # font size of the x and y labels # according to the user's locale. # For example, use ',' as a decimal # separator in the fr_FR locale. axes.formatter.use_mathtext: True # When True, use mathtext for scientific # notation. axes.prop_cycle : (cycler('color', ['k', 'r', 'b', 'g']) + cycler('ls', ['-', '--', ':', '-.'])) axes.xmargin: .05 # x margin. See `axes.Axes.margins` axes.ymargin: .05 # y margin. See `axes.Axes.margins` axes.zmargin: .05 # z margin. See `axes.Axes.margins` axes.autolimit_mode: "round_numbers" #round_numbers # If "data", use axes.xmargin and axes. xtick.top: True xtick.bottom: True xtick.major.size: 4.0 xtick.minor.size: 2.0 xtick.major.width: 1 xtick.minor.width: 1 xtick.direction: out xtick.minor.visible: True xtick.major.top: True xtick.major.bottom: True xtick.minor.top: True xtick.minor.bottom: True xtick.major.pad: 5.0 xtick.minor.pad: 5.0 ytick.labelsize:medium ytick.left: True ytick.right: True ytick.major.size: 4.0 ytick.minor.size: 2.0 ytick.major.width: 1 ytick.minor.width: 1 ytick.direction: out ytick.minor.visible: True ytick.major.left: True ytick.major.right: True ytick.minor.left: True ytick.minor.right: True ytick.major.pad: 5.0 ytick.minor.pad: 5.0 xtick.labelsize:medium grid.color: black #"#b0b0b0" # grid color grid.alpha: 0.1 # transparency, between 0.0 and 1.0 legend.loc: best legend.frameon: True # if True, draw the legend on a background patch legend.framealpha: 1 # legend patch transparency legend.edgecolor: 0 # background patch boundary color legend.fancybox: False # if True, use a rounded box for the legend.markerscale: 0.25 # the relative size of legend markers vs. original legend.fontsize: medium legend.handlelength: 1 # the length of the legend lines legend.handletextpad: 0.5 # the space between the legend line and legend text legend.columnspacing: .75 # column separation figure.figsize: 5.6, 1.7305315203955498 # figure size in inches figure.dpi: 300 # figure dots per inch figure.autolayout: True # When True, automatically adjust subplot figure.constrained_layout.use: False # When True, automatically make plot savefig.dpi: 300 # figure dots per inch or 'figure' savefig.bbox: tight # {tight, standard}
内容的提问来源于stack exchange,提问作者Danky
相关产品推荐
相关产品推荐

