Setting vmin and vmax should do this.

Here's an example:

import matplotlib.pyplot as plt

xyc = range(20)

plt.subplot(121)
plt.scatter(xyc[:13], xyc[:13], c=xyc[:13], s=35, vmin=0, vmax=20)
plt.colorbar()
plt.xlim(0, 20)
plt.ylim(0, 20)

plt.subplot(122)
plt.scatter(xyc[8:20], xyc[8:20], c=xyc[8:20], s=35, vmin=0, vmax=20)   
plt.colorbar()
plt.xlim(0, 20)
plt.ylim(0, 20)

plt.show()

And the plot this produces:

Answer from tom10 on Stack Overflow
Top answer
1 of 2
55

Setting vmin and vmax should do this.

Here's an example:

import matplotlib.pyplot as plt

xyc = range(20)

plt.subplot(121)
plt.scatter(xyc[:13], xyc[:13], c=xyc[:13], s=35, vmin=0, vmax=20)
plt.colorbar()
plt.xlim(0, 20)
plt.ylim(0, 20)

plt.subplot(122)
plt.scatter(xyc[8:20], xyc[8:20], c=xyc[8:20], s=35, vmin=0, vmax=20)   
plt.colorbar()
plt.xlim(0, 20)
plt.ylim(0, 20)

plt.show()

And the plot this produces:

2 of 2
0

Ok, this isn't really an answer-but a follow-up. The results of my coding altering Tom's code above. [not sure that I want to remove the answer check-mark, as the code above does work, and is an answer to the question!]

It doesn't appear to work for my data!! Below is modified code that can be used with my data to produce a plot which wasn't working for me for some strange reason. The input came by way of the h5py functions (hdf5 data file import).

