In [16]:
!pip install plotly --upgrade
In [1]:
from plotly import __version__
print(__version__)
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot, plot
init_notebook_mode(connected=True)
from plotly.graph_objs import *
import pandas as pd
import seaborn as sns
In [2]:
df = sns.load_dataset("iris")
data = [
Parcoords(
line=dict(
color=df['species'],
colorscale=[[0, 'red'], [0.5, 'green'], [1, 'blue']]),
dimensions=list([
dict(
range=[2, 4.5], label='Sepal Width', values=df['sepal_width']),
dict(
range=[4, 8],
# constraintrange=[5, 6],
label='Sepal Length',
values=df['sepal_length']),
dict(
range=[0, 2.5], label='Petal Width', values=df['petal_width']),
dict(
range=[1, 7], label='Petal Length', values=df['petal_length'])
]))
]
iplot(data, filename='parcoords-basic')
In [3]:
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/bcdunbar/datasets/master/parcoords_data.csv")
data = [
Parcoords(
line = dict(
color = df['colorVal'],
colorscale = 'Jet',
showscale = True,
reversescale = True,
cmin = -4000,
cmax = -100
),
dimensions = list([
dict(range = [32000,227900],
constraintrange = [100000,150000],
label = 'Block Height', values = df['blockHeight']),
dict(range = [0,700000],
label = 'Block Width', values = df['blockWidth']),
dict(tickvals = [0,0.5,1,2,3],
ticktext = ['A','AB','B','Y','Z'],
label = 'Cyclinder Material', values = df['cycMaterial']),
dict(range = [-1,4],
tickvals = [0,1,2,3],
label = 'Block Material', values = df['blockMaterial']),
dict(range = [134,3154],
visible = True,
label = 'Total Weight', values = df['totalWeight']),
dict(range = [9,19984],
label = 'Assembly Penalty Weight', values = df['assemblyPW']),
dict(range = [49000,568000],
label = 'Height st Width', values = df['HstW']),
dict(range = [-28000,196430],
label = 'Min Height Width', values = df['minHW']),
dict(range = [98453,501789],
label = 'Min Width Diameter', values = df['minWD']),
dict(range = [1417,107154],
label = 'RF Block', values = df['rfBlock'])
])
)
]
#plot(data)
iplot(data, filename = 'parcoords-advanced.html') ## this seems to don't work anymore ... the html is blank
In [4]:
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot, plot
from plotly.graph_objs import *
init_notebook_mode()
def parallel_coordinates_plot(df,
use_cols,
color_col,
create_html='',
line={},
dimensions=[]):
"""
Produce a parallel plot using the selected columns from the dataframe df.
It use offline plotly functions.
- df: pandas DataFrame
- use_cols: column to be used from df
- color_col: dataframe column with integers that specify the color of the line for that observation
- create_html: when different from '' this is the name of the html to be created with the parallel plot
- line: update the line option of Parcoords (check https://plot.ly/python/reference/#parcoords)
- dimensions: update the dimensions option of Parcoords (check https://plot.ly/python/reference/#parcoords)
"""
myline = dict(color=df[color_col], showscale=True)
if line != {}:
myline.update()
mydimensions = [
dict(label=v, values=df[v]) # range=[df[v].min(), df[v].max()],
for v in use_cols
]
if dimensions != []: mydimensions.extend(dimensions)
data = [Parcoords(line=myline, dimensions=list(mydimensions))]
iplot(data)
if (create_html != ''):
plot(data, filename=create_html)
In [5]:
df.columns
Out[5]:
In [6]:
parallel_coordinates_plot(df, [
'blockHeight', 'blockWidth', 'cycMaterial', 'blockMaterial', 'totalWeight', 'assemblyPW', 'rfBlock'
], 'colorVal', create_html='prova_CANCELLAMI.html')
In [7]:
import pandas
import matplotlib.pyplot as plt
%matplotlib inline
from pandas.tools.plotting import parallel_coordinates
In [9]:
data = sns.load_dataset("iris")
In [10]:
data.head()
Out[10]:
In [11]:
plt.figure(figsize=(20,10))
parallel_coordinates(data, 'species', cols = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width'])
plt.show()
In [ ]:
In [ ]:
In [ ]:
In [ ]: