首页 > 解决方案 > 可视化两个数值数组之间的差异

问题描述

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

我试图在一个图表中可视化:

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()

给出以下输出:

数组的 CDF

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

哪种绘图可以可视化元素之间的差异和元素的值?

我不希望像下面这样绘制线图,因为从可视化中无法得出太多的统计见解。

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

标签: pythonpandasnumpymatplotlibseaborn

解决方案


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

在此处输入图像描述

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)

推荐阅读