Adding a colorbar to two subplots with equal aspect ratios

2024/10/14 0:28:00

I'm trying to add a colorbar to a plot consisting of two subplots with equal aspect ratios, i.e. with set_aspect('equal'):

enter image description here

The code used to create this plot can be found in this IPython notebook.

The image created using the code shown below (and here in the notebook) is the best result I could get, but it is still not quite what I want.

plt.subplot(1,2,1)
plt.pcolormesh(rand1)
plt.gca().set_aspect('equal')plt.subplot(1,2,2)
plt.pcolormesh(rand2)
plt.gca().set_aspect('equal')plt.tight_layout()from mpl_toolkits.axes_grid1 import make_axes_locatable
divider = make_axes_locatable(plt.gca())
cax = divider.append_axes("right", size="5%", pad=0.05)plt.colorbar(cax=cax)

enter image description here

This question seems related:

  • Matplotlib 2 Subplots, 1 Colorbar
Answer

I'm still not sure what you exactly want but I guess you want to subplots using pcolormesh to have the same size when you add a colorbar?

What I have now is a bit of a hack as I add a colorbar for both subplots to ensure they have the same size. Afterwords I remove the first colorbar. If the result is what you want I can look into a more pythonic way of achieving it. For now it is still a bit vague as to what you exactly want.

import numpy
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatabledata = numpy.random.random((10, 10))fig = plt.figure()ax1 = fig.add_subplot(1,2,1, aspect = "equal")
ax2 = fig.add_subplot(1,2,2, aspect = "equal")im1 = ax1.pcolormesh(data)
im2 = ax2.pcolormesh(data)divider1 = make_axes_locatable(ax1)
cax1 = divider1.append_axes("right", size="5%", pad=0.05)divider2 = make_axes_locatable(ax2)
cax2 = divider2.append_axes("right", size="5%", pad=0.05)#Create and remove the colorbar for the first subplot
cbar1 = fig.colorbar(im1, cax = cax1)
fig.delaxes(fig.axes[2])#Create second colorbar
cbar2 = fig.colorbar(im2, cax = cax2)plt.tight_layout()plt.show()

enter image description here

https://en.xdnf.cn/q/69474.html

Related Q&A

Why is C++ much faster than python with boost?

My goal is to write a small library for spectral finite elements in Python and to that purpose I tried extending python with a C++ library using Boost, with the hope that it would make my code faster. …

pandas: How to get .to_string() method to align column headers with column values?

This has been stumping me for a while and I feel like there has to be a solution since printing a dataframe always aligns the columns headers with their respective values.example:df = pd.DataFrame({Fir…

Do I need to use `nogil` in Cython

I have some Cython code that Id like to run as quickly as possible. Do I need to release the GIL in order to do this? Lets suppose my code is similar to this: import numpy as np# trivial definition ju…

supervisord environment variables setting up application

Im running an application from supervisord and I have to set up an environment for it. There are about 30 environment variables that need to be set. Ive tried putting all on one bigenvironment=line and…

Updating gui items withing the process

I am trying to make a GUI for my app and ran into a problem: using PySimpleGUI I have to define layout at first and only then display the whole window. Right now the code is like this:import PySimpleGU…

UnicodeDecodeError with Djangos request.FILES

I have the following code in the view call..def view(request):body = u"" for filename, f in request.FILES.items():body = body + Filename: + filename + \n + f.read() + \nOn some cases I getU…

bifurcation diagram with python

Im a beginner and I dont speak english very well so sorry about that. Id like to draw the bifurcation diagram of the sequence : x(n+1)=ux(n)(1-x(n)) with x(0)=0.7 and u between 0.7 and 4.I am supposed …

How do I use nordvpn servers as python requests proxies

Dont ask how, but I parsed the server endpoints of over 5000 nordvpn servers. They usually are something like ar15.nordvpn.com for example. Im trying to use nordvpn servers as request proxies. I know i…

Python Proxy Settings

I was using the wikipedia module in which you can get the information that is present about that topic on wikipedia. When I run the code it is unable to connect because of proxy. When I connected PC to…

Docker - Run Container from Inside Container

I have two applications:a Python console script that does a short(ish) task and exits a Flask "frontend" for starting the console app by passing it command line argumentsCurrently, the Flask …