【发布时间】:2021-07-13 18:10:18
【问题描述】:
我的目标是创建一个基于月份的彩色地图。 我有两个月度数据数据集(相同长度等)。 但是我想绘制两个数据集的散点图,但是颜色图要根据月份着色。希望这在我浏览示例时更有意义:
这是我以散点图的形式相互绘制的两个数据集:
data1 = np.random.rand(360)
data2 = np.random.rand(360)
然后我使用这个函数(split_months)将data1和data2变成一个大小为12、30的二维数组。这就像按月重新分组一样,其中12代表所有月份,30代表所有年份那个特定的月份:
def split_months(monthly_data):
month_split = []
for month in range(12):
month_split.append(monthly_data[month::12])
month_split = np.array(month_split)
return month_split
split_data1 = split_months(data1)
split_data2 = split_months(data2)
print(split_data1.shape, split_data2.shape)
(12, 30) (12, 30)
然后,我将拆分的月份数据重新整形为一维数组,基本上是第一个月和所有年份,然后是第二个月和所有年份。所以制作一个一维数组,但按月重新排序,因此按年数重新排序(如下例所示):
split_months_reshape_data1= split_data1.reshape(12*30) ## reshaping so organized by month now (jan - dec for all years)
split_months_reshape_data2 = split_data2.reshape(12*30)
print(split_data1[0])
print(split_months_reshape_data1[:30])
[0.70049451 0.24326443 0.29633189 0.35540148 0.68205274 0.15130453
0.34046832 0.54975106 0.4502673 0.39086571 0.5610824 0.88443547
0.85777702 0.39887896 0.82240821 0.31162978 0.23496537 0.68776803
0.84677736 0.04060598 0.7735167 0.23317739 0.49447141 0.53932027
0.62494628 0.19676697 0.41435389 0.22843223 0.22817976 0.09133836]
[0.70049451 0.24326443 0.29633189 0.35540148 0.68205274 0.15130453
0.34046832 0.54975106 0.4502673 0.39086571 0.5610824 0.88443547
0.85777702 0.39887896 0.82240821 0.31162978 0.23496537 0.68776803
0.84677736 0.04060598 0.7735167 0.23317739 0.49447141 0.53932027
0.62494628 0.19676697 0.41435389 0.22843223 0.22817976 0.09133836]
## data arrays are the same, split_months is showing all of the numbers for the first month, while split_months_reshape_data1 is showing the first 30 values which is the same as the `split_months[0]`
现在的问题是,有没有办法使用 split_months 中的 12 个数组中的每一个来创建颜色图(1 月 - 12 月),但在每个数组中使用这些特定值?
例如,对于一月份,使用来自 split_months[0] 的值为颜色图制作一种颜色。然后对于二月,使用来自 split_months[1] 的值为颜色图制作另一种颜色
这是我想要的想法,但颜色条不正确:
plt.scatter(split_months_reshape_data1,split_months_reshape_data2, c = split_data1)
plt.colorbar()
plt.show()
plt.show()
如果我的问题需要澄清,请告诉我,它有点具体,但主要目标是获得基于重构数据数组(split_data1 和 split_data2)的颜色图。
【问题讨论】:
-
如果我理解正确,您想使用从连续颜色图中获取的 12 种独特颜色(例如 Viridis 而不是 Set3),而不是使用包含 12 个小矩形的标准图例来显示它们,你想将它们显示为一个有 12 个部分的颜色条(我假设带有月份的标签而不是数字)?
-
是的,完全正确!我尝试创建一个离散的颜色图,但它们只采用硬边界(如特定值)而不是数字数组
标签: python numpy matplotlib jupyter colormap