14

I am trying to plot to fields with the same scale. The upper image values are a 10 times higher than the one bellow, but they turn out to be the same color in the imshow. How can I set both to have the same scales in colours?

I added the code I am using bellow the image..

Two imshow plots

def show_field(field1,field2):
    fig = plt.figure()
    ax = fig.add_subplot(2, 1, 1)
    ax.imshow(field1,cmap=plt.cm.YlGn)
    ax.set_adjustable('box-forced')
    ax.autoscale(False)
    ax2 = fig.add_subplot(2, 1, 2)
    ax2.set_adjustable('box-forced')
    ax2.imshow(field2,cmap=plt.cm.YlGn)
    ax2.autoscale(False)
    plt.show()
Ohm
  • 2,312
  • 4
  • 36
  • 75
  • 2
    You're looking for the `vmin` and `vmax` arguments. (Side note, this is a duplicate question, though I can't find the canonical version at the moment...) – Joe Kington Sep 24 '15 at 18:24
  • Yes I also haven't found this question, though I was sure that it was raised many times... – Ohm Sep 24 '15 at 20:31
  • I'm fairly sure there's a more exact duplicate than the question I flagged this as a duplicate of... If you or anyone else comes across it, feel free to change it! – Joe Kington Sep 24 '15 at 21:40
  • 1
    Blerg, well, I can't re-close it with the correct duplicate question, but here's a more exact duplicate question: http://stackoverflow.com/questions/3373256/set-colorbar-range-in-matplotlib/3376734#3376734 – Joe Kington Sep 24 '15 at 21:42
  • @JoeKington So what do you say, should I delete my question? – Ohm Sep 24 '15 at 21:47
  • No, don't delete it. It's a good question, and there's nothing wrong with it being a duplicate. It's actually better to have (closed) duplicates around, as people searching have a higher chance of finding their answer (i.e. someone might be searching for something phrased very similar to your question title and find this). – Joe Kington Sep 24 '15 at 21:50

2 Answers2

19

First you need to define the min and max of the color range you want to use. In this example it is the min and max of both arrays you are plotting. Then use these values to set the range of the imshow color code.

import numpy as np     
def show_field(field1,field2):

    combined_data = np.array([field1,field2])
    #Get the min and max of all your data
    _min, _max = np.amin(combined_data), np.amax(combined_data)

    fig = plt.figure()
    ax = fig.add_subplot(2, 1, 1)
    #Add the vmin and vmax arguments to set the color scale
    ax.imshow(field1,cmap=plt.cm.YlGn, vmin = _min, vmax = _max)
    ax.set_adjustable('box-forced')
    ax.autoscale(False)
    ax2 = fig.add_subplot(2, 1, 2)
    ax2.set_adjustable('box-forced')
    #Add the vmin and vmax arguments to set the color scale
    ax2.imshow(field2,cmap=plt.cm.YlGn, vmin = _min, vmax = _max)
    ax2.autoscale(False)
    plt.show()
Ryszard Cetnarski
  • 1,952
  • 19
  • 20
  • 1
    I didn't want to concatenate two very large matrices to get `combined_data`, so I just did `_min = min(field1.min(), field2.min())`. – jds Feb 19 '20 at 16:33
0

To compliment the accepted answer, here is a function that can make an arbitrary number of imshow plots that all share the same color map:

def show_fields(fields):
    combined_data = np.array(fields)
    #Get the min and max of all your data
    _min, _max = np.amin(combined_data), np.amax(combined_data)

    fig = plt.figure()
    for i in range(len(fields)):
        ax = fig.add_subplot(len(fields), 1, i+1)
        #Add the vmin and vmax arguments to set the color scale
        ax.imshow(fields[i],cmap=plt.cm.YlGn, vmin = _min, vmax = _max)
        ax.set_adjustable('box-forced')
        ax.autoscale(False)

    plt.show()

Usage:

show_fields([field1,field2,field3])
ttb
  • 229
  • 3
  • 12