Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

This dungeon is owned and operated by Frobozz Magic Co., Ltd.


devel / comp.lang.python / matplotlib: scatterplot and histogram with same colour scale

SubjectAuthor
* matplotlib: scatterplot and histogram with same colour scaleLoris Bennett
`- Re: matplotlib: scatterplot and histogram with same colour scaleLoris Bennett

1
matplotlib: scatterplot and histogram with same colour scale

<87k0bci7dl.fsf@hornfels.zedat.fu-berlin.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18058&group=comp.lang.python#18058

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: loris.be...@fu-berlin.de (Loris Bennett)
Newsgroups: comp.lang.python
Subject: matplotlib: scatterplot and histogram with same colour scale
Date: Tue, 26 Apr 2022 10:19:50 +0200
Organization: Freie Universitaet Berlin
Lines: 50
Message-ID: <87k0bci7dl.fsf@hornfels.zedat.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: news.uni-berlin.de K5J/0nvqfDUMHJyWHDWfxwm8g/4xbWUzewbqDKMftZ0qpY
Cancel-Lock: sha1:4sGOqKlNJAC8g9ftDDwp1I8b+Dc=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Loris Bennett - Tue, 26 Apr 2022 08:19 UTC

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.

Re: matplotlib: scatterplot and histogram with same colour scale

<87fsm0i6md.fsf@hornfels.zedat.fu-berlin.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18059&group=comp.lang.python#18059

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: loris.be...@fu-berlin.de (Loris Bennett)
Newsgroups: comp.lang.python
Subject: Re: matplotlib: scatterplot and histogram with same colour scale
Date: Tue, 26 Apr 2022 10:36:10 +0200
Organization: ZEDAT, Freie Universität Berlin
Lines: 68
Message-ID: <87fsm0i6md.fsf@hornfels.zedat.fu-berlin.de>
References: <87k0bci7dl.fsf@hornfels.zedat.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de uKdVM9w8AJW75gtbn7cfGAmBVw2N3xNJT+Ski8FKlOra8c
Cancel-Lock: sha1:FXwAavh1Kun69czyIr6d5S596WY=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Loris Bennett - Tue, 26 Apr 2022 08:36 UTC

"Loris Bennett" <loris.bennett@fu-berlin.de> 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 loris.bennett@fu-berlin.de

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor