[英]Axis labels in line with tick labels in matplotlib
由於篇幅原因,我有時會按以下樣式制作圖:
fig, ax = plt.subplots(figsize=(3, 3))
ax.plot([0,1], [1000, 1001])
ax.set_xticks([0, 1])
ax.set_yticks([1000, 1001])
ax.set_xlabel("x", labelpad=-8)
ax.set_ylabel("y", labelpad=-18)
在這里,我只保留標記 X/Y 域邊界的刻度線,並且我使用labelpad
關鍵字參數手動對齊xlabel
和ylabel
,以便 x 和 y 軸標簽在視覺上與刻度標簽對齊。
請注意,我不得不為不同的軸使用不同數量的填充,因為 y 刻度標簽1000
和1001
的長度比 x 刻度標簽0
和1
的高度更遠離軸,並且由於垂直x 軸 label 的 position 和 y 軸 label 的水平 position 是相對於它們通常的 position 而言的,這將剛好超過刻度標簽的范圍。
我想知道,有沒有辦法使這個過程自動化,並且准確地而不是視覺地進行? 例如,如果labelpad
是相對於書脊的,那將非常好,或者如果有一種方法可以確定刻度和刻度標簽遠離書脊的范圍,那么該數字也可以用於自動執行此操作。
使用ax.yaxis.set_label_coords
可以獲得類似的效果,但這會相對於軸的變換轉換 position,因此取決於軸的大小,而刻度線相對於脊柱絕對定位。
你用ax.{x,y}axis.set_label_coords
的路幾乎就在那里! 您需要做的就是將transAxes
變換包裝在一個offset_copy
中,然后提供一個偏移量,該偏移量是刻度的當前長度 + 刻度 bbox 周圍的任何空間的組合。
import matplotlib.pyplot as plt
from matplotlib.transforms import offset_copy
fig, ax = plt.subplots(figsize=(3, 3))
fig.set_facecolor('white')
ax.plot([0,1], [1000, 1001])
ax.set_xticks([0, 1])
ax.set_yticks([1000, 1001])
# Create a transform that vertically offsets the label
# starting at the edge of the Axes and moving downwards
# according to the total length of the bounding box of a major tick
t = offset_copy(
ax.transAxes, y=-(ax.xaxis.get_tick_padding() + ax.xaxis.majorTicks[0].get_pad()),
fig=fig, units='dots'
)
ax.xaxis.set_label_coords(.5, 0, transform=t)
ax.set_xlabel('x', va='top')
# Repeat the above, but on the y-axis
t = offset_copy(
ax.transAxes, x=-(ax.yaxis.get_tick_padding() + ax.yaxis.majorTicks[0].get_pad()),
fig=fig, units='dots'
)
ax.yaxis.set_label_coords(0, .5, transform=t)
ax.set_ylabel('y', va='bottom')
import matplotlib.pyplot as plt
from matplotlib.transforms import offset_copy
fig, ax = plt.subplots(figsize=(3, 3))
fig.set_facecolor('white')
ax.plot([0,1], [1000, 1001])
ax.set_xticks([0, 1])
ax.set_yticks([1000, 1001])
ax.xaxis.set_tick_params(length=10)
ax.yaxis.set_tick_params(length=15)
t = offset_copy(
ax.transAxes, y=-(ax.xaxis.get_tick_padding() + ax.xaxis.majorTicks[0].get_pad()),
fig=fig, units='points'
)
ax.xaxis.set_label_coords(.5, 0, transform=t)
ax.set_xlabel('x', va='top')
t = offset_copy(
ax.transAxes, x=-(ax.yaxis.get_tick_padding() + ax.yaxis.majorTicks[0].get_pad()),
fig=fig, units='points'
)
ax.yaxis.set_label_coords(0, .5, transform=t)
ax.set_ylabel('y', va='bottom')
import matplotlib.pyplot as plt
from matplotlib.transforms import offset_copy
fig, ax = plt.subplots(figsize=(3, 3), dpi=150)
fig.set_facecolor('white')
ax.plot([0,1], [1000, 1001])
ax.set_xticks([0, 1])
ax.set_yticks([1000, 1001])
ax.xaxis.set_tick_params(length=10)
ax.yaxis.set_tick_params(length=15)
t = offset_copy(
ax.transAxes, y=-(ax.xaxis.get_tick_padding() + ax.xaxis.majorTicks[0].get_pad()),
fig=fig, units='points'
)
ax.xaxis.set_label_coords(.5, 0, transform=t)
ax.set_xlabel('x', va='top')
t = offset_copy(
ax.transAxes, x=-(ax.yaxis.get_tick_padding() + ax.yaxis.majorTicks[0].get_pad()),
fig=fig, units='points'
)
ax.yaxis.set_label_coords(0, .5, transform=t)
ax.set_ylabel("y", va='bottom')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.