如何在matplotlib或seaborn中将彩色形状用作ytick?

时间:2019-02-14 09:53:04

标签: python matplotlib data-visualization seaborn

我正在从事一项名为“知识追踪”的任务,该任务可以随着时间的推移估算学生的掌握水平。我想使用Matplotlib或Seaborn绘制一个类似下面的图。

image1: Target result

它使用不同的颜色表示知识概念,而不是文本。但是,我已经在Google上搜索,发现没有文章在谈论我们如何做到这一点。

我尝试了以下

# simulate a record of student mastery level
student_mastery = np.random.rand(5, 30)
df = pd.DataFrame(student_mastery)

# plot the heatmap using seaborn
marker = matplotlib.markers.MarkerStyle(marker='o', fillstyle='full')
sns_plot = sns.heatmap(df, cmap="RdYlGn", vmin=0.0, vmax=1.0)
y_limit = 5
y_labels = [marker for i in range(y_limit)]
plt.yticks(range(y_limit), y_labels)

但是,它只是简单地返回了标记上的标记__repr__,例如,在yticks上返回了<matplotlib.markers.MarkerStyle at 0x1c5bb07860>

谢谢!

1 个答案:

答案 0 :(得分:3)

尽管How can I make the xtick labels of a plot be simple drawings using matplotlib?为您提供了任意形状的通用解决方案,但对于此处显示的形状,将unicode符号用作文本并根据您的需要对其进行着色可能是有意义的。

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(42)

fig, ax = plt.subplots()
ax.imshow(np.random.rand(3,10), cmap="Greys")

symbolsx = ["⚪", "⚪", "⚫", "⚫", "⚪", "⚫","⚪", "⚫", "⚫","⚪"]
colorsx = np.random.choice(["#3ba1ab", "#b43232", "#8ecc3a", "#893bab"], 10)
ax.set_xticks(range(len(symbolsx)))
ax.set_xticklabels(symbolsx, size=40)
for tick, color in zip(ax.get_xticklabels(), colorsx):
    tick.set_color(color)

symbolsy = ["◾", "◾", "◾"]
ax.set_yticks(range(len(symbolsy)))
ax.set_yticklabels(symbolsy, size=40)
for tick, color in zip(ax.get_yticklabels(), ["crimson", "gold", "indigo"]):
    tick.set_color(color)


plt.show()

enter image description here