如何在 Matplotlib 中繪製模糊的點?


要在 matplotlib 中繪製模糊的點,我們可以採取以下步驟 −

  • 設定影像大小,調整子圖之間和周圍的填充。

  • 建立新的影像或啟用一個現有的影像。

  • ax1 新增到影像中作為子圖排列的一部分。

  • 首先,我們可以製作一個標記,即模糊的標記。

  • 設定 X 和 Y 軸刻度,關閉軸。

  • 將標記儲存在檔案中,在模糊後加載該影像以便繪製。

  • 關閉之前的影像,fig1

  • 建立新的影像或啟用一個現有的影像,fig2

  • 建立隨機資料點,x 和 y。

  • 應用高斯濾波器來產生模糊,將該藝術品新增到當前的軸上。

  • ax2 上設定 X 和 Y 軸刻度。

  • 要顯示影像,使用 show() 方法。

示例

import matplotlib.pyplot as plt
from scipy import ndimage
from matplotlib.image import BboxImage
from matplotlib.transforms import Bbox, TransformedBbox
import numpy as np

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
ax1.plot(0.5, 0.5, 'd', ms=200)
ax1.set_ylim(0, 1)
ax1.set_xlim(0, 1)
plt.axis('off')
fig1.savefig('marker.png')

marker = plt.imread('marker.png')
plt.close(fig1)

fig2 = plt.figure()
ax2 = fig2.add_subplot(111)

x = 8 * np.random.rand(10) + 1
y = 8 * np.random.rand(10) + 1

sigma = np.arange(10, 60, 5)

for xi, yi, sigmai in zip(x, y, sigma):
   markerBlur = ndimage.gaussian_filter(marker, sigmai)
   bb = Bbox.from_bounds(xi, yi, 1, 1)
   bb2 = TransformedBbox(bb, ax2.transData)
   bbox_image = BboxImage(bb2,norm=None,origin=None, clip_on=False)
   bbox_image.set_data(markerBlur)
   ax2.add_artist(bbox_image)

ax2.set_xlim(0, 10)
ax2.set_ylim(0, 10)

plt.show()

輸出

更新於: 2021 年 8 月 9 日

772 次瀏覽

開啟你的職業生涯

完成課程認證

開始
廣告
© . All rights reserved.