【问题标题】:Matplotlib subplot axesMatplotlib 子图轴
【发布时间】:2023-03-12 00:27:01
【问题描述】:

感谢您对此问题的任何帮助。我正在尝试使用 matplotlib 制作子图,我编写的代码如下:

import networkx as nx

Fig, Axes = plt.subplots(nrows=1, ncols=2)
plt.tight_layout()
for i in range(0, NoOfVehicles):
  Axes[i].set_aspect(1)
  Axes[i].xaxis.set_major_formatter(mtick.NullFormatter())
  Axes[i].yaxis.set_major_formatter(mtick.NullFormatter()

现在我如何在第一个情节中绘制一些东西,然后在第二个情节中绘制其他东西。

我想做

nx.drawing.nx_pylab.draw_networkx_nodes(GPlot[0].G, GPlot[0].Position, node_size=100, node_color=GPlot[0].Color)

在第一个情节和

nx.drawing.nx_pylab.draw_networkx_nodes(GPlot[1].G, GPlot[1].Position, node_size=100, node_color=GPlot[1].Color)

在第二个。

总之,这就是我想要做的:我希望第一组节点出现在 subplot(1,2,1) 中,第二组节点出现在 subplot(1,2,2) 中。但两者都出现在同一个情节中 (1,2,2)。

GPlot 只是一个包含 GraphPlot 类的 2 个对象的列表

class GraphForPlot:
    def __init__(self):
        self.G = nx.Graph()
        self.Components = []
        self.ActiveStatus = {}
        self.Nodes = []
        self.Position = {}
        self.Color = []

【问题讨论】:

  • 我不明白你在问什么。您希望代码做什么以及它在做什么?
  • 我希望第一组节点出现在 subplot(1,2,1) 中,第二组节点出现在 subplot(1,2,2) 中。但两者都在同一个情节(1,2,2)
  • 什么是GPlot?您应该编辑您的问题以包含您之前的评论,以及 GPlot 是什么。

标签: python matplotlib networkx


【解决方案1】:

您需要告诉netwkorkx 要绘制到哪些轴,如果不这样做,它将绘制到当前活动的轴(plt.gca() 返回什么 (doc)。

nx.drawing.nx_pylab.draw_networkx_nodes(..., ax=Axes[0])
nx.drawing.nx_pylab.draw_networkx_nodes(..., ax=Axes[1])

附带说明,您不应该对实例变量使用驼峰式大小写 (pep8),它可能会与类名发生冲突(在这种情况下为 matplotlib.axes.Axes)。

【讨论】:

  • 如果这样做,我会收到以下错误:ValueError: Argument must be an image, collection, or ContourSet in this Axes
  • 好吧,如果我这样做,它会起作用:ax=plt.subplot(1,2,i+1) 但我不知道如果我按照你提到的方式做它为什么不起作用跨度>
  • type(Axes[0])的结果是什么?
  • 我不明白该错误的来源。如果您的解决方法不能充分发挥作用,我会发布另一个问题,其中包含一个生成该错误的完整工作示例。
猜你喜欢
  • 2019-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-14
相关资源
最近更新 更多