matplotlib: scatterplot and histogram with same colour scale

Loris Bennett loris.bennett at fu-berlin.de
Tue Apr 26 04:19:50 EDT 2022


Hi,

I am using matplotlib to create scatterplot where the colour depends on
the y-value.  Additionally I want to project the y-values onto a rotated
histogram along the right side of the scatterplot. 

My problem is that with my current code, the colours used for the
histogram are normalised to the range of actual y-values.  These don't
match the colours in the scatterplot, which are based on the absolute
y-value in the range 0-100.

Can anyone tell me what I am doing wrong (code below)?

Cheers,

Loris



import matplotlib.pyplot as plt
import numpy as np

efficiencies = [69, 48, 21, 28, 28, 26, 28]
core_hours = [3, 8, 10, 13, 14, 18, 20]

figure, axis = plt.subplots(ncols=2, nrows=1, sharey=True, gridspec_kw={'width_ratios': [10, 1]})

cm = plt.cm.RdYlGn

n_bins = 10
colours = plt.get_cmap(cm)(np.linspace(0, 1, n_bins))

axis[0].scatter(core_hours, efficiencies, c=efficiencies,
                cmap=cm, vmin=0, vmax=100)
axis[0].set_xlabel("core-hours")
axis[0].set_ylabel("CPU efficiency [%]")
axis[0].set_ylim(ymin=-5, ymax=105)

n, bins, patches = axis[1].hist(efficiencies, n_bins,
                                histtype='bar', orientation='horizontal')
for patch, colour in zip(patches, colours):
    patch.set_facecolor(colour)
axis[1].set_xlabel("jobs")

plt.tight_layout()
plt.show()
plt.close()

-- 
This signature is currently under construction.


More information about the Python-list mailing list