matplotlib: scatterplot and histogram with same colour scale

2022-04-26 Thread Loris Bennett
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.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: matplotlib: scatterplot and histogram with same colour scale

2022-04-26 Thread Loris Bennett
"Loris Bennett"  writes:

> 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)?

Now I have written down the problem with MWE I can see that the mapping
of the colours to the patches of the histogram is wrong.  The following
values illustrate the problem much better

  efficiencies = [51, 52, 53, 54, 55, 56, 57, 58, 59]
  core_hours =   [ 5, 10, 15, 20, 25, 30, 35, 40, 45]

The histogram is just being constructed over the actual data, which is
reasonable.  However, I want the histogram over the entire range 0-100,
so I just need to add the 'range' parameter:

  n, bins, patches = axis[1].hist(efficiencies, n_bins, range=(0, 100),
  histtype='bar', orientation='horizontal')

D'oh!

> 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()
-- 
Dr. Loris Bennett (Herr/Mr)
ZEDAT, Freie Universität Berlin Email [email protected]
-- 
https://mail.python.org/mailman/listinfo/python-list