在迭代散点图旁边绘制自定义颜色图
Plot a custom colormap next to iterative scatterplot
我有一组每年排列的坐标,我将它们绘制成散点图。
我创建了一个反映那些年的自定义颜色图,但问题是我无法绘制该颜色图的颜色条,并用 datetime64 的 numpy 数组的值替换刻度。我不知道该怎么做,因为我没有绘制任何图像(在线示例主要是 plt.imshow)。
我怎么能:
- 使用我的自定义颜色图绘制颜色条
- 用我的日期替换颜色栏的标记?
这是我的代码:
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
# Create the dates
dates = np.datetime64('2017') + np.arange(5)*np.timedelta64(1, 'Y')
# Create array where the dates are the 2nd dimension
arr = np.random.randn(3,5,10,2)
colors = cm.seismic(np.linspace(0,1, num=arr.shape[1]))
fig,ax = plt.subplots()
# Iterate through the 1st and 2nd dimension
for g in range(0,arr.shape[0]):
for i in range(0,arr.shape[1]):
plot = plt.scatter(arr[g,i,:,0],arr[g,i,:,1], s = 10, marker = '_', color = colors)
# Attempt to plot the colorbar and replace ticks by the years
cb = plt.colorbar()
loc = mdates.AutoDateLocator()
cb.ax.yaxis.set_major_locator(loc)
cb.ax.yaxis.set_major_formatter(mdates.ConciseDateFormatter(loc))
colors
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import Normalize
import numpy as np
# Create the dates
dates = np.datetime64('2017') + np.arange(5)*np.timedelta64(1, 'Y')
# Create array where the dates are the 2nd dimension
arr = np.random.randn(3,5,10,2)
values = np.linspace(0,1, num=arr.shape[1])
# create a normalizer
norm = Normalize(vmin=values.min(), vmax=values.max())
# normalize values
norm_values = norm(values)
# choose a colormap
cmap = cm.seismic
# create colors
colors = cmap(norm_values)
# map values to a colorbar
mappable = cm.ScalarMappable(norm=norm, cmap=cmap)
mappable.set_array(values)
fig, ax = plt.subplots()
# Iterate through the 1st and 2nd dimension
for g in range(0,arr.shape[0]):
for i in range(0,arr.shape[1]):
plot = plt.scatter(arr[g,i,:,0],arr[g,i,:,1], s = 10, marker = '_', color = colors[i])
# replace ticks by the years
cb = fig.colorbar(mappable, ticks=values)
cb.ax.set_yticklabels(dates)
cb.set_label("Date")
我有一组每年排列的坐标,我将它们绘制成散点图。 我创建了一个反映那些年的自定义颜色图,但问题是我无法绘制该颜色图的颜色条,并用 datetime64 的 numpy 数组的值替换刻度。我不知道该怎么做,因为我没有绘制任何图像(在线示例主要是 plt.imshow)。
我怎么能:
- 使用我的自定义颜色图绘制颜色条
- 用我的日期替换颜色栏的标记?
这是我的代码:
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
# Create the dates
dates = np.datetime64('2017') + np.arange(5)*np.timedelta64(1, 'Y')
# Create array where the dates are the 2nd dimension
arr = np.random.randn(3,5,10,2)
colors = cm.seismic(np.linspace(0,1, num=arr.shape[1]))
fig,ax = plt.subplots()
# Iterate through the 1st and 2nd dimension
for g in range(0,arr.shape[0]):
for i in range(0,arr.shape[1]):
plot = plt.scatter(arr[g,i,:,0],arr[g,i,:,1], s = 10, marker = '_', color = colors)
# Attempt to plot the colorbar and replace ticks by the years
cb = plt.colorbar()
loc = mdates.AutoDateLocator()
cb.ax.yaxis.set_major_locator(loc)
cb.ax.yaxis.set_major_formatter(mdates.ConciseDateFormatter(loc))
colors
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import Normalize
import numpy as np
# Create the dates
dates = np.datetime64('2017') + np.arange(5)*np.timedelta64(1, 'Y')
# Create array where the dates are the 2nd dimension
arr = np.random.randn(3,5,10,2)
values = np.linspace(0,1, num=arr.shape[1])
# create a normalizer
norm = Normalize(vmin=values.min(), vmax=values.max())
# normalize values
norm_values = norm(values)
# choose a colormap
cmap = cm.seismic
# create colors
colors = cmap(norm_values)
# map values to a colorbar
mappable = cm.ScalarMappable(norm=norm, cmap=cmap)
mappable.set_array(values)
fig, ax = plt.subplots()
# Iterate through the 1st and 2nd dimension
for g in range(0,arr.shape[0]):
for i in range(0,arr.shape[1]):
plot = plt.scatter(arr[g,i,:,0],arr[g,i,:,1], s = 10, marker = '_', color = colors[i])
# replace ticks by the years
cb = fig.colorbar(mappable, ticks=values)
cb.ax.set_yticklabels(dates)
cb.set_label("Date")