如何在Matplotlib中绘制模糊点 [英] How to plot blurred points in Matplotlib

查看:190
本文介绍了如何在Matplotlib中绘制模糊点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如问题所述,我正在寻找一种使用Matplotlib绘制模糊点的方法. 我不想绘制一组点,然后应用滤镜使整个图像模糊.取而代之的是,我想绘制一组点,每个点都具有关联的模糊度.

As the question says, I'm looking for a way to plot blurred points using Matplotlib. I don't want to plot a set of points and then apply a filter to blurry the whole image. Instead of it, I would like to plot a set of points, each of them with an associated level of blur.

先谢谢您.

推荐答案

这是另一种解决方法.您可以使用BboxImage在每个位置显示图像而不是标记.这样,您可以根据需要以任何方式模糊或处理图像.本教程提供了有关 BboxImages 的更多信息.

Here's another work around. You can display an image at each location instead of a marker using a BboxImage. That way you can blur or manipulate the image any way you want. This tutorial has more about BboxImages.

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

# Create and save an image with just a marker in it
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
ax1.plot(0.5,0.5,'*',ms=200)
ax1.set_ylim(0,1)
ax1.set_xlim(0,1)
plt.axis('off')
fig1.savefig('marker.png')

# Read in the same marker image
marker = plt.imread('marker.png')

# New figure and data
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)

# Blur the marker and image plot the blurred image at each data point. 
for xi, yi, sigmai in zip(x,y,sigma):
    markerBlur = ndimage.gaussian_filter(marker,sigmai) # Blur the marker image

    # Create an BboxImage for the blurred marker and add it to the plot. 
    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()

这篇关于如何在Matplotlib中绘制模糊点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