1

I am trying to insert a .png image to the right side of the plot and following the code mentioned here: Combine picture and plot with Python Matplotlib

Here is what I have tried:

import numpy as np
from matplotlib.colors import LinearSegmentedColormap
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.cbook as cbook
from matplotlib._png import read_png
from matplotlib.offsetbox import OffsetImage 
cmap = mpl.cm.hot
norm = mpl.colors.Normalize(vmin=-1 * outlier, vmax=outlier)
cmap.set_over('green')
cmap.set_under('green')
cmap.set_bad('green')
plt.xlim(0,35)
plt.ylim(0,35)
fig, ax = plt.subplots()
ax.set_aspect('equal')
cb_ax=fig.add_axes([0.85, 0.1, 0.03, 0.8])
img = ax.imshow(np.ma.masked_values(data, outlier), cmap=cmap, norm=norm, interpolation='none',vmax=outlier)
cb = mpl.colorbar.ColorbarBase(cb_ax, cmap=cmap, norm=norm, extend='both')
##axim = plt.subplot2grid(shape, loc, rowspan=1)

## phlo tree
image_file = cbook.get_sample_data('mytree.png',asfileobj=False)
image = plt.imread(image_file)
phyl_ax=fig.add_axes([0.10,0.1, 0.03, 0.8])
phyl_ax.imshow(image,interpolation='nearest')

Th heat map would be on the left side and the image of a tree will be inserted on the right side. With the above code here is what I get...

Image not added properly:-----

Here is the image I am trying to add:----

There is something being added to the right side but obviously it isn't the way it should look like. At first I thought I was setting the dimensions of phyl_ax too small but when I try to increase it, even the previous "something" is also not being added.

Could someone point at where I am going wrong with it?

1 Answer 1

1

You are calling both subplots, which, by default, gives you a single axes, and also adding axes via add_axes. You should do one or the other, e.g.

...
fig = plt.figure()
ht_ax = fig.add_axes([0.1, 0.1, 0.3, 0.8])
cb_ax = fig.add_axes([0.45, 0.3, 0.02, 0.4])
phyl_ax = fig.add_axes([0.6, 0.1, 0.3, 0.8])

...

--or--

...
fig, ax = plt.subplots(1,2)
fig.subplots_adjust(left=0.15)
ht_ax = ax[0]
phyl_ax = ax[1]
cb_ax=fig.add_axes([0.05, 0.3, 0.02, 0.4])

...

You can use subplots_adjust and set_aspect to adjust the layout. You can also use colorbar.make_axes to get an appropriately sized colorbar axes. Here I also used grid_spec to get the plots to be a size ratio I liked

gs = gridspec.GridSpec(1, 2, width_ratios=[3, 2]) 
ht_ax = plt.subplot(gs[0])
phyl_ax = plt.subplot(gs[1])
cb_ax, kw = mpl.colorbar.make_axes(ht_ax, shrink=0.55)
...
cb = mpl.colorbar.ColorbarBase(ax=cb_ax, cmap=cmap, norm=norm, extend='both', **kw)
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry I need a small clarification. There is an axis I use with imshow for the heat map, so according to your 1st solution I should add this axis too before using it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.