【问题标题】:Trying to visualize a sorted table with matplotlib (parallel coordinates?)试图用 matplotlib 可视化排序表(平行坐标?)
【发布时间】:2015-10-23 08:46:28
【问题描述】:

我正在尝试可视化排序表(按列排序)。我的理想结果应该是这样的

visualization of a sorted table

关于如何使用 matplotlib 实现这一目标的任何建议?

我已经尝试过给出herehere 的建议,但我正在寻找附图中类似的东西。

提前致谢,

【问题讨论】:

  • 我认为 matplotlib 不支持这种绘图。
  • 欢迎来到 SO。我回答了你的问题,即使它并不真正符合 SO 的标准:SO 不是编码服务,所以通常你不会得到答案,除非你展示你自己尝试过的东西(是的,发布你的代码)以及在哪里你失败了。

标签: python matplotlib plot


【解决方案1】:

Matplotlib 不直接支持这一点,但复制您链接到的绘图相当容易。

给定一个二维数据数组,下面的函数会做类似的事情。可以排序,也可以不排序,函数无所谓。

import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import numpy as np

def sorted_table_plot(data, labels, categories, cmap=None, ax=None):

    # check if an axes was supplied
    if ax is None:
        ax = plt.gca()

    # check if a colormap was supplied
    if cmap is None:
        cmap = plt.cm.jet

    # generate the grid arrays with the coordinates for the annotations
    yy, xx = np.mgrid[:data.shape[0], :data.shape[1]]
    x = xx.flatten()
    y = yy.flatten()
    d = data.flatten()

    # a norm object which we will use with the colorbar
    norm = plt.Normalize(d.min(), d.max())

    # iterate over the data points and draw the labels
    for di, xi, yi in zip(d, x, y):
        color = cmap(norm(di))
        hsv = mcolors.rgb_to_hsv(color[:3])
        fc = 'w' if hsv[2] < 0.7 else 'k'
        ax.annotate(str(di), xy=(xi,yi), xycoords="data",
                va="center", ha="center", color=fc,
                bbox=dict(boxstyle="circle", fc=color))

    # iteratve over all the appearing values and draw the lines
    for i in np.unique(data):
        xi, yi = x[d==i], y[d==i]
        idx = np.argsort(xi)
        plt.plot(xi[idx], yi[idx], color=plt.cm.jet(norm(i)), lw=2)

    # add the axes labels
    ax.set_xticks(xx[0,:])
    ax.set_xticklabels(categories)
    ax.set_yticks(yy[:,0])
    ax.set_yticklabels(labels)

    # adjust the axes ranges
    ax.set_xlim(xx[0,0] - 0.5, xx[-1,-1] + 0.5)
    ax.set_ylim(yy[-1,-1] + 0.5, yy[0,0] - 0.5)

现在,您可以简单地在数据数组上调用它。下面我创建了一个随机数组,因为您不关心提供示例数据集。

# fix the seed for reproducability
np.random.seed(2)

# create random data
data = np.tile(np.arange(1,8), (3,1)).T
labels = map(lambda x: 'label ' + str(x), data[:,1])
categories = map(lambda x: 'cat ' + str(x), np.arange(data.shape[1])+1)
for i in range(1,data.shape[1]):
    # shuffle all but the first column
    np.random.shuffle(data[:,i])

# call the function and show the plot
sorted_table_plot(data, labels, categories)
plt.show()

结果:

【讨论】:

    猜你喜欢
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 2021-02-27
    • 2022-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多