【问题标题】:Matplotlib using image for points on plotMatplotlib 使用图像作为绘图点
【发布时间】:2017-10-28 03:48:15
【问题描述】:

我有以下 matplotlib 脚本。我想用图像替换绘图上的点。假设红色点为“red.png”,蓝色点为“blue.png”。如何调整以下内容以绘制这些图像而不是默认点?

from scipy import linalg
import numpy as np
import pylab as pl
import matplotlib as mpl
import matplotlib.image as image

from sklearn.qda import QDA

###############################################################################
# load sample dataset
from sklearn.datasets import load_iris

iris = load_iris()
X = iris.data[:, 0:2]  # Take only 2 dimensions
y = iris.target
X = X[y > 0]
y = y[y > 0]
y -= 1
target_names = iris.target_names[1:]

###############################################################################
# QDA
qda = QDA()
y_pred = qda.fit(X, y, store_covariances=True).predict(X)


###############################################################################
# Plot results

# constants
dpi = 72; imageSize = (32,32)
# read in our png file
im_red = image.imread('red.png')
im_blue = image.imread('blue.png')

def plot_ellipse(splot, mean, cov, color):
    v, w = linalg.eigh(cov)
    u = w[0] / linalg.norm(w[0])
    angle = np.arctan(u[1] / u[0])
    angle = 180 * angle / np.pi  # convert to degrees
    # filled gaussian at 2 standard deviation
    ell = mpl.patches.Ellipse(mean, 2 * v[0] ** 0.5, 2 * v[1] ** 0.5,
                                            180 + angle, color=color)
    ell.set_clip_box(splot.bbox)
    ell.set_alpha(0.5)
    splot.add_artist(ell)

xx, yy = np.meshgrid(np.linspace(4, 8.5, 200), np.linspace(1.5, 4.5, 200))
X_grid = np.c_[xx.ravel(), yy.ravel()]
zz_qda = qda.predict_proba(X_grid)[:, 1].reshape(xx.shape)

pl.figure()
splot = pl.subplot(1, 1, 1)
pl.contourf(xx, yy, zz_qda > 0.5, alpha=0.5)
pl.scatter(X[y == 0, 0], X[y == 0, 1], c='b', label=target_names[0])
pl.scatter(X[y == 1, 0], X[y == 1, 1], c='r', label=target_names[1])
pl.contour(xx, yy, zz_qda, [0.5], linewidths=2., colors='k')
print(xx)
pl.axis('tight')
pl.show()

【问题讨论】:

    标签: matplotlib


    【解决方案1】:

    您可以使用BboxImage 在图形中绘制图像而不是标记,如本教程中所述。

    from matplotlib import pyplot as plt
    from matplotlib.image import BboxImage
    from matplotlib.transforms import Bbox, TransformedBbox
    
    # Load images.
    redMarker = plt.imread('red.jpg')
    blueMarker = plt.imread('blue.jpg')
    
    # Data
    blueX = [1, 2, 3, 4]
    blueY = [1, 3, 5, 2]
    redX = [1, 2, 3, 4]
    redY = [3, 2, 3, 4]
    
    # Create figure
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    # Plots an image at each x and y location. 
    def plotImage(xData, yData, im):
        for x, y in zip(xData, yData):
            bb = Bbox.from_bounds(x,y,1,1)  
            bb2 = TransformedBbox(bb,ax.transData)
            bbox_image = BboxImage(bb2,
                                norm = None,
                                origin=None,
                                clip_on=False)
    
            bbox_image.set_data(im)
            ax.add_artist(bbox_image)
    
    
    plotImage(blueX, blueY, blueMarker)
    plotImage(redX, redY, redMarker)
    
    # Set the x and y limits
    ax.set_ylim(0,6)
    ax.set_xlim(0,6)
    
    plt.show()
    

    【讨论】:

    • 我的图片出现了奇怪的歪斜:(
    • 对于其他遇到图像变形的情况,您可以调整bb = Bbox.from_bounds(x,y,1,1)这一行中的两个1,例如调整为bb = Bbox.from_bounds(x,y,1,2),以使图像的高度是宽度的两倍。
    猜你喜欢
    • 2021-02-27
    • 2021-01-31
    • 2021-10-14
    • 2020-06-12
    • 2018-06-23
    • 2021-03-07
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多