0

I am producing the probability distribution function of my variable, which is temperature:

enter image description here

and I am going to produce several plots with temperature PDF evolution. For this reason, I would like to link the color of the plot (rainbow-style) with the value of the peak of the temperature distribution. In this way, it is easy to associate the average value of the temperature just by looking at the color. Here's the code I have written for producing plots of the PDF evolution:

from netCDF4 import Dataset
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import seaborn as sns
from scipy.stats import gaussian_kde


my_file = 'tas/tas.nc'
fh = Dataset(my_file, mode='r')
lons = (fh.variables['rlon'][:])
lats = (fh.variables['rlat'][:])
t = (fh.variables['tas'][:])-273
step = len(t[:,0,0])

t_units = fh.variables['tas'].units
fh.close()

len_lon = len(t[0,0,:])
len_lat = len(t[0,:,0])
len_tot = len_lat*len_lon

temperature = np.zeros(len_tot)

for i in range(step):    
   temperature=t[i,:,:]
   temperature_array = temperature.ravel()

   density = gaussian_kde(temperature_array)
   xs = np.linspace(-80,50,200)
   density.covariance_factor = lambda : .25
   density._compute_covariance()

   plt.title(str(1999+i))
   plt.xlabel("Temperature (C)")
   plt.ylabel("Frequency")

   plt.plot(xs,density(xs))
   plt.savefig('temp_'+str(i))
7
  • Do you have a question? Commented Aug 1, 2017 at 11:27
  • How to automatize in the code the association of rainbow-colors and the maximum temperature values Commented Aug 1, 2017 at 11:31
  • I see a question in the dust of my crystal ball, but it is nebulous... Probably regarding the color / rainbow color map? Or the assignment of the specific colors to each line? Furthermore, you write that you want to set the color according to the peak value, and one sentence later, it's about "average value"? Commented Aug 1, 2017 at 11:33
  • In the plot, there is just one line, which is the PDF. I would like the line to be colored depending on the value of the peak temperature. Tpeak = -20 --> color of the plot = blue, T peak = -10 --> color of the plot = green etc etc Commented Aug 1, 2017 at 11:37
  • How about plt.annotate() ?? It conveys the intended meaning in a clear manner, it is clear even after one week, you have no problems with color blindness, etc etc Commented Aug 1, 2017 at 12:45

1 Answer 1

1

Because the question is lacking a working snippet, I had to come up with some sample data. This creates three datasets, where each one is colored with a specific color between blue (cold) and red (hot) according to their maximum value.

import matplotlib.pyplot as plt
import random
from colour import Color

nrange = 20
mydata1 = random.sample(range(nrange), 3)
mydata2 = random.sample(range(nrange), 3)
mydata3 = random.sample(range(nrange), 3)

colorlist = list(Color('blue').range_to(Color('red'), nrange))

# print(mydata1) print(mydata2) print(mydata3)

plt.plot(mydata1, color='{}'.format(colorlist[max(mydata1)]))
plt.plot(mydata2, color='{}'.format(colorlist[max(mydata2)]))
plt.plot(mydata3, color='{}'.format(colorlist[max(mydata3)]))
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.