Seaborn或Matplotlib的语义差分图

时间:2019-02-12 10:39:43

标签: python matplotlib seaborn

是否可以使用Seaborn或Matplotlib创建语义差异图?无论在文档中还是在这里,我都找不到任何提示。

语义差异图:

enter image description here

我正在考虑一个线图,但是如何绘制第二个y轴值?

如果可能的话,怎么办?

感谢您的帮助!

1 个答案:

答案 0 :(得分:5)

让我们首先使用OrderedDict存储我们将绘制的属性:

from collections import OrderedDict
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
import numpy as np

dict = OrderedDict([('Wholesome', -2), ('Unique', -1), ('Established', 2), ('Traditional', 1)])
opposites = ('Exotic', 'Standard', 'New', 'Contemporary')

通过此设置,我们可以开始绘制。让我们定义我们的图形和轴,并绘制一个简单的数据散点图:

fig, ax1 = plt.subplots(1, 1)

ax1.plot(dict.values(), np.r_[:len(dict)], marker='o')

好的,很好,我们在路上。接下来是将yticks设置为我们在字典中设置的属性(并且在这里时,请确保我们仅在x轴刻度上使用整数)。

ax1.set_yticks(np.r_[:len(dict)])
ax1.set_yticklabels(dict.keys())
ax1.xaxis.set_major_locator(MaxNLocator(integer=True))

接下来,让我们在图的“另一侧”设置相反的属性。让我们建立一个双轴来帮助我们。

ax2 = ax1.twinx()

最后,让我们标记双轴,并确保配对的属性垂直对齐。

ax2.set_ylim(ax1.get_ylim())
ax2.set_yticks(np.r_[:len(dict)])
ax2.set_yticklabels(opposites)

fig.tight_layout()的调用应确保y轴标签可见。所有这些共同给了我以下情节:

enter image description here