简体   繁体   中英

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. When using the pyplot backend, each plot has it's own colorbar, which I don't want as they have the same data. 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. 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:

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.

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'")

Which makes me think po :axes [:contourf] is not the way to summon what I am trying to.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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