日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

matplotlib 高阶之Transformations Tutorial

發(fā)布時(shí)間:2024/4/15 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 matplotlib 高阶之Transformations Tutorial 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

  • Data coordinates
  • Axes coordinates
  • Blended transformations 混合坐標(biāo)系統(tǒng)
  • plotting in physical units
  • 使用offset transforms 創(chuàng)建陰影效果
  • 函數(shù)鏈接

之前在legend的使用中,便已經(jīng)提及了transforms,用來轉(zhuǎn)換參考系,一般情況下,我們不會(huì)用到這個(gè),但是還是了解一下比較好

坐標(biāo)轉(zhuǎn)換對(duì)象描述
"data"ax.transData數(shù)據(jù)的坐標(biāo)系統(tǒng),通過xlim, ylim來控制
"axes"ax.transAxesAxes的坐標(biāo)系統(tǒng),(0, 0)代表左下角,(1, 1)代表右上角
"figure"fig.transFigureFigure的坐標(biāo)系統(tǒng),(0, 0)代表左下角,(1, 1)代表右上角
"figure-inches"fig.dpi_scale_trans以inches來表示的Figure坐標(biāo)系統(tǒng),(0, 0)左下角,而(width, height)表示右上角
"display"None or IdentityTransform()顯示窗口的像素坐標(biāo)系統(tǒng),(0, 0)表示窗口的左下角,而(width, height)表示窗口的右上角
"xaxis", "yaxis"ax.get_xaxis_transform(), ax.get_yaxis_transform()混合坐標(biāo)系; 在另一個(gè)軸和軸坐標(biāo)之一上使用數(shù)據(jù)坐標(biāo)。沒看懂

Data coordinates

最為常見的便是通過set_xlim, 和set_ylim來控制數(shù)據(jù)坐標(biāo)

import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as mpatchesx = np.arange(0, 10, 0.005) y = np.exp(-x/2.) * np.sin(2*np.pi*x)fig, ax = plt.subplots() ax.plot(x, y) ax.set_xlim(0, 10) ax.set_ylim(-1, 1)plt.show()

你可以通過ax.transData來將你的數(shù)據(jù)坐標(biāo),轉(zhuǎn)換成再顯示窗口上的像素坐標(biāo),單個(gè)坐標(biāo),或者傳入序列都是被允許的

type(ax.transData) matplotlib.transforms.CompositeGenericTransform ax.transData.transform((5, 0)) #數(shù)據(jù)坐標(biāo)(5, 0) 轉(zhuǎn)換為顯示窗口的像素坐標(biāo)(221.4, 144.72) 這個(gè)玩意兒不一定的 array([221.4 , 144.72]) ax.transData.transform(((5, 0), (2, 3))) array([[221.4 , 144.72],[120.96, 470.88]])

你也可以通過使用inverted()來反轉(zhuǎn),獲得數(shù)據(jù)坐標(biāo)

inv = ax.transData.inverted() type(inv) matplotlib.transforms.CompositeGenericTransform inv.transform((221.4, 144.72)) array([5., 0.])

下面是一個(gè)比較完整的例子

