For a project, I’m trying to plot on the same figure (using subplots) to “graph”. The data have been collected using a database (but for my problem here, I tried to selected only reasonable number of value).
On the left of the figure, I plot the temperature in JFK during 10 consecutive days whereas on the right, I plot a scatter of the temperature in various locations in the US on a specific day.
This is the part where it gets tricky : I’d like to use a colormap
to display the variation of the temperature across the country and even if I’ve been able to change the color of the colormap
, the dots of the scatter are all with the same color
Here are my variables:
import datetime Tmin=[3.3, 0.5, -8.8, -10.5, -3.3, 0.0, 2.2, 0.5, -1.6, -1.1] # For the 10 days in JFK Tmax=[10.0, 10.0, 0.5, -1.6, 5.5, 12.2, 16.1, 8.8, 3.8, 8.8] Tavg=[6.6, 5.5, -3.8, -6.1, 1.1, 6.1, 9.4, 5.0, 1.1, 3.8] Date_raw=['20120101', '20120102', '20120103', '20120104', '20120105', '20120106', '20120107', '20120108', '20120109', '20120110'] Date=[] for i in Date_raw: Date.append(datetime.datetime.strptime(i,'%Y%m%d')) # For the temperature across the country on a specific day longitude=[-102.68806, -106.5214, -103.5042, -118.33417, -82.5375, -86.61667, -97.64694, -111.03056, -123.812, -87.00583, -83.10528, -99.46667, -97.04639, -79.7238, -82.5067, -97.6799, -93.1566, -68.0173, -73.6167, -87.75222, -83.53333, -93.05556, -100.927, -104.33778, -113.01667, -120.9505, -108.7261, -119.31917, -114.0925, -123.35472, -109.2181, -112.3374, -84.90333, -89.83083, -96.59028, -70.9175, -75.1225, -99.12958, -86.6115, -80.6929, -117.6931, -119.9947, -76.66056, -84.6058, -91.93472, -104.1584, -117.42833, -122.76861, -73.7075, -85.39361, -90.59139] latitude=[38.07, 35.8584, 37.2168, 33.92278, 35.43194, 39.13333, 35.53417, 41.27306, 47.5139, 41.4525, 29.63333, 27.53333, 28.08361, 34.1852, 31.5358, 30.1831, 36.2668, 46.8705, 43.35, 41.78611, 42.23333, 44.93194, 29.3784, 37.26222, 38.41667, 37.6241, 42.8154, 47.20778, 46.9208, 43.23889, 36.292, 35.7552, 38.18472, 35.94028, 33.18028, 42.58417, 40.33, 45.71145, 32.8516, 28.6159, 35.6875, 38.8983, 34.73361, 38.0408, 34.175, 42.0613, 47.97417, 45.24861, 41.06694, 40.23417, 41.61389] temp=[1.6, -7.7, 5.0, 13.8, 2.2, -6.1, 8.3, 2.2, 0.0, -8.3, 8.3, 15.0, 13.8, 5.5, 8.3, 7.2, 4.4, -18.8, -13.8, -5.5, -5.5, -2.2, 11.6, 7.7, 5.0, 7.2, 2.2, -3.8, -5.0, 2.2, 1.1, 2.7, -2.7, 3.8, 9.4, -12.2, -7.2, -0.5, 6.6, 9.4, 6.6, 3.3, 5.0, -2.7, 7.2, 5.0, -5.5, 1.1, -7.2, -8.8, -2.7]
Here is the code itself :
import matplotlib.pyplot as plt import matplotlib.dates as mdates import matplotlib as mpl import numpy as np fig,ax=plt.subplots(1,2,figsize=(8, 6), dpi=100) #For the temperature in JFK ax[0].plot(Date,Tmin,'b',label='minimal') # For the 10 days in JFK ax[0].plot(Date,Tmax,'r',label='maximal') ax[0].plot(Date,Tavg, label='Average') ax[0].xaxis_date() ax[0].xaxis.set_major_formatter(mdates.DateFormatter('%d')) fig.autofmt_xdate() ax[0].set_title('Temperature JFK 10 first days of JAN-12') ax[0].legend(loc='upper left') ax[0].set_xlabel('Day') ax[0].set_ylabel('Temperature (°C)') #For the temperature across the country norm = mpl.colors.Normalize(vmin=-20, vmax=20) ax[1].scatter(longitude,latitude,cmap=temp,s=20) ax[1].set_xticks([]) ax[1].set_yticks([]) ax[1].set_title('Temperature on the 15-01-2012') cmap = plt.cm.RdBu fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap),cax=plt.axes([0.9, 0.11, 0.02, 0.77])).set_label('Temperature °C') plt.show()
I think that what I’m doing wrong is with the cmap = plt.cm.RdBu
line but I don’t know what to write otherwise.
Here is the picture of how it looks:
Furthermore, when I try to display only the map of the country on a specific day, it works perfectly fine. Here is the code and a image of how it looks :
plt.scatter(longitude,latitude,c=temp,s=20) plt.xticks([]) plt.yticks([]) plt.colorbar(cax=plt.axes([0.9, 0.11, 0.02, 0.77])).set_label('Température en °C') plt.show()
What I’ve noticed is that when using subplots I have to specify what norm
and cmap
are in fig.colorbar
for mpl.cm.ScalarMappable
which is absent when displaying only the temperature of the country
Finally, I’d like to have the colorbar to have the same dimension as the plot when using subplots (as it is the case when I’m only plotting the temperature of the country, see 2nd image)
EDIT 1:
To sum it up, my problem is that when I plot the temperature in JFK during 10 days and the temperature across the US on a specific day on the same figure, these mistakes happen:
- The dots (scatter function) all appear with the same color (blue) instead of having different colors depending on its value
- The colorbar overflows below the graph
Advertisement
Answer
Thanks to @JohanC, I’ve been able to fix my issues:
Using scatter1=ax[1].scatter(longitude,latitude,cmap='RdBu',s=20)
and fig.colorbar(scatter1, ax=ax[1], pad=0)
have help ensuring the colorbar
was not overflowing below the graph. This is what I get when I’m inserting these line in my code :
Then, by working on the code by my self, I realized that in order to have the dots in different colors depending on the temperature, I could ‘simply’ remove the line cmap = plt.cm.RdBu
and, change in the scatter
function (scatter1=
) the value
to temp
: scatter1=ax[1].scatter(longitude,latitude,c=temp,s=20)