Saturday, 18 February 2017

Seaborn Visualisation


!pip install seaborn
Requirement already satisfied: seaborn in ./anaconda3/lib/python3.5/site-packages
import numpy
import pandas
import seaborn
import matplotlib.pyplot as plt
%config IPCompleter.greedy = True
%matplotlib inline

Basic Barplots Using Seaborn

seaborn.set(style="white", context="talk")
rs = numpy.random.RandomState(7)

# Set up the matplotlib figure
f, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(8, 6), sharex=True)

# Generate some sequential data
x = numpy.array(list("ABCDEFGHI"))
y1 = numpy.arange(1, 10)
seaborn.barplot(x, y1, palette="BuGn_d", ax=ax1)
ax1.set_ylabel("Sequential")

# Center the data to make it diverging
y2 = y1 - 5
seaborn.barplot(x, y2, palette="RdBu_r", ax=ax2)
ax2.set_ylabel("Diverging")

# Randomly reorder the data to make it qualitative
y3 = rs.choice(y1, 9, replace=False)
seaborn.barplot(x, y3, palette="Set3", ax=ax3)
ax3.set_ylabel("Qualitative")

# Finalize the plot
seaborn.despine(bottom=True)
plt.setp(f.axes, yticks=[])
plt.tight_layout(h_pad=3)

Pairplot Using Seaborn on in-built data

seaborn.set()

df = seaborn.load_dataset("iris")
seaborn.pairplot(df, hue="species")
<seaborn.axisgrid.PairGrid at 0x7faea43ec518>

Basic Pairplot on Inputed Data

seaborn.set()
seaborn.pairplot(data, hue="CATCHMENT")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-f063fc1f0cf1> in <module>()
      1 seaborn.set()
----> 2 seaborn.pairplot(data, hue="CATCHMENT")

NameError: name 'data' is not defined

Grouped Barplot on Inputed Data

Sol_data=pandas.read_csv("/home/kirtiman/Downloads/Solar Power- Train.csv")
Sol_data.describe()
/home/kirtiman/anaconda3/lib/python3.5/site-packages/numpy/lib/function_base.py:3834: RuntimeWarning: Invalid value encountered in percentile
  RuntimeWarning)

Day of Year Year Month Day First Hour of Period Distance to Solar Noon Average Temperature (Day) Average Wind Direction (Day) Average Wind Speed (Day) Sky Cover Visibility Relative Humidity Average Wind Speed (Period) Average Barometric Pressure (Period) Power Generated
count 2500.000000 2500.000000 2500.000000 2500.000000 2500.000000 2500.000000 2500.000000 2500.000000 2500.000000 2500.000000 2500.000000 2500.000000 2499.000000 2500.000000 2500.000000
mean 177.636800 2015.609600 6.347200 15.347200 11.490400 0.515302 57.460800 24.384000 9.666560 1.984400 9.500900 73.211600 9.692677 30.030356 6592.578000
std 113.150826 0.487938 3.691518 8.827878 6.875232 0.303965 6.686879 7.300758 4.987849 1.435333 1.470489 15.238704 7.275654 0.147790 9962.505854
min 1.000000 2015.000000 1.000000 1.000000 1.000000 0.051078 42.000000 1.000000 1.100000 0.000000 0.000000 14.000000 0.000000 29.480000 0.000000
25% 79.000000 2015.000000 3.000000 8.000000 4.000000 0.243714 53.000000 22.000000 6.000000 1.000000 10.000000 65.000000 NaN 29.930000 0.000000
50% 157.000000 2016.000000 6.000000 15.000000 10.000000 0.490759 57.000000 27.000000 9.300000 2.000000 10.000000 77.000000 NaN 30.010000 207.000000
75% 288.000000 2016.000000 10.000000 23.000000 16.000000 0.751727 62.000000 29.000000 12.700000 3.000000 10.000000 84.000000 NaN 30.140000 11108.000000
max 366.000000 2016.000000 12.000000 31.000000 22.000000 1.141361 77.000000 36.000000 26.600000 4.000000 10.000000 100.000000 40.000000 30.530000 36580.000000
seaborn.set(style="ticks")
seaborn.boxplot(x="Visibility", y="Power Generated", hue="Sky Cover", data=Sol_data, palette="PRGn")
seaborn.despine(offset=10, trim=True)

Grid Plot

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="ticks")

# Create a dataset with many short random walks
rs = np.random.RandomState(4)
pos = rs.randint(-1, 2, (20, 5)).cumsum(axis=1)
pos -= pos[:, 0, np.newaxis]
step = np.tile(range(5), 20)
walk = np.repeat(range(20), 5)
df = pd.DataFrame(np.c_[pos.flat, step, walk],
                  columns=["position", "step", "walk"])

# Initialize a grid of plots with an Axes for each walk
grid = sns.FacetGrid(df, col="walk", hue="walk", col_wrap=5, size=1.5)

# Draw a horizontal line to show the starting point
grid.map(plt.axhline, y=0, ls=":", c=".5")

# Draw a line plot to show the trajectory of each random walk
grid.map(plt.plot, "step", "position", marker="o", ms=4)

# Adjust the tick positions and labels
grid.set(xticks=np.arange(5), yticks=[-3, 3],
         xlim=(-.5, 4.5), ylim=(-3.5, 3.5))

