【问题标题】:Visualizing the difference between two numeric arrays可视化两个数值数组之间的差异
【发布时间】:2020-06-26 07:14:14
【问题描述】:

我有两个长度相等的数值数组,其中一个数组的元素值总是 >= 到第二个数组中对应的(相同索引)元素。

我正在尝试在单个图表中进行可视化:

i) 对应元素之间的差异,

ii) 两个数组中对应元素的值。

我尝试如下绘制 CDF:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

arr1 = np.random.uniform(1,20,[25,1])
arr2 = arr1 + np.random.uniform(1,10,[25,1])
df1 = pd.DataFrame(arr1)
df2 = pd.DataFrame(arr2)

fix, ax = plt.subplots()
sns.kdeplot(df1[0], cumulative=True, color='orange', label='arr1')
sns.kdeplot(df2[0], cumulative=True, color='b', label='arr2')
sns.kdeplot(df2[0]-df1[0], cumulative=True, color='r', label='difference')
plt.show()

给出以下输出:

但是,它并没有捕捉到差异,并将对应元素的值放在一起。例如,假设两个元素的差是 3。这两个数可以是 2 和 5,但也可以是 15 和 18,这不能从 CDF 确定。

哪种绘图可以同时显示元素之间的差异和元素的值?

我不希望绘制如下线图,因为无法从可视化中获得太多统计信息。

ax.plot(df1[0])
ax.plot(df2[0])
ax.plot(df2[0]-df1[0])

【问题讨论】:

    标签: python pandas numpy matplotlib seaborn


    【解决方案1】:

    有很多方法可以显示两个值之间的差异。这实际上取决于您对图表的目标,您想要的定量或定性,或者您是否想以某种方式显示原始数据。这里有一些不涉及简单线图或密度函数的想法。我强烈推荐 Johnathan Schwabish 的书Better Data Visualization。他讨论了有关数据表示的有趣考虑。

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    from matplotlib import ticker
    
    arr1 = np.random.uniform(1,20, size=25)
    arr2 = arr1 + np.random.uniform(1,10, size=25)
    
    df = pd.DataFrame({
        'col1' : arr1,
        'col2' : arr2
    })
    
    df['diff'] = df.col2 - df.col1
    df['sum']  = df.col1 + df.col2
    
    fig, axes = plt.subplots(ncols=2, nrows=3, figsize=(15,15))
    axes = axes.flatten()
    
    # Pyramid chart
    df_sorted = df.sort_values(by='sum', ascending=True)
    axes[0].barh(
        y = np.arange(1,26),
        width = -df_sorted.col1
    )
    axes[0].barh(
        y = np.arange(1,26),
        width = df_sorted.col2
    )
    # Style axes[0]
    style_func(axes[0], 'Pyramid Chart')
    
    # Dot Plot
    axes[1].scatter(df.col1, np.arange(1, 26), label='col1')
    axes[1].scatter(df.col2, np.arange(1, 26), label='col2')
    axes[1].hlines(
        y = np.arange(1, 26),
        xmin = df.col1, xmax = df.col2,
        zorder=0, linewidth=1.5, color='k'
    )
    # Style axes[1]
    legend = axes[1].legend(ncol=2, loc='center', bbox_to_anchor=(0.14,1.025), edgecolor='w')
    style_func(axes[1], 'Dot Plot')
    set_xlim    = axes[1].set_xlim(0,25)
    
    # Dot Plot 2
    df_sorted = df.sort_values(by=['col1', 'diff'], ascending=False)
    axes[2].scatter(df_sorted.col1, np.arange(1, 26), label='col1')
    axes[2].scatter(df_sorted.col2, np.arange(1, 26), label='col2')
    axes[2].hlines(
        y = np.arange(1, 26),
        xmin = df_sorted.col1, xmax = df_sorted.col2,
        zorder=0, linewidth=1.5, color='k'
    )
    # Style axes[2]
    legend = axes[2].legend(ncol=2, loc='center', bbox_to_anchor=(0.14,1.025), edgecolor='w')
    style_func(axes[2], 'Dot Plot')
    set_xlim    = axes[2].set_xlim(0,25)
    
    # Dot Plot 3
    df_sorted = df.sort_values(by='sum', ascending=True)
    axes[3].scatter(-df_sorted.col1, np.arange(1, 26), label='col1')
    axes[3].scatter(df_sorted.col2, np.arange(1, 26), label='col2')
    axes[3].vlines(x=0, ymin=-1, ymax=27, linewidth=2.5, color='k')
    axes[3].hlines(
        y = np.arange(1, 26),
        xmin = -df_sorted.col1, xmax = df_sorted.col2,
        zorder=0, linewidth=2
    )
    # Style axes[3]
    legend = axes[3].legend(ncol=2, loc='center', bbox_to_anchor=(0.14,1.025), edgecolor='w')
    style_func(axes[3], 'Dot Plot')
    
    
    # Strip plot
    axes[4].scatter(df.col1, [4] * 25)
    axes[4].scatter(df.col2, [6] * 25)
    axes[4].set_ylim(0, 10)
    axes[4].vlines(
        x = [df.col1.mean(), df.col2.mean()],
        ymin = [3.5, 5.5], ymax=[4.5,6.5],
        color='black', linewidth =2 
    )
    
    # Style axes[4]
    axes[4].yaxis.set_major_locator(ticker.FixedLocator([4,6]))
    axes[4].yaxis.set_major_formatter(ticker.FixedFormatter(['col1','col2']))
    hide_spines = [axes[4].spines[x].set_visible(False) for x in ['left','top','right']]
    set_title   = axes[4].set_title('Strip Plot', fontweight='bold')
    tick_params = axes[4].tick_params(axis='y', left=False)
    grid = axes[4].grid(axis='y', dashes=(8,3), alpha=0.3, color='gray')
    
    # Slope chart
    for i in range(25):
        axes[5].plot([0,1], [df.col1[i], df.col2[i]], color='k')
    align = ['left', 'right']
    for i in range(1,3): 
        axes[5].text(x = i - 1, y = 0, s = 'col' + str(i), 
                     fontsize=14, fontweight='bold', ha=align[i-1])
    set_title   = axes[5].set_title('Slope chart', fontweight='bold')
    axes[5].axis('off')
    
    
    def style_func(ax, title):
        hide_spines = [ax.spines[x].set_visible(False) for x in ['left','top','right']]
        set_title   = ax.set_title(title, fontweight='bold')
        set_xlim    = ax.set_xlim(-25,25)
        x_locator   = ax.xaxis.set_major_locator(ticker.MultipleLocator(5))
        y_locator   = ax.yaxis.set_major_locator(ticker.FixedLocator(np.arange(1,26, 2)))
        spine_width = ax.spines['bottom'].set_linewidth(1.5)
        x_tick_params = ax.tick_params(axis='x', length=8, width=1.5)
        x_tick_params = ax.tick_params(axis='y', left=False)
    

    【讨论】:

      【解决方案2】:

      用 plotly 绘制平行坐标图呢?这将允许查看每个原始数组的不同值,但也可以查看它们是否收敛到相同的差异上?

      https://plot.ly/python/parallel-coordinates-plot/

      【讨论】:

      • 它比基本的线图要好,收敛到相同的差异在情节上很好,因为会有多条不同的线收敛到那个差异。 plotly 的缺点是,如果一个数组有很多值要绘制,图表看起来很混乱。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      • 1970-01-01
      • 2018-01-07
      • 2021-03-22
      相关资源
      最近更新 更多