Parallel Coordinates plot in Matplotlib

独自空忆成欢 提交于 2019-11-28 03:04:30
ev-br

I'm sure there is a better way of doing it, but here's a quick-and-dirty one (a really dirty one):

#!/usr/bin/python import numpy as np import matplotlib.pyplot as plt import matplotlib.ticker as ticker  #vectors to plot: 4D for this example y1=[1,2.3,8.0,2.5] y2=[1.5,1.7,2.2,2.9]  x=[1,2,3,8] # spines  fig,(ax,ax2,ax3) = plt.subplots(1, 3, sharey=False)  # plot the same on all the subplots ax.plot(x,y1,'r-', x,y2,'b-') ax2.plot(x,y1,'r-', x,y2,'b-') ax3.plot(x,y1,'r-', x,y2,'b-')  # now zoom in each of the subplots  ax.set_xlim([ x[0],x[1]]) ax2.set_xlim([ x[1],x[2]]) ax3.set_xlim([ x[2],x[3]])  # set the x axis ticks  for axx,xx in zip([ax,ax2,ax3],x[:-1]):   axx.xaxis.set_major_locator(ticker.FixedLocator([xx])) ax3.xaxis.set_major_locator(ticker.FixedLocator([x[-2],x[-1]]))  # the last one  # EDIT: add the labels to the rightmost spine for tick in ax3.yaxis.get_major_ticks():   tick.label2On=True  # stack the subplots together plt.subplots_adjust(wspace=0)  plt.show() 

This is essentially based on a (much nicer) one by Joe Kingon, Python/Matplotlib - Is there a way to make a discontinuous axis?. You might also want to have a look at the other answer to the same question.

In this example I don't even attempt at scaling the vertical scales, since it depends on what exactly you are trying to achieve.

EDIT: Here is the result

pandas has a parallel coordinates wrapper:

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

Source code, how they made it: plotting.py#L494

Timmm

When using pandas (like suggested by theta), there is no way to scale the axes independently.

The reason you can't find the different vertical axes is because there aren't any. Our parallel coordinates is "faking" the other two axes by just drawing a vertical line and some labels.

https://github.com/pydata/pandas/issues/7083#issuecomment-74253671

Best example I've seen thus far is this one

https://python.g-node.org/python-summerschool-2013/_media/wiki/datavis/olympics_vis.py

See the normalised_coordinates function. Not super fast, but works from what I've tried.

normalised_coordinates(['VAL_1', 'VAL_2', 'VAL_3'], np.array([[1230.23, 1500000, 12453.03], [930.23, 140000, 12453.03], [130.23, 120000, 1243.03]]), [1, 2, 1]) 
user11534694

Still far from perfect but it works and is relatively short:

import numpy as np  import matplotlib.pyplot as plt  def plot_parallel(data,labels):      data=np.array(data)     x=list(range(len(data[0])))     fig, axis = plt.subplots(1, len(data[0])-1, sharey=False)       for d in data:         for i, a in enumerate(axis):             temp=d[i:i+2].copy()             temp[1]=(temp[1]-np.min(data[:,i+1]))*(np.max(data[:,i])-np.min(data[:,i]))/(np.max(data[:,i+1])-np.min(data[:,i+1]))+np.min(data[:,i])             a.plot(x[i:i+2], temp)       for i, a in enumerate(axis):         a.set_xlim([x[i], x[i+1]])         a.set_xticks([x[i], x[i+1]])         a.set_xticklabels([labels[i], labels[i+1]], minor=False, rotation=45)         a.set_ylim([np.min(data[:,i]),np.max(data[:,i])])       plt.subplots_adjust(wspace=0)      plt.show() 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!