# Adjust the arrangement of the plots
grid.fig.tight_layout(w_pad=1)
 
 
Seaborn Visualization
In [1]:
!pip install seaborn
Requirement already satisfied: seaborn in ./anaconda3/lib/python3.5/site-packages
In [2]:
import numpy
import pandas
import seaborn
import matplotlib.pyplot as plt
%config IPCompleter.greedy = True
%matplotlib inline

Basic Barplots Using Seaborn

In [3]:
seaborn.set(style="white", context="talk")
rs = numpy.random.RandomState(7)

# Set up the matplotlib figure
f, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(8, 6), sharex=True)

# Generate some sequential data
x = numpy.array(list("ABCDEFGHI"))
y1 = numpy.arange(1, 10)
seaborn.barplot(x, y1, palette="BuGn_d", ax=ax1)
ax1.set_ylabel("Sequential")

# Center the data to make it diverging
y2 = y1 - 5
seaborn.barplot(x, y2, palette="RdBu_r", ax=ax2)
ax2.set_ylabel("Diverging")

# Randomly reorder the data to make it qualitative
y3 = rs.choice(y1, 9, replace=False)
seaborn.barplot(x, y3, palette="Set3", ax=ax3)
ax3.set_ylabel("Qualitative")

# Finalize the plot
seaborn.despine(bottom=True)
plt.setp(f.axes, yticks=[])
plt.tight_layout(h_pad=3)

Pairplot Using Seaborn on in-built data

In [4]:
seaborn.set()

df = seaborn.load_dataset("iris")
seaborn.pairplot(df, hue="species")
Out[4]:
<seaborn.axisgrid.PairGrid at 0x7faea43ec518>

Basic Pairplot on Inputed Data

In [5]:
seaborn.set()
seaborn.pairplot(data, hue="CATCHMENT")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-f063fc1f0cf1> in <module>()
      1 seaborn.set()
----> 2 seaborn.pairplot(data, hue="CATCHMENT")

NameError: name 'data' is not defined

Grouped Barplot on Inputed Data

In [6]:
Sol_data=pandas.read_csv("/home/kirtiman/Downloads/Solar Power- Train.csv")
In [7]:
Sol_data.describe()
/home/kirtiman/anaconda3/lib/python3.5/site-packages/numpy/lib/function_base.py:3834: RuntimeWarning: Invalid value encountered in percentile
  RuntimeWarning)
Out[7]:
Day of Year Year Month Day First Hour of Period Distance to Solar Noon Average Temperature (Day) Average Wind Direction (Day) Average Wind Speed (Day) Sky Cover Visibility Relative Humidity Average Wind Speed (Period) Average Barometric Pressure (Period) Power Generated
count 2500.000000 2500.000000 2500.000000 2500.000000 2500.000000 2500.000000 2500.000000 2500.000000 2500.000000 2500.000000 2500.000000 2500.000000 2499.000000 2500.000000 2500.000000
mean 177.636800 2015.609600 6.347200 15.347200 11.490400 0.515302 57.460800 24.384000 9.666560 1.984400 9.500900 73.211600 9.692677 30.030356 6592.578000
std 113.150826 0.487938 3.691518 8.827878 6.875232 0.303965 6.686879 7.300758 4.987849 1.435333 1.470489 15.238704 7.275654 0.147790 9962.505854
min 1.000000 2015.000000 1.000000 1.000000 1.000000 0.051078 42.000000 1.000000 1.100000 0.000000 0.000000 14.000000 0.000000 29.480000 0.000000
25% 79.000000 2015.000000 3.000000 8.000000 4.000000 0.243714 53.000000 22.000000 6.000000 1.000000 10.000000 65.000000 NaN 29.930000 0.000000
50% 157.000000 2016.000000 6.000000 15.000000 10.000000 0.490759 57.000000 27.000000 9.300000 2.000000 10.000000 77.000000 NaN 30.010000 207.000000
75% 288.000000 2016.000000 10.000000 23.000000 16.000000 0.751727 62.000000 29.000000 12.700000 3.000000 10.000000 84.000000 NaN 30.140000 11108.000000
max 366.000000 2016.000000 12.000000 31.000000 22.000000 1.141361 77.000000 36.000000 26.600000 4.000000 10.000000 100.000000 40.000000 30.530000 36580.000000
In [8]:
seaborn.set(style="ticks")
seaborn.boxplot(x="Visibility", y="Power Generated", hue="Sky Cover", data=Sol_data, palette="PRGn")
seaborn.despine(offset=10, trim=True)

Grid Plot

In [9]:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="ticks")

# Create a dataset with many short random walks
rs = np.random.RandomState(4)
pos = rs.randint(-1, 2, (20, 5)).cumsum(axis=1)
pos -= pos[:, 0, np.newaxis]
step = np.tile(range(5), 20)
walk = np.repeat(range(20), 5)
df = pd.DataFrame(np.c_[pos.flat, step, walk],
                  columns=["position", "step", "walk"])

# Initialize a grid of plots with an Axes for each walk
grid = sns.FacetGrid(df, col="walk", hue="walk", col_wrap=5, size=1.5)

# Draw a horizontal line to show the starting point
grid.map(plt.axhline, y=0, ls=":", c=".5")

# Draw a line plot to show the trajectory of each random walk
grid.map(plt.plot, "step", "position", marker="o", ms=4)

# Adjust the tick positions and labels
grid.set(xticks=np.arange(5), yticks=[-3, 3],
         xlim=(-.5, 4.5), ylim=(-3.5, 3.5))

# Adjust the arrangement of the plots
grid.fig.tight_layout(w_pad=1)
In [ ]:
 
In [ ]:
 

No comments:

Post a Comment