【问题标题】:matplotlib visualization- positive negative proportion chartmatplotlib可视化-正负比例图
【发布时间】:2021-07-05 16:27:13
【问题描述】:

我正在尝试制作与下面相同的图表,并想知道 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)

这些是结果,左边是Negative词,右边是Positive

负正比例图表示例:

【问题讨论】:

  • Alex 和我有不同的方法,但 根本上 的不同来自我们对所提供数组的理解。它们是代表每对的两个类别(例如邻里、沐浴体验)还是代表单个类别的正值/负值(例如,客房服务正值 0.128209;客房服务负值 0.02422131)?换句话说,数组是代表 20 个类别还是 10 个?该图表显示了 30 个类别,因此不清楚。

标签: python matplotlib nlp data-visualization topic-modeling


【解决方案1】:

可以创建与您包含的图像非常接近的东西。我知道右栏应该是负数,右栏应该是正数?

先将数据设为负数:

import numpy as np

arr = np.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")

# Make the right col negative
arr[:, 0] *= -1

然后我们可以像这样绘制:

from string import ascii_lowercase
import matplotlib.pyplot as plt


fig, ax = plt.subplots()

for y, x in enumerate(arr.flatten()):
    # Get a label from the alphabet
    label = ascii_lowercase[y]
    # Plot the point
    ax.plot(x, y, "o", color="black")
    # Annotate the point with the label
    ax.annotate(label, xy=(x, y), xytext=(x - 0.036, y), verticalalignment="center")

# Add the vertical line at zero
ax.axvline(0, ls="--", color="black", lw=1.25)

# Make the x axis equal
xlim = abs(max(ax.get_xlim(), key=abs))
ax.set_xlim((-xlim, xlim))

# Remove y axis
ax.yaxis.set_visible(False)

# Add two text labels for the x axis
for text, x in zip(["Negative", "Positive"], ax.get_xlim()):
    ax.text(x / 2, -3.75, f"{text} Reviews", horizontalalignment="center")

哪些输出:

如果您需要更改文本在绘图或 x 轴上的位置,您可以调整对 ax.annotateax.text 的调用中的值。

【讨论】:

    【解决方案2】:

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

    我假设每个子列表代表一个类别。考虑到这一点,我所做的是为值的差异创建一个单独的列 (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="--" ) 
    

    这给出了一个结果:

    【讨论】:

      猜你喜欢
      • 2019-05-10
      • 2019-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-27
      • 2021-02-27
      • 2016-08-09
      • 2016-12-22
      相关资源
      最近更新 更多