In the below, rf85 is a subset of the arrays for the large batch of experiments where the RF power applied to the system was approximately 85 watts forward. I'm basically slicing and dicing the data in various ways to try and see a trend. This is the 85 watts compared to the full dataset that's current input (there's more data, but this is what I have for now).

import numpy
import matplotlib.pyplot as plt

CurrentsArray = [array([ 0.83333333,  0.8       ,  0.57142857,  0.83333333,  1.03333333,
        0.25      ,  0.81666667,  0.35714286,  0.26      ,  0.57142857,
        0.83333333,  0.47368421,  0.80645161,  0.47368421,  0.52631579,
        0.36666667,  0.47368421,  0.57142857,  0.47368421,  0.47368421,
        0.47368421,  0.47368421,  0.47368421,  0.61764706,  0.81081081,
        0.41666667,  0.47368421,  0.47368421,  0.45      ,  0.73333333,
        0.8       ,  0.8       ,  0.8       ,  0.47368421,  0.45      ,
        0.47368421,  0.83333333,  0.47368421,  0.22222222,  0.32894737,
        0.57142857,  0.83333333,  0.83333333,  1.        ,  1.        ,
        0.46666667])]

growthTarray = [array([ 705.,  620.,  705.,  725.,  712.,  705.,  680.,  680.,  620.,
        660.,  660.,  740.,  721.,  730.,  720.,  720.,  730.,  705.,
        690.,  705.,  680.,  715.,  705.,  670.,  705.,  705.,  650.,
        725.,  725.,  650.,  650.,  650.,  714.,  740.,  710.,  717.,
        737.,  740.,  660.,  705.,  725.,  650.,  710.,  703.,  700.,  650.])]

CuSearray = [array([ 0.46395015,  0.30287259,  0.43496888,  0.46931773,  0.47685844,
        0.44894925,  0.50727844,  0.45076198,  0.44977095,  0.41455029,
        0.38089693,  0.98174953,  0.48600461,  0.65466528,  0.40563053,
        0.22990327,  0.54372179,  0.43143358,  0.92515847,  0.73701742,
        0.64152173,  0.52708783,  0.51794063,  0.49      ,  0.48878252,
        0.45119732,  0.2190089 ,  0.43470776,  0.43509758,  0.52697697,
        0.21576805,  0.32913721,  0.48828072,  0.62201997,  0.71442359,
        0.55454867,  0.50981136,  0.48212956,  0.46      ,  0.45732419,
        0.43402525,  0.40290777,  0.38594786,  0.36777306,  0.36517926,
        0.29880924])]

PFarray = [array([ 384.,  285.,  280.,  274.,  185.,  185.,  184.,  184.,  184.,
        184.,  184.,  181.,  110.,  100.,  100.,  100.,   85.,   85.,
         84.,   84.,   84.,   84.,   84.,   84.,   84.,   84.,   84.,
         84.,   84.,   84.,   84.,   84.,   27.,   20.,    5.,    5.,
          1.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.])]

rf85growthTarray = [array([ 730.,  705.,  690.,  705.,  680.,  715.,  705.,  670.,  705.,
        705.,  650.,  725.,  725.,  650.,  650.,  650.])]

rf85CuSearray = [array([ 0.54372179,  0.43143358,  0.92515847,  0.73701742,  0.64152173,
        0.52708783,  0.51794063,  0.49      ,  0.48878252,  0.45119732,
        0.2190089 ,  0.43470776,  0.43509758,  0.52697697,  0.21576805,
        0.32913721])]

rf85PFarray = [array([ 85.,  85.,  84.,  84.,  84.,  84.,  84.,  84.,  84.,  84.,  84.,
        84.,  84.,  84.,  84.,  84.])]

rf85CurrentsArray = [array([ 0.54372179,  0.43143358,  0.92515847,  0.73701742,  0.64152173,
        0.52708783,  0.51794063,  0.49      ,  0.48878252,  0.45119732,
        0.2190089 ,  0.43470776,  0.43509758,  0.52697697,  0.21576805,
        0.32913721])]

Datavmax = max(max(CurrentsArray))
Datavmin = min(min(CurrentsArray))

plt.subplot(121)
plt.scatter(growthTarray, CuSearray, PFarray, CurrentsArray, vmin=Datavmin, vmax=Datavmax, alpha=0.75)
plt.colorbar()
plt.xlim(600,760)
plt.ylim(0,2.5)

plt.subplot(122)
plt.scatter(rf85growthTarray, rf85CuSearray, rf85PFarray, rf85CurrentsArray, vmin=Datavmin, vmax=Datavmax, alpha=0.75)
plt.colorbar()
plt.xlim(600,760)
plt.ylim(0,2.5)

plt.show()

And finally, the output:

Please note that this is not the perfect output for my work, but I didn't expend effort making it perfect. What is important however: datapoints that you'll recognize as the same between plots do not contain the same color as should be the case based on the vmin vmax use above (as Tom's code suggests).

This is insane. :( I do hope someone can shed light on this for me! I'm positive my code is not that great, so please don't worry about offending in anyway when it comes to my code!!

Extra bag of firey-hot cheetos to anyone who can suggest a way forward. -Allen

UPDATE- Tom10 caught the problem - I had inadvertently used the wrong data for one of my sub-arrays, causing the values to give different color levels than expected (i.e., my data was wrong!) Big props to Tom for this- I wish I could give him another up-vote, but due to my method of asking this question, I can't (sorry Tom!)

Please also see his wonderful example of plotting text at the data positions mentioned below.

Here's an updated image showing that Tom's method does indeed work, and that the plotting was a problem in my own code:

🌐
GeeksforGeeks
geeksforgeeks.org › matplotlib-pyplot-clim-in-python
Matplotlib.pyplot.clim() in Python - GeeksforGeeks
April 19, 2020 - There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot, etc. The clim() function in pyplot module of matplotlib library is used to set the color limits of the current image.
🌐
Python Data Science Handbook
jakevdp.github.io › PythonDataScienceHandbook › 04.07-customizing-colorbars.html
Customizing Colorbars | Python Data Science Handbook
We'll use our discrete colormap to view the results, setting the ticks and clim to improve the aesthetics of the resulting colorbar: In [14]: # plot the results plt.scatter(projection[:, 0], projection[:, 1], lw=0.1, c=digits.target, cmap=plt.cm.get_cmap('cubehelix', 6)) plt.colorbar(ticks=range(6), label='digit value') plt.clim(-0.5, 5.5) The projection also gives us some interesting insights on the relationships within the dataset: for example, the ranges of 5 and 3 nearly overlap in this projection, indicating that some hand written fives and threes are difficult to distinguish, and therefore more likely to be confused by an automated classification algorithm.
🌐
Python Data Science Handbook
jakevdp.github.io › mpl_tutorial › tutorial_pages › tut3.html
3. Various Plotting Examples — mpl-tutorial 0.1 documentation
x = np.random.random(50) y = np.random.random(50) c = np.random.random(50) # color of points s = 500 * np.random.random(50) # size of points fig, ax = plt.subplots() im = ax.scatter(x, y, c=c, s=s, cmap=plt.cm.jet) # Add a colorbar fig.colorbar(im, ax=ax) # set the color limits - not necessary here, but good to know how. im.set_clim(0.0, 1.0) Contour plots can be used to show the variation of a quantity with respect to two others: In [7]: x = np.linspace(0, 10, 50) y = np.linspace(0, 20, 60) z = np.cos(y[:, np.newaxis]) * np.sin(x) fig, ax = plt.subplots() # filled contours im = ax.contourf(x, y, z, 100) # contour lines im2 = ax.contour(x, y, z, colors='k') fig.colorbar(im, ax=ax) Out [7]: <matplotlib.colorbar.Colorbar instance at 0x352f5a8> The imshow command allows displaying images in a variety of formats.
🌐
Stack Overflow
stackoverflow.com › questions › 74416490 › set-clim-to-colormap-in-plt-subblots
python - Set clim to colormap in plt.subblots - Stack Overflow
I tried axs[0].set_clim(0,0.1) , but that doesn't work. ... Copyfig, axs = plt.subplots(1,2, figsize=(10,4.5)) axs[0].scatter(my_data.obs['n_counts'], my_data.obs['n_genes'], c=my_data.obs['percent_mito'], cmap='viridis',s=2) axs[0].set_xlabel('n_counts') axs[0].set_ylabel('n_genes') axs[0].axhline(500, color='red') axs[0].axvline(1000, color='red') axs[0].axvline(35000, color='red') axs[1].scatter(my_data.obs['n_genes'], my_data.obs['percent_mito'], s=2) axs[1].set_xlabel('n_genes') axs[1].set_ylabel('percent_mito') axs[1].axhline(10, color='red') axs[1].axvline(500, color='red') plt.tight_layout()
🌐
GitHub
github.com › matplotlib › matplotlib › pull › 21525
Fix support for clim in scatter. by anntzer · Pull Request #21525 · matplotlib/matplotlib
Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all). New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there). API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there). Fix support for clim in scatter...
Author: matplotlib
🌐
Matplotlib
matplotlib.org › 3.3.0 › api › _as_gen › matplotlib.pyplot.clim.html
matplotlib.pyplot.clim — Matplotlib 3.3.0 documentation
July 17, 2020 - If you want to set the clim of multiple images, use set_clim on every image, for example:
Find elsewhere
🌐
Matplotlib
matplotlib.org › 3.1.0 › api › _as_gen › matplotlib.pyplot.clim.html
matplotlib.pyplot.clim — Matplotlib 3.1.0 documentation
May 18, 2019 - If you want to set the clim of multiple images, use set_clim on every image, for example:
🌐
Matplotlib
matplotlib.org › 3.1.1 › api › _as_gen › matplotlib.pyplot.clim.html
matplotlib.pyplot.clim — Matplotlib 3.1.2 documentation
May 1, 2020 - If you want to set the clim of multiple images, use set_clim on every image, for example:
🌐
Matplotlib
matplotlib.org › 3.3.3 › api › _as_gen › matplotlib.pyplot.clim.html
matplotlib.pyplot.clim — Matplotlib 3.3.3 documentation
November 12, 2020 - If you want to set the clim of multiple images, use set_clim on every image, for example:
🌐
Jklymak
jklymak.github.io › matplotlib.github.com › stable › api › _as_gen › matplotlib.pyplot.clim.html
matplotlib.pyplot.clim — Matplotlib 3.3.3 documentation
matplotlib.pyplot.clim(vmin=None, vmax=None)[source]¶ · Set the color limits of the current image. If either vmin or vmax is None, the image min/max respectively will be used for color scaling.
🌐
Matplotlib
matplotlib.org › 3.3.1 › api › _as_gen › matplotlib.pyplot.clim.html
matplotlib.pyplot.clim — Matplotlib 3.3.1 documentation
August 14, 2020 - If you want to set the clim of multiple images, use set_clim on every image, for example:
🌐
Matplotlib
matplotlib.org › 3.3.2 › api › _as_gen › matplotlib.pyplot.clim.html
matplotlib.pyplot.clim — Matplotlib 3.3.2 documentation
September 15, 2020 - If you want to set the clim of multiple images, use set_clim on every image, for example:
🌐
Matplotlib
matplotlib.org › 3.2.1 › api › _as_gen › matplotlib.pyplot.clim.html
matplotlib.pyplot.clim — Matplotlib 3.2.1 documentation
April 8, 2020 - If you want to set the clim of multiple images, use set_clim on every image, for example: