如何解决如何在子图中使用颜色图?
对于一个项目,我试图在同一个图上(使用子图)绘制“图形”。 数据是使用数据库收集的(但对于我这里的问题,我尝试仅选择合理数量的值)。
在图的左侧,我绘制了肯尼迪机场连续 10 天的温度,而在右侧,我绘制了特定日期美国不同地点的温度分布图。
这是它变得棘手的部分:我想使用 colormap
来显示全国各地的温度变化,即使我已经能够更改 colormap
的颜色,散点的点都是一样的颜色
这是我的变量:
import datetime
Tmin=[3.3,0.5,-8.8,-10.5,-3.3,0.0,2.2,-1.6,-1.1] # For the 10 days in JFK
Tmax=[10.0,10.0,5.5,12.2,16.1,8.8,3.8,8.8]
Tavg=[6.6,-3.8,-6.1,1.1,6.1,9.4,5.0,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,13.8,8.3,-8.3,15.0,7.2,4.4,-18.8,-13.8,-5.5,-2.2,11.6,7.7,-5.0,2.7,-2.7,-12.2,-7.2,-0.5,6.6,3.3,-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,c=temp,s=20)
plt.xticks([])
plt.yticks([])
plt.colorbar(cax=plt.axes([0.9,0.77])).set_label('Température en °C')
plt.show()
我注意到的是,当使用子图时,我必须指定 norm
和 cmap
在 fig.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
不会在图表下方溢出。这是我在代码中插入这些行时得到的结果:
然后,通过自己编写代码,我意识到为了根据温度使点具有不同颜色,我可以“简单地”删除行 cmap = plt.cm.RdBu
并更改 { {1}} 函数 (scatter
) scatter1=
到 value
:temp
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。