在顺序颜色图中可视化一维数据

时间:2019-07-11 11:04:18

标签: pandas matplotlib data-visualization seaborn colormap

我有一个熊猫系列,其数字范围在0到100之间。我想在包含3种主要颜色的单杠中可视化它。

我尝试使用seaborn,但我只能得到一个热图矩阵。我还尝试了以下代码,这些代码可以产生所需的内容,但不能生成所需的方式。

x = my_column.values
y = x
t = x
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.scatter(x, y, c=t, cmap='brg')
ax2.scatter(x, y, c=t, cmap='brg')
plt.show()

我要寻找的是与下图相似的东西,如何使用matplotlib或seaborn实现它?

enter image description here

1 个答案:

答案 0 :(得分:1)

此操作的目的尚不明确,但是,以下内容将生成类似于问题中所示图像的图像:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

x = np.linspace(100,0,101)

fig, ax = plt.subplots(figsize=(6,1), constrained_layout=True)

cmap = LinearSegmentedColormap.from_list("", ["limegreen", "gold", "crimson"])
ax.imshow([x], cmap=cmap, aspect="auto", 
          extent=[x[0]-np.diff(x)[0]/2, x[-1]+np.diff(x)[0]/2,0,1])
ax.tick_params(axis="y", left=False, labelleft=False)
plt.show()

enter image description here

相关问题