简体   繁体   English

如何在Matplotlib中绘制模糊点

[英]How to plot blurred points in Matplotlib

As the question says, I'm looking for a way to plot blurred points using Matplotlib. 正如问题所说,我正在寻找一种使用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. 而不是它,我想绘制一组点,每个点都有一个相关的模糊水平。

Thank you in advance. 先感谢您。

Here's another work around. 这是另一项工作。 You can display an image at each location instead of a marker using a BboxImage . 您可以使用BboxImage在每个位置显示图像而不是标记。 That way you can blur or manipulate the image any way you want. 这样,您可以以任何方式模糊或操纵图像。 This tutorial has more about BboxImages . 本教程有更多关于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()

模糊的标记图

When you cannot make it, fake it. 当你无法做到时,假装它。

import matplotlib.pyplot as plt
import numpy as np

# some random data
x = np.random.random(100)
y = np.random.random(100)
z = np.random.random(100)

# z reflects the amount of defocus at each dot
# if z=0, the point is small (1 pt)
# if z=1, the point is large (50 pt)
# each dot is composed of different layers
fig = plt.figure()
ax = fig.add_subplot(111)
for i in np.arange(.1,1.01,.1):
    ax.scatter(x, y, s=(50*i*(z*.9+.1))**2, color=(0,0,0,.5/i/10))

This gives: 这给出了:

在此输入图像描述

This is by no means perfect, but something along these lines might suffice your needs. 这绝不是完美的,但这些方面的东西可能足以满足您的需求。 Things to consider: 需要考虑的事项:

  • the point size is now in absolute units, it does not scale (requires more maths for scaling) 点大小现在是绝对单位,它不会缩放(需要更多数学进行缩放)
  • if you want to have an equal amount of ink in each point, you will have to decrease the alpha value for larger blobs 如果你想在每个点上有相同数量的墨水,你将不得不减少较大斑点的alpha值
  • do you want to have the blur diameter reflect the value (as here) or the blur area? 你想让模糊直径反映值(如此处)还是模糊区域?
  • real "blur" is usually Gaussian, this is not; 真正的“模糊”通常是高斯,这不是; this can be made, but then the size and alpha scaling become a bit longer 这可以做,但随后大小和alpha缩放变得有点长
  • what do you want to see happen when blurred points overlap each other? 当模糊点相互重叠时,你想看到什么?
  • when doing maths with alpha values and color values, remember the gamma function of the display 在使用alpha值和颜色值进行数学运算时,请记住显示器的gamma功能

So, this is just an ugly fake. 所以,这只是一个丑陋的假货。 Sometimes they look good enough, sometimes not. 有时他们看起来不够好,有时候不行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM