简体   繁体   English

使用Plots.jl中的pyplot。 如何使几个子图只有一个疣鼻?

[英]Using pyplot from Plots.jl. How to make several subplots have only one colobar?

I am using Plots.jl to make several plots in the same figure. 我正在使用Plots.jl在同一图中绘制多个图。 When using the pyplot backend, each plot has it's own colorbar, which I don't want as they have the same data. 使用pyplot后端时,每个图都有其自己的颜色条,我不希望这样做,因为它们具有相同的数据。 I am trying to replicate the answer from this question , however I don't know in detail of the machinery under the Plots.jl API, so I haven't been able to replicate it. 我正在尝试从这个问题中复制答案,但是我不了解Plots.jl API下的机制的详细信息,因此我无法复制它。 My plot is done as: 我的情节是这样完成的:

using Plots;pyplot()
p1 = plot(a,st=:contour,fill=true)
p2 = plot(b,st=:contour,fill=true)
p  = plot(p1,p2)

And, the answer (which is in python) from the link is this: 并且,链接的答案(在python中)是这样的:

fig, axes = plt.subplots(nrows=2, ncols=2)
for ax in axes.flat:
    im = ax.imshow(np.random.random((10,10)), vmin=0, vmax=1)

fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
fig.colorbar(im, cax=cbar_ax)

plt.show()

As far as I understand, the code inside the for is actually making the plots in the axes created by plt.subplots (in my case this is done by Plots.jl The next line makes the plots closer, and then the line fig.add_axes creates a new axis for the colorbar. Finally, the line of fig.colorbar creates a colorbar in the new axis and uses the data from the last plot in the for loop. 据我了解,for中的代码实际上是在由plt.subplots创建的轴上绘制图(在我的情况下,这是由Plots.jl完成的。下一行使图更接近,然后是图fig.add_axes为colorbar创建一个新轴,最后,fig.colorbar这行在new axis中创建一个colorbar,并使用for循环中最后一个图的数据。

My current code is: 我当前的代码是:

cbar_ax = p.o[:add_axes]([0.85, 0.15, 0.05, 0.7]);
p.o[:colorbar](p.o[:axes][1][:contourf],cax=cbar_ax)
display(p)

And it doesn't work (I wouldn't expect it to work because I don't know what I'm doing. 而且它不起作用(我不希望它起作用,因为我不知道自己在做什么。

The error I get is: 我得到的错误是:

AttributeError("'function' object has no attribute 'autoscale_None'") AttributeError(“'function'对象没有属性'autoscale_None'”)

Which makes me think po :axes [:contourf] is not the way to summon what I am trying to. 这让我觉得po :axes [:contourf]并不是召唤我要尝试的方法。

Can anyone help out? 有人可以帮忙吗? Thanks 谢谢

In general, if you want to use code on the PyPlot object it's better to just use PyPlot and forget about Plots. 通常,如果要在PyPlot对象上使用代码,最好只使用PyPlot而忽略Plots。 The mix rarely works in practice. 这种混合在实践中很少起作用。 If you do want to use Plots you should be able to do 如果您确实想使用绘图,则应该可以

using Plots;pyplot()
lims = extrema([a;b])
p1 = plot(a,st=:contour,fill=true, colorbar = false)
p2 = plot(b,st=:contour,fill=true, colorbar = true, clims = lims)
p  = plot(p1,p2)

One of the subplots will be much wider than the other - you probably need to adjust with @layout to get them the same width. 一个子图将比另一个子图宽得多-您可能需要使用@layout进行调整,以使其具有相同的宽度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM