gpt4 book ai didi

python - Matplotlib 中的平行坐标图

转载 作者:IT老高 更新时间:2023-10-28 21:33:14 31 4
gpt4 key购买 nike

使用传统绘图类型可以相对直接地查看二维和三维数据。即使是四维数据,我们也经常能找到一种显示数据的方法。然而,超过四个的尺寸变得越来越难以显示。幸运的是,parallel coordinates plots 提供了一种查看更高维度结果的机制。

Example Parallel Coordinates Plot from Wikipedia

几个绘图包提供平行坐标图,例如 MatlabRVTK type 1VTK type 2 ,但我不知道如何使用 Matplotlib 创建一个。

  1. Matplotlib 中是否有内置的平行坐标图?我当然看不到一个 in the gallery
  2. 如果没有内置类型,是否可以使用 Matplotlib 的标准功能构建平行坐标图?

编辑:

基于下面Zhenya提供的答案,我开发了以下支持任意数量轴的泛化。按照我在上面的原始问题中发布的示例的绘图样式,每个轴都有自己的比例。我通过对每个轴点的数据进行归一化并使轴的范围为 0 到 1 来实现这一点。然后我返回并将标签应用于每个刻度线,在该截距处给出正确的值。

该函数通过接受可迭代的数据集来工作。每个数据集被认为是一组点,其中每个点位于不同的轴上。 __main__ 中的示例在两组 30 行中为每个轴获取随机数。线条在导致线条聚集的范围内是随机的;我想验证的行为。

此解决方案不如内置解决方案,因为您有奇怪的鼠标行为,而且我通过标签伪造数据范围,但在 Matplotlib 添加内置解决方案之前,它是可以接受的。

#!/usr/bin/python
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

def parallel_coordinates(data_sets, style=None):

dims = len(data_sets[0])
x = range(dims)
fig, axes = plt.subplots(1, dims-1, sharey=False)

if style is None:
style = ['r-']*len(data_sets)

# Calculate the limits on the data
min_max_range = list()
for m in zip(*data_sets):
mn = min(m)
mx = max(m)
if mn == mx:
mn -= 0.5
mx = mn + 1.
r = float(mx - mn)
min_max_range.append((mn, mx, r))

# Normalize the data sets
norm_data_sets = list()
for ds in data_sets:
nds = [(value - min_max_range[dimension][0]) /
min_max_range[dimension][2]
for dimension,value in enumerate(ds)]
norm_data_sets.append(nds)
data_sets = norm_data_sets

# Plot the datasets on all the subplots
for i, ax in enumerate(axes):
for dsi, d in enumerate(data_sets):
ax.plot(x, d, style[dsi])
ax.set_xlim([x[i], x[i+1]])

# Set the x axis ticks
for dimension, (axx,xx) in enumerate(zip(axes, x[:-1])):
axx.xaxis.set_major_locator(ticker.FixedLocator([xx]))
ticks = len(axx.get_yticklabels())
labels = list()
step = min_max_range[dimension][2] / (ticks - 1)
mn = min_max_range[dimension][0]
for i in xrange(ticks):
v = mn + i*step
labels.append('%4.2f' % v)
axx.set_yticklabels(labels)


# Move the final axis' ticks to the right-hand side
axx = plt.twinx(axes[-1])
dimension += 1
axx.xaxis.set_major_locator(ticker.FixedLocator([x[-2], x[-1]]))
ticks = len(axx.get_yticklabels())
step = min_max_range[dimension][2] / (ticks - 1)
mn = min_max_range[dimension][0]
labels = ['%4.2f' % (mn + i*step) for i in xrange(ticks)]
axx.set_yticklabels(labels)

# Stack the subplots
plt.subplots_adjust(wspace=0)

return plt


if __name__ == '__main__':
import random
base = [0, 0, 5, 5, 0]
scale = [1.5, 2., 1.0, 2., 2.]
data = [[base[x] + random.uniform(0., 1.)*scale[x]
for x in xrange(5)] for y in xrange(30)]
colors = ['r'] * 30

base = [3, 6, 0, 1, 3]
scale = [1.5, 2., 2.5, 2., 2.]
data.extend([[base[x] + random.uniform(0., 1.)*scale[x]
for x in xrange(5)] for y in xrange(30)])
colors.extend(['b'] * 30)

parallel_coordinates(data, style=colors).show()

编辑 2:

以下是绘制 Fisher's Iris data 时上述代码的示例。它不如 Wikipedia 中的引用图像那么好,但如果你只有 Matplotlib 并且需要多维绘图,它是可以接受的。

Example result of parallel coordinates plot from this answer

最佳答案

pandas有一个平行坐标包装器:

import pandas
import matplotlib.pyplot as plt
from pandas.tools.plotting import parallel_coordinates

data = pandas.read_csv(r'C:\Python27\Lib\site-packages\pandas\tests\data\iris.csv', sep=',')
parallel_coordinates(data, 'Name')
plt.show()

screenshot

源代码,他们是如何做到的:plotting.py#L494

关于python - Matplotlib 中的平行坐标图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8230638/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com