网格线出现在Qt中matplotlib中的三维条形图顶部?

2024-10-05 12:45:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经建立了一个程序,它可以根据直接输出到Matplotlib的输入数据成功地创建格式正确的三维直方图。然而,当我最近尝试将相同的代码输出到Qt Designer中的MatplotlibWidget时,网格线总是出现在3D绘图的前面,除非我将它们从图形中完全删除。在

良好的直方图:
Histogram with well-behaved gridlines
错误的直方图:
Histogram with unruly gridlines

两者使用的代码基本相同。我尝试过改变绘制不同元素的顺序,尝试过set_axisbelow(),但没有任何帮助,也尝试过更改zorder,它没有任何明显的影响。唯一能起作用的是网格(b=False),它完全关闭了线条,但是我更愿意保留它们,因为没有它们的三维图形很难获得精确的值。在

以下是违规代码:

def plotHist(self, xVals, yVals, zVals, cVals, targetWidget):

    targetWidget.canvas.ax.clear()
    targetWidget.canvas.ax.grid(b=True, zorder=2)

    x = np.array(xVals[1:], dtype=np.float) 
    y = np.array(yVals[1:], dtype=np.float)        

    if np.max(x) !=0.0:
        x = x[np.nonzero(x)]
        y = y[np.nonzero(y)]

        histPlot = targetWidget.canvas.ax

        hist, xedges, yedges = np.histogram2d(x, y, bins=self.binNum)

        hist = np.rot90(hist, k=3)
        hist = np.fliplr(hist)
        elements = (len(xedges) - 1) * (len(yedges) - 1)

        xpos, ypos = np.meshgrid(xedges[:-1], yedges[:-1])

        xpos = xpos.flatten()
        ypos = ypos.flatten()
        zpos = np.zeros(elements)

        xwidth = abs(xedges[0]-xedges[1])
        ywidth = abs(yedges[0]-yedges[1])

        dx = xwidth * np.ones_like(zpos)
        dy = ywidth * np.ones_like(zpos)
        dz = hist.flatten()

        xbottom = xedges[0] - xwidth*self.histPad
        xtop = xedges[-1] + xwidth*self.histPad
        ybottom = yedges[0] - ywidth*self.histPad
        ytop = yedges[-1] + ywidth*self.histPad

        histPlot.bar3d(xpos, ypos, zpos, dx, dy, dz, color='Tomato', alpha=0.8, zsort='max',zorder=1)

        histPlot.set_xlim(xbottom, xtop)
        histPlot.set_ylim(ybottom, ytop)
        histPlot.set_xticks(xedges)                        
        histPlot.set_yticks(yedges)
        histPlot.ticklabel_format(scilimits=(-3,2))
        histPlot.tick_params(labelsize='small')

    targetWidget.canvas.fig.tight_layout(pad=1, h_pad=None, w_pad=None, rect=(0,0,1,.95))
    targetWidget.canvas.draw()            

在用于与Qt Designer集成的mplwidget文件中,我初始化画布如下:

^{pr2}$

如果有用的话,我可以发布已知的好的代码段,但是我现在怀疑bar3d的一些已知错误正在表现出来——尽管我很想知道其他情况。在

如果还有其他有用的代码片段,我很乐意提供,但我不确定如何削减它,因为代码在独立状态下完全是功能性的,并且只在与Qt集成时显示问题,这很难简洁地捕捉到。在


Tags: 代码selfnphistcanvassetyedgesxedges

热门问题