簡體   English   中英

Matplotlib tight_layout 設置矩形元組

[英]Matplotlib tight_layout set rect tuple

我創建了以下代碼,它打印 plot 並以對我有用的方式格式化軸標簽和刻度。 我遇到了tight_layout 的問題,它使我的垂直旋轉的x 軸刻度標簽以及x 軸label 在圖形window 之外。

為了嘗試解決這個問題,我所做的是手動重新調整 plot window,並從圖 window 手動設置矩形元組。 經過一些嘗試,我發現(左、下、右、上)的最佳值在我的情況下是 [0.163, 0.391, 0.905, 0.977]。 接下來,我認為我應該將其合並到我的代碼中,以便我的繪圖首先以正確的大小出現:為此,我使用了以下命令:

fig.tight_layout(rect=[0.163, 0.391, 0.905, 0.977])

但是,它不起作用,並且該圖形出現的矩形值與我設置的值不同。

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

編輯:因為有人告訴我我的問題不清楚,所以我提供了兩個屏幕截圖以更好地解釋問題。 這是上述代碼的結果。 此外,在左下角,您可以在 window 上看到,頂部、底部、左側、右側的值與我的代碼中在 rect 元組中設置的值不同。

在此處輸入圖像描述

我想要的 output 是這樣的:

在此處輸入圖像描述

它是通過手動調整圖形的參數來實現的,直到達到令人滿意的程度。 正是從這里我提取了值並將它們放在 rect 元組中,但它不起作用。 希望現在更清楚我想要實現什么,以及問題是什么。

編輯2:這是建議解決方案的結果

fig,ax = plt.subplots(constrained_layout=True)

如您所見,兩個軸的標簽都沒有正確放置。

在此處輸入圖像描述

嘗試:

fig, ax = plt.subplots(constrained_layout=True)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM