如何在子图中使用颜色图?

How to use colormap in subplots?

对于一个项目,我试图在同一个图形上绘制(使用子图)以“绘制”。 数据是使用数据库收集的(但对于我这里的问题,我试图只选择合理数量的值)。

在图的左侧,我绘制了肯尼迪国际机场连续 10 天的温度,而在右侧,我绘制了特定日期美国不同地点的温度分布图。 这是它变得棘手的部分:我想使用 colormap 来显示全国各地的温度变化,即使我已经能够更改 colormap 的颜色, 散点的颜色都是一样的

这是我的变量:

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]

这是代码本身:

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()

我认为我做错的是 cmap = plt.cm.RdBu 行,但我不知道该写些什么。

这是它的外观图片:

此外,当我尝试只显示特定日期的国家/地区地图时,效果非常好。这是代码和它的外观图片:

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()

我注意到,在使用子图时,我必须指定 normcmapfig.colorbar 中用于 mpl.cm.ScalarMappable,仅在显示时不存在国家的温度

最后,我希望在使用子图时颜色条与图具有相同的尺寸(就像我只绘制国家/地区的温度时的情况一样,请参见第二张图)

编辑 1:

总而言之,我的问题是,当我在同一张图上绘制肯尼迪国际机场 10 天内的温度和特定日期美国各地的温度时,会发生这些错误:

感谢@JohanC,我已经能够解决我的问题: 使用 scatter1=ax[1].scatter(longitude,latitude,cmap='RdBu',s=20)fig.colorbar(scatter1, ax=ax[1], pad=0) 有助于确保 colorbar 不会溢出到图表下方。这是我在代码中插入这些行时得到的:

然后,通过自己编写代码,我意识到为了根据温度使点具有不同的颜色,我可以 'simply' 删除行 cmap = plt.cm.RdBu 并且,将 scatter 函数 (scatter1=) 中的 value 更改为 tempscatter1=ax[1].scatter(longitude,latitude,c=temp,s=20)

因此,我得到了我想要的: