[Python] How to use the same colour map and scale for two different graphs plotted together...

Discussão em 'Python' iniciado por Stack, Março 18, 2025.

  1. Stack

    Stack Membro Participativo

    I have 3 data sets: height, data1 and data2, where all are different and I want to plot height vs data1 and height vs data2 as a (1,2) subplot using pcolormesh and have them share a colorbar.

    The issue is that when I plot the two sets of data they are using different colormap scales so the shared colorbar is only accurate to one. I have also probably made this harder for myself as I have edited a custom colormap as I didn't like any of the ones I found (I want a colourful gradient but for all the 0 counts to just be white).

    This was my first time trying to plot like this so I built my code to bin the data and create the colormap based on examples, so if there's a glaring problem in this part then I won't see it.

    Here is the code:


    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.colors import BoundaryNorm, LinearSegmentedColormap
    import matplotlib

    height, data1, data2 = np.loadtxt('data.txt', unpack=True)

    if np.min(data1) <= np.min(data2):
    x_min = np.min(data1)
    else:
    x_min = np.min(data2)

    if np.max(data1) <= np.max(data2):
    x_max = np.max(data2)
    else:
    x_max = np.max(data1)


    y_bins = np.linspace(np.min(height), np.max(height), 100)
    x_bins = np.linspace(x_min, x_max, 100)


    # making colour map
    colors = ['lightcyan','royalblue', 'darkviolet', 'crimson', 'orangered', 'orange', 'yellow'] # Gradient from blue to purple for non-zero values
    cmap = LinearSegmentedColormap.from_list("blue_purple_cmap", colors, N=256)
    bounds = [0, 1, 6, 12, 19, 22, 33, 40]
    norm = BoundaryNorm(bounds, cmap.N)



    fig, axes = plt.subplots(1,2, figsize=(15,6))


    # plot 1
    counts, x_edges, y_edges = np.histogram2d(data1, height, bins=[x_bins, y_bins])
    X, Y = np.meshgrid(x_edges[:-1] + np.diff(x_edges)/2, y_edges[:-1] + np.diff(y_edges)/2)
    masked_counts = np.ma.masked_where(counts == 0, counts)

    pc1 = axes[0].pcolormesh(X, Y, masked_counts.T, cmap=cmap, shading='auto')


    # plot 2
    counts, x_edges, y_edges = np.histogram2d(data2, height, bins=[x_bins, y_bins])
    X, Y = np.meshgrid(x_edges[:-1] + np.diff(x_edges)/2, y_edges[:-1] + np.diff(y_edges)/2)
    masked_counts = np.ma.masked_where(counts == 0, counts)

    pc2 = axes[1].pcolormesh(X, Y, masked_counts.T, cmap=cmap, shading='auto')



    cbar = fig.colorbar(mappable=pc1, ax=axes, orientation='vertical', label='Counts', fraction=0.03, aspect=(30), pad=0.02)

    plt.show()



    At the moment here the colorbar is mappable to pc1. When I change it to pc2 the scale obviously changes.

    For pc1 the scale goes to 50 and for pc2 the scale goes to 40:

    colorbar mapped to pc1

    colorbar mapped to pc2

    The closest to this problem I have seen was for two separate plots and was solved by setting the limits of one colorbar to the limits of the other, but I only want to create one colorbar to begin with.

    I'm not the most efficient coder as you can probably see and I've no idea how to set the scale like how I want so please I'd be grateful for any suggestions, thanks!

    Continue reading...

Compartilhe esta Página