Matplotlib使用绘图点的图像

Matplotlib使用绘图点的图像,matplotlib,Matplotlib,我有以下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 ###############

我有以下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()

可以使用本教程中的方法在地物中打印图像而不是标记

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()

您可以使用本教程中的那样在图形中打印图像而不是标记

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()

我的图像以奇怪的倾斜结束:(对于其他经历图像变形的人,您可以调整此行中的两个1
bb=Bbox。从边界(x,y,1,1)
,例如到
bb=Bbox。从边界(x,y,1,2)
,使图像的宽度增加一倍。我的图像以奇怪的倾斜结束:(对于其他正在经历图像变形的用户,您可以调整此行中的两个1,例如,将
bb=Bbox.from_bounds(x,y,1,1)
调整为
bb=Bbox.from_bounds(x,y,1,2)
,以使图像的高度达到宽度的两倍。