从Python代码生成
import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize=(20, 10))
n = 70
x = [f'{i}{val}' for i,val in enumerate(np.arange(n)[::-1])]
y = np.random.randint(50, 100, n)
scatter_colors = np.random.rand(n)
plt.scatter(x, y, c=scatter_colors)
plt.xticks(rotation=90, ha='right')
plt.show()
关于如何使刻度标签与绘制点具有相同颜色的任何想法?
将
c=scatter_colors
或 color=scatter_colors
添加到 plt.xticks(rotation=90, ha='right')
会引发值错误。这是一种方法(使用面向对象的接口重写):
fig, ax = plt.subplots(figsize=(20, 10))
n = 70
x = [f'{i}{val}' for i,val in enumerate(np.arange(n)[::-1])]
y = np.random.randint(50, 100, n)
scatter_colors = np.random.rand(n)
viridis = plt.get_cmap('viridis')
ax.scatter(x, y, c=scatter_colors)
ax.set_xticks(np.arange(n), x, ha='right', rotation=90)
for color, label in zip(scatter_colors, ax.get_xticklabels()):
label.set_color(viridis(color))
plt.show()
输出: