python - 底部有三个颜色条的单图
问题描述
我对 Python 比较陌生。我希望从三个颜色条创建一个填充轮廓图,如下所示:http ://www.cpc.ncep.noaa.gov/products/predictions/30day/off15_temp.gif
我已经能够创建三个颜色条,每个颜色条的长度都与绘图的一侧相同,并将它们分别放置在左侧、底部和右侧。我的问题有两个:
你如何将颜色栏的大小调整为比绘图的一侧短(我已经能够缩小颜色栏的宽度,而不是颜色栏的长度。此外,我只能将颜色栏缩小到与绘图的一侧一样长情节)?
你如何定位多个颜色条,使它们并排出现在图的底部(我还没有看到一个解决方案在图的一侧有多个颜色条)?
以下是我的部分代码:
import matplotlib.pyplot as plt
import numpy as np
#import data
#lon and lat are arrays representing longitude and latitude respectively
#prob_above, prob_normal and prob_below are arrays representing the probability of above average, normal and below average temperature or precipitation occurring
clevs = np.arange(40,110,10) #percent
cs_above = plt.contourf(lon, lat, prob_above, clevs)
cs_normal = plt.contourf(lon, lat, prob_normal, clevs)
cs_below = plt.contourf(lon, lat, prob_below, clevs)
cbar_above = plt.colorbar(cs_above, location = 'left')
cbar_normal = plt.colorbar(cs_normal, location = 'bottom')
cbar_below = plt.colorbar(cs_below, location = 'right')
解决方案
颜色条是在轴内创建的。要完全控制颜色条所在的位置,您可以在相应位置创建一个轴,并使用颜色条的cax
参数来指定用于显示颜色条的轴。
要创建有用的轴,aGridSpec
可能会有所帮助,其中主图跨越多个网格单元,并且单元高度的比率非常不对称。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
x,y1,y2,y3 = np.random.rand(4,15)
gs = GridSpec(2,3, height_ratios=[15,1])
fig = plt.figure()
# Axes for plot
ax = fig.add_subplot(gs[0,:])
# three colorbar axes
cax1 = fig.add_subplot(gs[1,0])
cax2 = fig.add_subplot(gs[1,1])
cax3 = fig.add_subplot(gs[1,2])
# plot
sc1 = ax.scatter(x, y1, c=y1, cmap="viridis")
sc2 = ax.scatter(x, y2, c=y2, cmap="RdYlGn")
sc3 = ax.scatter(x, y3, c=y3, cmap="copper")
# colorbars
fig.colorbar(sc1, cax=cax1, orientation="horizontal")
fig.colorbar(sc2, cax=cax2, orientation="horizontal")
fig.colorbar(sc3, cax=cax3, orientation="horizontal")
plt.show()
推荐阅读
- c++ - IncludeOS hello world 失败
- python - 在列块中展平或分组数组 - NumPy / Python
- php - Laravel 5.8 - 如何在循环内更新表
- docker - 无法在詹金斯管道中以更高分辨率保存柏树屏幕截图
- php - 与数据库的连接在本地主机上工作,但不在线
- c - 如何在数组中逐字节读取
- xamarin.forms - 在使用最新版本的 Xamarin Forms 进行的开发中替换 Xamarin.Forms.Theme.Dark
- scala - spark-submit with scala package ++ operator 返回 java.lang.NoSuchMethodError: scala.Predef$.refArrayOps
- ansible - ansible junos UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0:
- tensorflow - 如何在没有大量标记数据的情况下训练神经网络