簡體   English   中英

Matplotlib藝術家在放大時保持相同的大小?

[英]Matplotlib artists to stay the same size when zoomed in?

我正在使用matplotlib在圖表上繪制一些藝術家(特別是幾個矩形)。 我想以某種方式錨定這些,以便無論使用什么縮放級別它們都保持相同的大小。 我已經使用谷歌搜索,我已閱讀大部分藝術家文檔,並沒有運氣找到我需要錨定矩形大小的功能。

我喜歡詳細說明如何執行此功能的答案,但如果你能讓我大致知道如何做到這一點,或者甚至給我一些關鍵詞讓我的谷歌搜索更有效,我真的很感激:)

謝謝!

只需將transform=ax.transAxes關鍵字應用於PolygonRectangle實例即可。 如果將補丁錨定到圖形而不是軸上更有意義,也可以使用transFigure 這是關於變換的教程

以下是一些示例代碼:

from matplotlib import pyplot as plt
from matplotlib.patches import Polygon
import numpy as np
x = np.linspace(0,5,100)
y = np.sin(x)

plt.plot(x,y)
ax = plt.gca()

polygon = Polygon([[.1,.1],[.3,.2],[.2,.3]], True, transform=ax.transAxes)
ax.add_patch(polygon)

plt.show()

如果您不想使用軸坐標系放置多邊形,而是希望使用數據坐標系定位多邊形,則可以使用變換在定位之前靜態轉換數據。 這里最好的例子:

from matplotlib import pyplot as plt
from matplotlib.patches import Polygon
import numpy as np

x = np.linspace(0,5,100)
y = np.sin(x)

plt.plot(x,y)
ax = plt.gca()

dta_pts = [[.5,-.75],[1.5,-.6],[1,-.4]]

# coordinates converters:
#ax_to_display = ax.transAxes.transform
display_to_ax = ax.transAxes.inverted().transform
data_to_display = ax.transData.transform
#display_to_data = ax.transData.inverted().transform

ax_pts = display_to_ax(data_to_display(dta_pts))

# this triangle will move with the plot
ax.add_patch(Polygon(dta_pts, True)) 
# this triangle will stay put relative to the axes bounds
ax.add_patch(Polygon(ax_pts, True, transform=ax.transAxes))

plt.show()

暫無
暫無

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

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