python - 可视化两个数值数组之间的差异

标签 python pandas numpy matplotlib seaborn

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

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

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 of arrays

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

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

我不希望按如下方式绘制线图,因为无法从可视化中得出太多统计见解。
ax.plot(df1[0])
ax.plot(df2[0])
ax.plot(df2[0]-df1[0])

最佳答案

有很多方法可以显示两个值之间的差异。这实际上取决于您的图表目标、您想要的定量或定性程度,或者您是否想以某种方式显示原始数据。以下是一些不涉及简单线图或密度函数的想法。我强烈推荐这本书Better Data Visualization作者:乔纳森·施瓦比什他讨论了有关数据呈现的有趣考虑。
enter image description here

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)

关于python - 可视化两个数值数组之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60684558/

相关文章:

python - 在日志文件的多个列上使用 ffill 和 bffill 生成每月级别的数据

python - 比较 numpy 数组中的元素、查找对、如何处理边/角

python - 基于第一行合并多维 NumPy 数组

python - 将不同大小的numpy数组组合成一个更大的矩阵

python - 如果下拉列表为空而不显示,我如何从前端检查?

python - 在 python 中划分两个非常大的数字返回 1

python - 如何在 Cartopy 投影中绘制圆的半径

python - 让 Pyinstaller 识别 Kivy Garden Matplotlib 模块的路径

python - 在 MySQL 中存储数据帧字典的好方法是什么?

python - 如何在 Rpy2 中使用 pandas 数据帧和 numpy 数组?