【问题标题】:Matplotlib FigureCanvas: Correctly plot Quadmesh into axesMatplotlib FigureCanvas:正确地将 Quadmesh 绘制成轴
【发布时间】:2015-07-03 17:27:45
【问题描述】:

我将 Qt5 后端用于 matplotlib,并尝试在 FigureCanvas 中嵌入绘图。现在我在将带有 pcolormesh 的大型矩阵绘制到图形的轴对象中时遇到了问题。不知何故,生成的 QuadMesh 不会填充整个轴,而是在网格上方和右侧留下空间。

第二个问题是 x 轴标签没有旋转,尽管我调用了 autofmt_xdata()。以下是相关代码:

class MyCanvas(FigureCanvas):
    def __init__(self, parent=None):
        self.figure = Figure()
        self.figure.set_tight_layout(True)
        self.matrix_axes = self.figure.add_subplot(111)
        divider = make_axes_locatable(self.matrix_axes)
        self.color_axes = divider.append_axes("right", size=0.1, pad=0.05)
        self.matrix_axes.hold(False)
        self.color_axes.hold(False)

    def build_connectivity_matrix(self, source_neurons, target_neurons):
        # plot the data,
        # data is a 2d numpy array
        color_mesh = self.matrix_axes.pcolormesh(data, cmap=color_map)
        # add tick labels
        self.matrix_axes.set_yticklabels(labels_y)
        self.matrix_axes.set_xticklabels(labels_x)
        self.figure.autofmt_xdate(rotation=30)  # rotate x axis labels to fit more
        # plot color bar
        colorbar = self.figure.colorbar(color_mesh, cax=self.color_axes, orientation='vertical')

canvas = MyCanvas()
canvas.build_connectivity_matrix()

有什么想法吗?

【问题讨论】:

    标签: python matplotlib pyqt5


    【解决方案1】:

    在这种情况下,我也遇到了空格问题。在完成颜色网格后尝试添加:

    self.matrix_axes.set_xlim(xmax = data.shape[1])
    self.matrix_axes.set_ylim(ymax = data.shape[0])
    

    编辑以添加 x 平方的答案:

    您可以使用self.matrix_axes.set_xticklabels(labels_x, rotation=30)来设置文字的旋转。

    【讨论】:

    • 做到了!对 x 标签的其他问题有任何想法吗?
    • 我发现了标签。除了使用额外的调用:self.figure.autofmt_xdate(rotation=30),我可以将所需的旋转添加到 set_xticklabels 调用,如下所示:self.matrix_axes.set_xticklabels(labels_x, rotation=30)。请在您的答案中添加此信息,以便它完整并且我可以接受。谢谢!
    • 有趣。我想过这么说,但我认为如果 autofmt_xdate 不起作用,那也不会。我没有 Qt5,所以我无法测试您的代码以查看哪些有效,哪些无效。
    • 好吧,谁懂matplotlib? :)
    猜你喜欢
    • 2014-06-28
    • 2021-11-01
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多