首页 > 解决方案 > matplotlib可视化-正负比例图

问题描述

我正在尝试制作与下面相同的图表,并想知道 matplotlib 是否有类似的图表来制作它。

下图是R包中STM主题模型的结果

我在 Python 中使用 DMR 有概率值:

array([[0.07204196, 0.04238116],
       [0.04518877, 0.30546978],
       [0.0587892 , 0.19870868],
       [0.16710107, 0.07182639],
       [0.128209  , 0.02422131],
       [0.15264449, 0.07237352],
       [0.2250081 , 0.06986096],
       [0.1337716 , 0.10750801],
       [0.01197221, 0.06736039],
       [0.00527367, 0.04028973]], dtype=float32)

这些是结果,左边是否定词,右边是肯定词

负正比例图示例:

负正比例图示例

标签: pythonmatplotlibnlpdata-visualizationtopic-modeling

解决方案


我不确定问题的关键部分是什么。也就是说,您是否对基于类别标记各个点更感兴趣,或者您是否更关心带有一条线的唯一圆。使用提供的数组,数据代表的内容有点令人困惑。

我假设每个子列表代表一个类别。考虑到这一点,我所做的是为值的差异创建一个单独的列 (delta),然后将它们与索引进行对比。

# New column (delta) with styling
df['delta'] = df[0]-df[1]
col = np.where(df.delta>0,'g',np.where(df.index<0,'b','r'))

fig, ax = plt.subplots(figsize =(10,7))


# Style it up a bit

plt.title('Differnece in Topic Proportion (Negative vs Positive)')
plt.xlabel('Net Review Score')

plt.ylabel('Index Number')
plt.tight_layout()
plt.savefig("Evolution of rapport of polarisation - (Aluminium).png")


plt.scatter(df['delta'], df.index,  s=None, c=col, marker=None, linewidth=2)


plt.axvline(x = 0, color = 'b', label = 'axvline - full height', linestyle="--" ) 

这给出了一个结果:

在此处输入图像描述


推荐阅读