[英]Why does plt.hlines() adjust ylims when used with transform, but plt.plot() does not?
我正在嘗試使用plt.hlines()
和y=[0.2]
和transform=ax.get_xaxis_transform()
參數在 y 軸的 20% 處顯示一條水平線。 當我這樣做時,我的情節的上部 ylim 會自動更改為 0.2。
我不期望這種行為,因為據我了解get_xaxis_transform()
應該將y=[0.2]
值轉換為對應於我的 y 軸的 20% 的值。 特別是因為 hline 顯示為 20%,但無論如何都會調整限制。
當我使用plt.plot()
(最右邊的圖)執行相同的操作時,不會發生這種情況。 在這里,即使交互式移動可見區域,這條線也會保持在 y 軸的 20% 處。
我想知道為什么會發生這種情況,以及如何避免hlines()
在將其與 transform 參數一起使用時調整 ylims。
MWE:
import numpy as np
import matplotlib.pyplot as plt
y = np.random.rand(20)/100 # scale so all values should be far below 0.2
x = range(len(y))
# Scenario 0 (auto-scaled axes)
fig, (ax0, ax1, ax2) = plt.subplots(ncols=3)
ax0.plot(x, y)
ax0.set(title='No horizontal lines drawn')
# Scenario 1
ax1.hlines(y=[0.2], xmin=5, xmax=15, transform=ax1.get_xaxis_transform(), color='black')
ax1.plot(x, y)
ax1.set(title='Using hlines()')
# Scenario 2
ax2.plot(x, y)
ax2.plot([5, 15], [0.2, 0.2], transform=ax2.get_xaxis_transform(), color='black')
ax2.set(title='Using plot()')
plt.show(block=True)
這是因為 matplotlib 中的一個錯誤。 https://github.com/matplotlib/matplotlib/issues/23171
正如您所發現的,使用plt.plot()
是一種簡單的解決方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.