python - PyQt matplotlib 在图片顶部绘制轮廓数据 - 缩放问题

标签 python matplotlib pyqt python-imaging-library

我正在尝试将照片作为 matplotlib 中的背景。我设法添加了照片,其尺寸为 273 x 272 像素。然后我添加一个 30 x 30 大的等高线图。如果我注释掉绘制照片的线,等高线图将覆盖整个绘图区域。

如果我包含照片,等值线图将出现在左下角。它看起来很像沿着每个轴绘制在整个 Canvas 的大约 30/272 的部分上。我想要的是让等高线图覆盖整张照片。

这些是代码的相关部分(不是完整的工作示例):

# Matplotlib Figure object
from matplotlib.figure import Figure

# import the Qt4Agg FigureCanvas object, that binds Figure to
# Qt4Agg backend. It also inherits from QWidget
from matplotlib.backends.backend_qt4agg \
import FigureCanvasQTAgg as FigureCanvas

from PIL import Image

.....

class Qt4ContourCanvas(FigureCanvas):
    def __init__(self, Z_matrix, plot_freq, p2_freq, p2_power, ws_level, p2_patch_on, pmin, pmax, my_alpha, parent=None):

        global p2_frequency
        logger.debug("%s - created" % self.__class__.__name__)
        self.fig = Figure(facecolor='Lavender')
        self.axes = self.fig.add_subplot(111)

        #Reduce the size of the borders
        #http://stackoverflow.com/questions/1203639/how-do-i-limit-the-border-size-on-a-matplotlib-graph
        self.fig.subplots_adjust(left=0.05, bottom=0.05, right=0.95, top=1-0.05,
                wspace=0.01, hspace=0.01) 

        # We need to keep a class variable of Z to prevent it going out of scope
        self.Z = Z_matrix

............

    def drawContourPlot(self, Z_matrix, plot_freq, p2_freq, p2_power, ws_level, p2_patch_on, pmin, pmax, my_alpha):
        "Method to plot contour plots"
        global p2_frequency
        p2_frequency = p2_freq
        self.axes.cla()

        self.Z = Z_matrix


        map_dimensions = Z_matrix.shape
        my_xdim = map_dimensions[0]
        my_ydim = map_dimensions[1]

        levels = np.arange(pmin, pmax, 2.5)

        DIM = len(self.Z)
        x = y = np.arange(0, DIM, 1)
        X, Y = np.meshgrid(x, y)

        my_cm = ListedColormap(faramir_cm)

        # Background picture
        picture = Image.open('gondor.png')
        CSbkgr = self.axes.imshow(picture, origin='lower')


        # Swap X and Y to transpose the data, otherwise the click event
        # and the matrix coordinates do not agree
        CS = self.axes.contourf(Y, X, self.Z, levels, cmap=my_cm, alpha=my_alpha)

        CS2 = self.axes.contour(CS, levels=CS.levels, colors = 'r', hold='on')
        self.axes.clabel(CS2, fontsize=10, inline=1, fmt='%1.1f')

        CS3 = self.axes.contour(CS, levels=[ws_level], colors = 'black', hold='on', linestyles = 'solid', linewidths = 2)

        self.axes.clabel(CS3, fontsize=12, inline=1, fmt='%1.1f')

        self.axes.grid(True, color='white')

        self.fig.canvas.draw()

最佳答案

您可以重新调整等高线图的比例,使其正确拟合: enter image description here 而不是(左下角的彩色点是未缩放的等高线图...): enter image description here 代码:

import Image
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

#contour plot test data:
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)

plt.figure()
im = Image.open('tree_small.png')
plt.imshow(im, origin='lower')

#rescale contour plot:
X = X - np.min(X)
X = X * im.size[0] / np.max(X)
Y = Y - np.min(Y)
Y = Y * im.size[1] / np.max(Y)
plt.contour(X, Y, Z, 20)

plt.show()

也许您可以使用单独的轴将等值线图叠加在顶部,但这似乎是最快的方法;)

关于python - PyQt matplotlib 在图片顶部绘制轮廓数据 - 缩放问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10850882/

相关文章:

Python:在拟合模型上绘制残差

c++ - 3 个面板的 Qt 布局,全部垂直扩展以填充

python - 如何从 PyQt 应用程序中完全控制 Option/Alt 键?

python - 面向初学者的关于 OSX 上的 Python 包管理的常识性建议

python - 在python中导入图像

Python:无法从不同模块/函数访问类属性

python - 带有 QSizePolicy.Expanding 的 setSizePolicy() 不起作用 : the child does not expand to the size of the parent

Python spy 程序: changing a function in another file takes no effect

python - 绘制 f(x,y) = z = min(x,y) 创建 2D 三角形而不是 3D 曲面

Python matplotlib : Add legend for a colormap