Peter
Peter

Reputation: 371

Keeping the color scaling the same for different plots - Python

I am working with Python and matplotlib. I am doing a continuously plot of some data I get from a spectrum analyser. So with every new step of my loop a new piece is added to my plot.

But i just realised that the color scaling doesn't remain the same. So sometimes if the data are a little different it makes a completely new scaling:

enter image description here

You can recognise it at the big red circle in the middle. The value of the entries for the red circle are all zeros. So normally it should have the same color with every new iteration. But as you can see, if you look closely, this isn't the case. Sometimes the red is a little bit darker.

I already set my vminto -100 and my vmax to 0 :

 while True: 
  ... #some code 

  a = np.linspace( (i*np.pi/8-np.pi/16)%(np.pi*2) ,( i*np.pi/8+np.pi/16)%(np.pi*2) , 2)#Angle, circle is divided into 16 pieces
  b = np.linspace(start -scaleplot, stop,801) #points of the frequency + 200  more points to gain the inner circle
  A, B = np.meshgrid(a, longzeroarray)

  ax.contourf(a, b, B , cmap=cm.jet, vmin=-100, vmax=0)

  plt.draw()

I hope you have some helpful ideas.

Upvotes: 1

Views: 594

Answers (1)

CT Zhu
CT Zhu

Reputation: 54330

You can specify a fixed levels, for example (modified from doc):

You can't use vmax and vmin together with levels, as the former will override the latter. To get round it, you can use masked array:

import numpy as np
import matplotlib.pyplot as plt

origin = 'lower'
#origin = 'upper'

delta = 0.025

x = y = np.arange(-3.0, 3.01, delta)
X, Y = np.meshgrid(x, y)
Z1 = plt.mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = plt.mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = 10 * (Z1 - Z2)

levels = np.linspace(-3,2,50)
Vmin   = -1.
Vmax   = 1.
M = np.ma.array(Z, mask=((Z<Vmin)|(Z>Vmax)))

#levels = np.hstack(([Vmin], levels, [Vmax]))

f, (ax1, cax1, ax2, cax2) = plt.subplots(1,4,gridspec_kw={'width_ratios':[9,1,9,1]})
cf1 =  ax1.contourf(X, Y, M, levels=levels)
plt.colorbar(cf1, cax=cax1)
cf2 = ax2.contourf(X, Y, M*0.5, levels=levels)
plt.colorbar(cf2, cax=cax2)

enter image description here

Upvotes: 1

Related Questions