【发布时间】:2021-07-24 14:08:50
【问题描述】:
我创建了以下代码,它打印了一个绘图并以对我有用的方式格式化轴标签和刻度。我遇到了tight_layout 的问题,它使我的垂直旋转的x 轴刻度标签以及图形窗口之外的x 轴标签。
为了尝试解决问题,我所做的是手动重新缩放绘图窗口,并从图形窗口手动设置矩形元组。经过一些尝试,我发现(左、下、右、上)的最佳值在我的情况下是 [0.163, 0.391, 0.905, 0.977]。接下来,我认为我应该将其合并到我的代码中,以便我的绘图首先以正确的大小出现:为此,我使用了以下命令:
fig.tight_layout(rect=[0.163, 0.391, 0.905, 0.977])
但是,它不起作用,并且该图形出现的 rect 值与我设置的值不同。
问题 1:如何从我的代码中设置 rect 值,而不是手动设置?
问题 2:是否有更好/更简单的替代方法来实现所需功能?
# dt_objects is a list of datetime objects. My x-axis is timestamps
# for y_axis, set any series. The code will set the y axis based on the min,max value of y-values
matdates=date2num(dt_objects)
x_axis=matdates
fig,ax = plt.subplots()
ax.plot_date(x_axis,y_axis,markersize=8)
ax.axhline(y=y_axis.mean(),linestyle='--',color='red',alpha=0.5)
ax.xaxis.set_major_locator(AutoDateLocator(minticks=1, maxticks=5)) #Set position of Major X ticks
ax.xaxis.set_minor_locator(AutoDateLocator(minticks=10, maxticks=30)) #Set position of Minor X ticks
ax.xaxis.set_major_formatter( DateFormatter('%Y/%m/%d-%H:%M:%S')) #Set format of Major X ticks
ax.xaxis.set_minor_formatter( DateFormatter('%H:%M:%S')) #Set format of X ticks
ax.tick_params(axis='x',which='major',rotation=90,labelsize=14) #Set parameters of Major X ticks
ax.tick_params(axis='x',which='minor',rotation=80,labelsize=12) #Set parameters of Major X ticks
plt.setp(ax.get_xticklabels(), fontsize=14, fontweight="bold") #Set font of Major X ticks
ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f')) #Set format of Major Y ticks
ax.tick_params(axis='y',which='major',labelsize=14)
calculateYpadding=(y_axis.max()-y_axis.min())*0.1 # Padding is 10% of the max difference in y values :)
ax.set_ylim(round(y_axis.min(),2)-calculateYpadding, round(y_axis.max(),2)+calculateYpadding) #Set boundaries of Y axis
ax.yaxis.set_major_locator(MaxNLocator(nbins = 'auto',min_n_ticks = 5))
plt.grid()
ax.set_xlabel("Time",style='italic',fontsize=14)
ax.xaxis.set_label_coords(1.08, -0.1)
ax.set_ylabel(str(MeasurementType),labelpad=10,style='italic', fontsize=14)
#ax.yaxis.set_label_coords(-0.1, 0.5)
#plt.xlabel("Time",horizontalalignment='right', position=(1,60))\
#ax.set_title(str(MeasurementType),fontweight="bold", pad=20,fontsize=20)
rstButton.config(state=tk.NORMAL)
fig.tight_layout(rect=[0.163, 0.391, 0.905, 0.977])
plt.show()
编辑:因为有人告诉我我的问题不清楚,所以我提供了两个屏幕截图以更好地解释问题。这是上述代码的结果。此外,在左下角,您可以在窗口中看到 top、bottom、left、right 的值与我的代码中在 rect 元组中设置的值不同。
我想要的输出是这样的:
通过手动调整图形的参数来实现,直到达到满意的程度。正是从这里我提取了值并将它们放在 rect 元组中,但它不起作用。希望现在更清楚我想要实现的目标以及问题所在。
编辑 2:以下是建议解决方案的结果
fig,ax = plt.subplots(constrained_layout=True)
如您所见,两个轴的标签都没有正确放置。
【问题讨论】:
-
它真的完全不清楚你想要做什么,这不是一段可重现的代码。但是,您似乎不太可能希望 rect 为
[0.907, 0.392, 0.907, 0.977]。请阅读matplotlib.org/stable/api/_as_gen/… 了解 rect kwarg 的含义。也请考虑阅读matplotlib.org/stable/tutorials/intermediate/… 和matplotlib.org/stable/tutorials/intermediate/… -
@JodyKlymak 请查看我的编辑以获得更好的解释,希望它会更有帮助。至于 rect 元组,正如你所说,最初的元组是错误的,我将它们更改为我认为“应该”工作但不是。
-
是的,没错,没有任何东西会自动满足您的需求,您需要手动设置。为 ylabel 设置水平空间,而不是垂直空间,因为这会导致不可能的情况。您应该只使图形更大和/或使用更小的标签。我也不会坚持把“时间”放在最右边。如果您的观众不知道这些标签是时代,他们可能不会理解太多其他内容。
-
最后,就空间而言,您的时间刻度标签非常浪费。你可以考虑matplotlib.org/stable/gallery/ticks_and_spines/…
标签: python matplotlib plot figure