python画平行坐标图_Matplotlib中的平行坐标图
使用傳統(tǒng)的繪圖類型可以相對(duì)直觀地查看二維和三維數(shù)據(jù)。即使是四維數(shù)據(jù),我們也經(jīng)常能找到顯示數(shù)據(jù)的方法。不過(guò),4維以上的維度越來(lái)越難以顯示。幸運(yùn)的是,parallel coordinates plots提供了一種查看高維結(jié)果的機(jī)制。
有幾個(gè)繪圖包提供平行坐標(biāo)繪圖,例如Matlab、R、VTK type 1和VTK type 2,但我不知道如何使用Matplotlib創(chuàng)建一個(gè)。Matplotlib中是否有內(nèi)置的平行坐標(biāo)圖?我當(dāng)然看不到。
如果沒(méi)有內(nèi)置類型,是否可以使用Matplotlib的標(biāo)準(zhǔn)功能生成平行坐標(biāo)圖?
編輯:
基于下面的Zhenya提供的答案,我開發(fā)了以下支持任意軸數(shù)的泛化。按照我在上面原始問(wèn)題中發(fā)布的示例的打印樣式,每個(gè)軸都有自己的比例。我通過(guò)規(guī)范化每個(gè)軸點(diǎn)的數(shù)據(jù)并使軸的范圍為0到1來(lái)實(shí)現(xiàn)這一點(diǎn)。然后我回去給每個(gè)記號(hào)貼上標(biāo)簽,在截距處給出正確的值。
該函數(shù)通過(guò)接受一組數(shù)據(jù)集來(lái)工作。每個(gè)數(shù)據(jù)集被視為一組點(diǎn),其中每個(gè)點(diǎn)位于不同的軸上。__main__中的示例為兩組30行中的每個(gè)軸獲取隨機(jī)數(shù)。這些行在導(dǎo)致行聚集的范圍內(nèi)是隨機(jī)的;我想驗(yàn)證這一行為。
這個(gè)解決方案不如內(nèi)置的解決方案,因?yàn)槟衅婀值氖髽?biāo)行為,我通過(guò)標(biāo)簽偽造數(shù)據(jù)范圍,但是在Matplotlib添加內(nèi)置解決方案之前,這是可以接受的。#!/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:
下面是一個(gè)在繪制Fisher's Iris data時(shí)從上述代碼中得到的結(jié)果的示例。它不像維基百科上的參考圖片那么好,但是如果你只有Matplotlib并且你需要多維圖的話,它是可以接受的。
總結(jié)
以上是生活随笔為你收集整理的python画平行坐标图_Matplotlib中的平行坐标图的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python流程控制语法_005 Pyt
- 下一篇: python英文字典小程序_python