python how to format data for use with seaborn

Solutions on MaxInterview for python how to format data for use with seaborn by the best coders in the world

showing results for - "python how to format data for use with seaborn"
Luka
18 May 2016
1# Short answer:
2# You have to make a pandas dataframe with labeled data from all 
3# samples essentially concatenated together in the same columns.
4
5# Example process:
6# Say you're starting with 2 samples with x and y data in lists:
7sample_1_x = [1,2,1,1.6,1.4,3,2,0.9,2.2,2.6,3,2]
8sample_1_y = [2,2,1.5,1.6,1.4,3,2,3,2.2,2.6,3,2]
9sample_2_x = [1,1.7,1,1.6,1.4,3,2,1,2.2,2.6,3,2.1]
10sample_2_y = [2,3,1,1.6,1.7,3,2,0.9,2.3,2.6,2.5,2]
11
12# First, import packages and make sample-specific pandas dataframes:
13import pandas as pd
14import seaborn as sns
15import matplotlib.pyplot as plt
16
17sample_1_df = pd.DataFrame({'x':sample_1_x, 'y':sample_1_y})
18sample_2_df = pd.DataFrame({'x':sample_2_x, 'y':sample_2_y})
19
20# Second, add a column of labels to distinguish data later on:
21sample_1_df['labels'] = 'Sample_1'
22sample_2_df['labels'] = 'Sample_2'
23
24# Concatenate the dataframes together:
25vertical_concat = pd.concat([sample_1_df, sample_2_df], axis=0)
26
27# View final format:
28vertical_concat
29	x	y	labels
300	1.0	2.0	Sample_1
311	2.0	2.0	Sample_1
322	1.0	1.5	Sample_1
33.	.	.	.
34.	.	.	.
35.	.	.	.
360	1.0	2.0	Sample_2
371	1.7	3.0	Sample_2
382	1.0	1.0	Sample_2
39
40# Make plots in which samples are distinguished by their labels:
41sns.scatterplot(data=vertical_concat, x='x', y='y', hue='labels')