How to use a colourmap to plot multiple plots on one graph and iterate over multiple different colourmaps

45 Views Asked by At

I need to use a different colourmap for a range of different sensors. I'm planning to have these different colourmaps in a list that will be called upon in the loop. The individual colourmap for a sensor shows the range of temperatures the sensor was measured over.

So far I am struggling to implement this, I can specify a common colourmap in the loop, here 'Blues' but don't know how to loop over the different colourmaps

cmap = ['Blues', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds']
for j, n in enumerate(sensor_number):
    fig = plt.figure(str(n), figsize=[10,6])
    for k, T in enumerate(Temp):
        xs = np.linspace(t[0], t[-1], 1000)
        ys = model_funct(xs, *param[k][j])
        plt.plot(xs, ys, label="Standard Linear Model", color='k', linestyle='dashed
        cm = plt.cm.Blues(np.linspace(0.5, 0.8, len(Temp)))
        for i in range(len(Temp)):
            plt.scatter(t, R, s=0.5, label="Experimental Data", color=cm[i])
    #plt.savefig(dir + '\\Graphs\\' + str(n) + 'hj.png')
    plt.show()
    

where, t and R is my data

I have simplified and cut a lot of parts out of my code to show the problem, so the code might look a little unnecessary in places.

Also would a contour plot be better for this? Where a different contour is a different temperature

1

There are 1 best solutions below

3
cphlewis On

Two example with the colormaps passed around/used as parameters as objects. One is following your approach with a separate fig/colormap for each sensor. But the data might be more understandable if you use one colormap for all, so I've shown how to share a colormap across all the sensors.

A practice that I have strong feelings about, especially for experimental/sensor data: it should always be stored and passed around with all its identifiers, metadata, however you want to think about it. Everything you could need to reconstruct where it came from. Build it in anywhere you can -- the datastructures that write sensor data to file, the filenames of images, tiny-text captions that you can crop off for publication but are always "really" there... anything. It is so easy to get scrambled and not too hard to set yourself up once so that you automatically label everything. Make the computer do the boring repetitive parts!

import matplotlib.pyplot as plt
import numpy.random as rand
import matplotlib.cm as cmap

# Dummy sensors
sensor_numbers = (213, 902, 34) 
cmaps = [cmap.summer, cmap.winter, cmap.inferno]
sensor_colors = zip(sensor_numbers, cmaps)

#dummy data for each sensor
#    I like keeping data "tied" to its ID in one structure;
#    dictionaries are flexible builtins
sensor_data = dict()
rand.seed(20240304)
N = 12
for n, c in sensor_colors:
    #making up (x, y, c) array data
    #pandas.DataFrame allows meaningful column names for more readable code
    sensor_data[n] = [rand.rand(N), rand.rand(N), n * rand.rand(N), c]  # different color ranges

for i in sensor_data: 
    fig, ax = plt.subplots( figsize=[3.5, 2])
    scatter = ax.scatter(sensor_data[i][0], sensor_data[i][1], 
                         s=20, c = sensor_data[i][2],
                         cmap=sensor_data[i][3])
    fig.colorbar(scatter, ax=ax)
    fig.suptitle("Data from sensor " + str(i))
    plt.savefig("multiple_colormaps_"+str(i)+".png")

# or! if you want to compare all the sensor's data,
# you actually want the *same* colormap for all of them
# find the limits of *all* the data
# before we can plot *any* data with the right color:
cmin, cmax = 10, 10
for i in sensor_data:
    cmin = min(min(sensor_data[i][2]), cmin)
    cmax = max(max(sensor_data[i][2]), cmax)
norm = plt.Normalize(cmin, cmax)
    
shapes = ('1', 8, '+')   # zip these IDs into sensor_data in place of the colormaps
for a, b in zip(sensor_numbers, shapes):
    sensor_data[a].append(b)
    
fig, ax = plt.subplots(figsize=[6, 4])
for i in sensor_data:
    scatter = ax.scatter(sensor_data[i][0], sensor_data[i][1], 
                          s=20, c = sensor_data[i][2],                             
                          label=i, marker=sensor_data[i][4], 
                          cmap=cmap.winter, norm=norm)
fig.colorbar(scatter, ax=ax)
ax.legend()
fig.suptitle("Experimental data")
plt.savefig("multiple_colormaps_shared.png")    

From the first approach:

scatter with greenyellow colorbar

Shared colorbar:

scatter with three markers one colorbar