python core programming: Python Plotting of parallel coordinate plots (plotly) mode

Today small for everyone to share a Python implementation draw the parallel coordinates graph (plotly) mode, a good reference value, we want to help. Come and see, to follow the small series together
parallel plot Introduction

When the data exceeds a three dimensional visualization of the data at this time becomes longer so easy. To solve the problem of high-dimensional visualization of data, we can use a parallel plot. The following explanation is taken from the parallel coordinate plots Baidu Encyclopedia: In order to overcome the Cartesian coordinate system easy to run out of space, the problem is difficult to express the three-dimensional data of the above, the respective parallel graph variable high-dimensional data with a series of mutually parallel axis represents the value of the variable corresponding to the position of the shaft. In order to reflect changes in trends and relationships between the variables, is connected into a line will be described the points often different variables. Therefore, the graph is substantially parallel to a point Xi m dimensional Euclidean space (xi1, xi2, ..., xim) mapped to a curve on the two-dimensional plane. In the context of the N parallel lines, (N lines which are generally vertical and equidistant), at a point of the high-dimensional space can be represented as an inflection point in the fold line N parallel axes, the K-th the axes on the K-th dimension represents the value of the point.

Drawing parallel coordinate plot

This article describes two methods to draw a parallel coordinate plot using the Python, are plotted using pandas package and use plotly draw packages (packages and plotly default pandas package is installed).

Plotting using pandas parallel coordinate plot of

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from pandas.plotting import parallel_coordinates
  
data = sns.load_dataset('iris')
  
fig,axes = plt.subplots()
parallel_coordinates(data,'species',ax=axes)
fig.savefig('parallel.png')

Parallel coordinate mapping is as follows: Here Insert Picture Description
From the figure can be seen a common x-axis variable y coordinate axes, this time due to sepal_length, sepal_width, petal_length four variables and petal_width worth close range, made in this way FIG common axis parallel coordinate y has a very good visual effects; however, if the range of values sepal_length, sepal_width, petal_length and petal_width large difference between these variables, such a common axis parallel to the y coordinate of FIG no longer apply, this time What is needed is an independent parallel coordinate plot the y-axis. Another method described below is implemented parallel to the y-axis independently graph.

Plotting using plotly parallel coordinate plot of

plotly drawing has two modes, one is the online mode and the other mode is offline. As used herein, the offline mode and is plotted in the jupyter notebook.

First, familiarize yourself with the way plotly of drawing:

import plotly as py
import plotly.graph_objs as go
py.offline.init_notebook_mode(connected=True) # 初始化设置
  
py.offline.iplot({
 "data": [go.Parcoords(
  line = dict(color = 'blue'),
  dimensions = list([
   dict(range = [1,5],
     constraintrange = [1,2],
     label = 'A', values = [1,4]),
   dict(range = [1.5,5],
     tickvals = [1.5,3,4.5],
     label = 'B', values = [3,1.5]),
   dict(range = [1,5],
     tickvals = [1,2,4,5],
     label = 'C', values = [2,4],
     ticktext = ['text 1', 'text 2', 'text 3', 'text 4']),
   dict(range = [1,5],
     label = 'D', values = [4,2])
  ])
 )],
 "layout": go.Layout(title="My first parallel coordinates")
})

Graphing are as follows: Here Insert Picture Description
Drawing parallel coordinate plots of iris data:

df = sns.load_dataset('iris')
df['species_id'] = df['species'].map({'setosa':1,'versicolor':2,'virginica':3}) #用于颜色映射
  
py.offline.iplot({
 "data": [go.Parcoords(
  line = dict(color = df['species_id'],
     colorscale = [[0,'#D7C16B'],[0.5,'#23D8C3'],[1,'#F3F10F']]),
  dimensions = list([
   dict(range = [2,8],
    constraintrange = [4,8],
    label = 'Sepal Length', values = df['sepal_length']),
   dict(range = [1,6],
    label = 'Sepal Width', values = df['sepal_width']),
   dict(range = [0,8],
    label = 'Petal Length', values = df['petal_length']),
   dict(range = [0,4],
    label = 'Petal Width', values = df['petal_width'])
  ])
 )],
 "layout": go.Layout(title='Iris parallel coordinates plot')
})

Figure drawn as follows:Here Insert Picture Description

Note: For plotly.offline.iplot, go.Parcoords and go.Layout usage can use keywords help view help files, and pyecharts different, help documentation plotly provided very detailed.

We recommend learning Python buckle qun: 913066266, look at how seniors are learning! From basic web development python script to, reptiles, django, data mining, etc. [PDF, actual source code], zero-based projects to combat data are finishing. Given to every little python partner! Every day, Daniel explain the timing Python technology, to share some of the ways to learn and need to pay attention to small details, click on Join us python learner gathering

Published 47 original articles · won praise 53 · views 50000 +

Guess you like

Origin blog.csdn.net/haoxun03/article/details/104270650