x = np.arange(0, 10, 0.005) y = np.exp(-x/2.) * np.sin(2*np.pi*x)fig, ax = plt.subplots() ax.plot(x, y) ax.set_xlim(0, 10) ax.set_ylim(-1, 1)xdata, ydata = 5, 0 xdisplay, ydisplay = ax.transData.transform_point((xdata, ydata))bbox = dict(boxstyle="round", fc="0.8") arrowprops = dict(arrowstyle="->",connectionstyle="angle,angleA=0,angleB=90,rad=10")offset = 72 ax.annotate('data = (%.1f, %.1f)' % (xdata, ydata),(xdata, ydata), xytext=(-2*offset, offset), textcoords='offset points',bbox=bbox, arrowprops=arrowprops)disp = ax.annotate('display = (%.1f, %.1f)' % (xdisplay, ydisplay),(xdisplay, ydisplay), xytext=(0.5*offset, -offset), #xytext 好像是text離前面點(diǎn)的距離xycoords='figure pixels', #這個(gè)屬性來變換坐標(biāo)系textcoords='offset points',bbox=bbox, arrowprops=arrowprops)plt.show()

很顯然的一點(diǎn)是,當(dāng)我們改變xlim, ylim的時(shí)候,同樣的數(shù)據(jù)點(diǎn)轉(zhuǎn)換成顯示窗口后發(fā)生變化

ax.transData.transform((5, 0)) array([221.4 , 144.72]) ax.set_ylim(-1, 2) (-1, 2) ax.transData.transform((5, 0)) array([221.4 , 108.48]) ax.set_xlim(10, 20) (10, 20) ax.transData.transform((5, 0)) array([-113.4 , 108.48])

Axes coordinates

除了數(shù)據(jù)坐標(biāo)系,Axes坐標(biāo)系是第二常用的,就像在上表中提到的(0, 0)表示左下角,而(1, 1)表示右上角,(0.5, 0.5)則表示中心。我們也可以過分一點(diǎn),使用(-0.1, 1.1)會(huì)顯示在axes的外圍左上角部分。

fig = plt.figure() for i, label in enumerate(('A', 'B', 'C', 'D')):ax = fig.add_subplot(2, 2, i+1)ax.text(0.05, 0.95, label, transform=ax.transAxes,fontsize=16, fontweight='bold', va='top')plt.show()

從上面的例子中我們可以看到,想在多個(gè)axes中相同的位置放置相似的東西,用ax.transAxes時(shí)非常方便的

fig, ax = plt.subplots() x, y = 10*np.random.rand(2, 1000) ax.plot(x, y, 'go', alpha=0.2) # plot some data in data coordinatescirc = mpatches.Circle((0.5, 0.5), 0.25, transform=ax.transAxes,facecolor='blue', alpha=0.75) ax.add_patch(circ) plt.show()

可以看到,上面的橢圓與數(shù)據(jù)坐標(biāo)無關(guān),始終放置在中間

Blended transformations 混合坐標(biāo)系統(tǒng)

import matplotlib.transforms as transformsfig, ax = plt.subplots() x = np.random.randn(1000)ax.hist(x, 30) ax.set_title(r'$\sigma=1 \/ \dots \/ \sigma=2$', fontsize=16)# the x coords of this transformation are data, and the # y coord are axes trans = transforms.blended_transform_factory(ax.transData, ax.transAxes)# highlight the 1..2 stddev region with a span. # We want x to be in data coordinates and y to # span from 0..1 in axes coords rect = mpatches.Rectangle((1, 0), width=1, height=1,transform=trans, color='yellow',alpha=0.5)ax.add_patch(rect)plt.show()

注意到,上面我們使用了混合坐標(biāo)系統(tǒng),x軸方向是數(shù)據(jù)坐標(biāo)系,而y軸方向是axes的坐標(biāo)系統(tǒng),我們做一個(gè)反轉(zhuǎn)試試

import matplotlib.transforms as transformsfig, ax = plt.subplots() x = np.random.randn(1000)ax.hist(x, 30) ax.set_title(r'$\sigma=1 \/ \dots \/ \sigma=2$', fontsize=16)# the x coords of this transformation are data, and the # y coord are axes trans = transforms.blended_transform_factory(ax.transAxes, ax.transData) #調(diào)了一下# highlight the 1..2 stddev region with a span. # We want x to be in data coordinates and y to # span from 0..1 in axes coords rect = mpatches.Rectangle((0.5, 0), width=0.5, height=50, #注意這里的區(qū)別transform=trans, color='yellow',alpha=0.5)ax.add_patch(rect)plt.show()

plotting in physical units

fig, ax = plt.subplots(figsize=(5, 4)) x, y = 10*np.random.rand(2, 1000) ax.plot(x, y*10., 'go', alpha=0.2) # plot some data in data coordinates # add a circle in fixed-units circ = mpatches.Circle((2.5, 2), 1.0, transform=fig.dpi_scale_trans,facecolor='blue', alpha=0.75) ax.add_patch(circ) plt.show()

上面的圓使用了transform=fig.dpi_scale_trans坐標(biāo)系統(tǒng),其圓心為(2.5, 2),半徑為1,顯然這些都是以figsize為基準(zhǔn)的,所以,這個(gè)圓會(huì)在圖片中心,如果我們變換figsize,圖片的位置(顯示位置)會(huì)發(fā)生變化

fig, ax = plt.subplots(figsize=(7, 2)) x, y = 10*np.random.rand(2, 1000) ax.plot(x, y*10., 'go', alpha=0.2) # plot some data in data coordinates # add a circle in fixed-units circ = mpatches.Circle((2.5, 2), 1.0, transform=fig.dpi_scale_trans,facecolor='blue', alpha=0.75) ax.add_patch(circ) plt.show()

再來看一個(gè)有趣的例子,雖然我不知道改如何解釋

fig, ax = plt.subplots() xdata, ydata = (0.2, 0.7), (0.5, 0.5) ax.plot(xdata, ydata, "o") ax.set_xlim((0, 1))trans = (fig.dpi_scale_trans +transforms.ScaledTranslation(xdata[0], ydata[0], ax.transData))# plot an ellipse around the point that is 150 x 130 points in diameter... circle = mpatches.Ellipse((0, 0), 150/72, 130/72, angle=40,fill=None, transform=trans) ax.add_patch(circle) plt.show()

注意上面trans后面有個(gè)+號(hào),這個(gè)表示,顯示用dpi_scale_trans,即在圖片(0, 0)也就是左下角位置畫一個(gè)大小合適的橢圓,然后將這個(gè)橢圓移動(dòng)到(x[data][0], y[data][0])位置處,感覺實(shí)現(xiàn)是橢圓上點(diǎn)每個(gè)都加上(xdata[0], ydata[0])

使用offset transforms 創(chuàng)建陰影效果

fig, ax = plt.subplots()# make a simple sine wave x = np.arange(0., 2., 0.01) y = np.sin(2*np.pi*x) line, = ax.plot(x, y, lw=3, color='blue')# shift the object over 2 points, and down 2 points dx, dy = 2/72., -2/72. offset = transforms.ScaledTranslation(dx, dy, fig.dpi_scale_trans) shadow_transform = ax.transData + offset# now plot the same data with our offset transform; # use the zorder to make sure we are below the line ax.plot(x, y, lw=3, color='gray',transform=shadow_transform,zorder=0.5*line.get_zorder())ax.set_title('creating a shadow effect with an offset transform') plt.show()

函數(shù)鏈接

matplotlib.transforms

inverted()-轉(zhuǎn)回來

ScaledTranslation

轉(zhuǎn)載于:https://www.cnblogs.com/MTandHJ/p/10957744.html

總結(jié)

以上是生活随笔為你收集整理的matplotlib 高阶之Transformations Tutorial的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。