Seaborn
In [1]:
Copied!
import matplotlib.pyplot as plt
plt.style.use('classic')
%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt plt.style.use('classic') %matplotlib inline import numpy as np import pandas as pd
In [2]:
Copied!
# Create some data
rng = np.random.RandomState(0)
x = np.linspace(0, 10, 500)
y = np.cumsum(rng.randn(500, 6), 0)
# Create some data rng = np.random.RandomState(0) x = np.linspace(0, 10, 500) y = np.cumsum(rng.randn(500, 6), 0)
In [3]:
Copied!
# Plot the data with Matplotlib defaults
plt.plot(x, y)
plt.legend('ABCDEF', ncol=2, loc='upper left');
# Plot the data with Matplotlib defaults plt.plot(x, y) plt.legend('ABCDEF', ncol=2, loc='upper left');
In [4]:
Copied!
import seaborn as sns
sns.set()
import seaborn as sns sns.set()
In [5]:
Copied!
# same plotting code as above!
plt.plot(x, y)
plt.legend('ABCDEF', ncol=2, loc='upper left');
# same plotting code as above! plt.plot(x, y) plt.legend('ABCDEF', ncol=2, loc='upper left');
In [6]:
Copied!
data = np.random.multivariate_normal([0, 0], [[5, 2], [2, 2]], size=2000)
data = pd.DataFrame(data, columns=['x', 'y'])
for col in 'xy':
plt.hist(data[col], density=True, alpha=0.5)
data = np.random.multivariate_normal([0, 0], [[5, 2], [2, 2]], size=2000) data = pd.DataFrame(data, columns=['x', 'y']) for col in 'xy': plt.hist(data[col], density=True, alpha=0.5)
In [7]:
Copied!
for col in 'xy':
sns.kdeplot(data[col], fill=True)
for col in 'xy': sns.kdeplot(data[col], fill=True)
In [8]:
Copied!
sns.histplot(data['x'])
sns.histplot(data['y']);
sns.histplot(data['x']) sns.histplot(data['y']);
In [9]:
Copied!
sns.kdeplot(data);
sns.kdeplot(data);
In [10]:
Copied!
with sns.axes_style('white'):
sns.jointplot(data, x="x", y="y", kind='kde')
with sns.axes_style('white'): sns.jointplot(data, x="x", y="y", kind='kde')
In [11]:
Copied!
with sns.axes_style('white'):
sns.jointplot(data, x="x", y="y", kind='hex')
with sns.axes_style('white'): sns.jointplot(data, x="x", y="y", kind='hex')
In [12]:
Copied!
iris = sns.load_dataset("iris")
iris.head()
iris = sns.load_dataset("iris") iris.head()
Out[12]:
sepal_length | sepal_width | petal_length | petal_width | species | |
---|---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
In [13]:
Copied!
sns.pairplot(iris, hue='species', height=2.5);
sns.pairplot(iris, hue='species', height=2.5);
In [14]:
Copied!
tips = sns.load_dataset('tips')
tips.head()
tips = sns.load_dataset('tips') tips.head()
Out[14]:
total_bill | tip | sex | smoker | day | time | size | |
---|---|---|---|---|---|---|---|
0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
In [15]:
Copied!
tips['tip_pct'] = 100 * tips['tip'] / tips['total_bill']
grid = sns.FacetGrid(tips, row="sex", col="time", margin_titles=True)
grid.map(plt.hist, "tip_pct", bins=np.linspace(0, 40, 15));
tips['tip_pct'] = 100 * tips['tip'] / tips['total_bill'] grid = sns.FacetGrid(tips, row="sex", col="time", margin_titles=True) grid.map(plt.hist, "tip_pct", bins=np.linspace(0, 40, 15));
In [16]:
Copied!
with sns.axes_style(style='ticks'):
g = sns.catplot(data=tips, x="day", y="total_bill", hue="sex", kind="box")
g.set_axis_labels("Day", "Total Bill");
with sns.axes_style(style='ticks'): g = sns.catplot(data=tips, x="day", y="total_bill", hue="sex", kind="box") g.set_axis_labels("Day", "Total Bill");
In [17]:
Copied!
with sns.axes_style('white'):
sns.jointplot(tips, x="total_bill", y="tip", kind='hex')
with sns.axes_style('white'): sns.jointplot(tips, x="total_bill", y="tip", kind='hex')
In [18]:
Copied!
sns.jointplot(tips, x="total_bill", y="tip", kind='reg');
sns.jointplot(tips, x="total_bill", y="tip", kind='reg');
In [19]:
Copied!
planets = sns.load_dataset('planets')
planets.head()
planets = sns.load_dataset('planets') planets.head()
Out[19]:
method | number | orbital_period | mass | distance | year | |
---|---|---|---|---|---|---|
0 | Radial Velocity | 1 | 269.300 | 7.10 | 77.40 | 2006 |
1 | Radial Velocity | 1 | 874.774 | 2.21 | 56.95 | 2008 |
2 | Radial Velocity | 1 | 763.000 | 2.60 | 19.84 | 2011 |
3 | Radial Velocity | 1 | 326.030 | 19.40 | 110.62 | 2007 |
4 | Radial Velocity | 1 | 516.220 | 10.50 | 119.47 | 2009 |
In [20]:
Copied!
with sns.axes_style('white'):
g = sns.catplot(data=planets, x="year", aspect=2,
kind="count", color='steelblue')
g.set_xticklabels(step=5)
with sns.axes_style('white'): g = sns.catplot(data=planets, x="year", aspect=2, kind="count", color='steelblue') g.set_xticklabels(step=5)
In [21]:
Copied!
# !curl -O https://raw.githubusercontent.com/jakevdp/marathon-data/master/marathon-data.csv
# !curl -O https://raw.githubusercontent.com/jakevdp/marathon-data/master/marathon-data.csv
In [22]:
Copied!
data = pd.read_csv('marathon-data.csv')
data.head()
data = pd.read_csv('marathon-data.csv') data.head()
--------------------------------------------------------------------------- FileNotFoundError Traceback (most recent call last) Cell In[22], line 1 ----> 1 data = pd.read_csv('marathon-data.csv') 2 data.head() File /opt/conda/lib/python3.10/site-packages/pandas/util/_decorators.py:211, in deprecate_kwarg.<locals>._deprecate_kwarg.<locals>.wrapper(*args, **kwargs) 209 else: 210 kwargs[new_arg_name] = new_arg_value --> 211 return func(*args, **kwargs) File /opt/conda/lib/python3.10/site-packages/pandas/util/_decorators.py:331, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs) 325 if len(args) > num_allow_args: 326 warnings.warn( 327 msg.format(arguments=_format_argument_list(allow_args)), 328 FutureWarning, 329 stacklevel=find_stack_level(), 330 ) --> 331 return func(*args, **kwargs) File /opt/conda/lib/python3.10/site-packages/pandas/io/parsers/readers.py:950, in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, error_bad_lines, warn_bad_lines, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options) 935 kwds_defaults = _refine_defaults_read( 936 dialect, 937 delimiter, (...) 946 defaults={"delimiter": ","}, 947 ) 948 kwds.update(kwds_defaults) --> 950 return _read(filepath_or_buffer, kwds) File /opt/conda/lib/python3.10/site-packages/pandas/io/parsers/readers.py:605, in _read(filepath_or_buffer, kwds) 602 _validate_names(kwds.get("names", None)) 604 # Create the parser. --> 605 parser = TextFileReader(filepath_or_buffer, **kwds) 607 if chunksize or iterator: 608 return parser File /opt/conda/lib/python3.10/site-packages/pandas/io/parsers/readers.py:1442, in TextFileReader.__init__(self, f, engine, **kwds) 1439 self.options["has_index_names"] = kwds["has_index_names"] 1441 self.handles: IOHandles | None = None -> 1442 self._engine = self._make_engine(f, self.engine) File /opt/conda/lib/python3.10/site-packages/pandas/io/parsers/readers.py:1735, in TextFileReader._make_engine(self, f, engine) 1733 if "b" not in mode: 1734 mode += "b" -> 1735 self.handles = get_handle( 1736 f, 1737 mode, 1738 encoding=self.options.get("encoding", None), 1739 compression=self.options.get("compression", None), 1740 memory_map=self.options.get("memory_map", False), 1741 is_text=is_text, 1742 errors=self.options.get("encoding_errors", "strict"), 1743 storage_options=self.options.get("storage_options", None), 1744 ) 1745 assert self.handles is not None 1746 f = self.handles.handle File /opt/conda/lib/python3.10/site-packages/pandas/io/common.py:856, in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options) 851 elif isinstance(handle, str): 852 # Check whether the filename is to be opened in binary mode. 853 # Binary mode does not support 'encoding' and 'newline'. 854 if ioargs.encoding and "b" not in ioargs.mode: 855 # Encoding --> 856 handle = open( 857 handle, 858 ioargs.mode, 859 encoding=ioargs.encoding, 860 errors=errors, 861 newline="", 862 ) 863 else: 864 # Binary mode 865 handle = open(handle, ioargs.mode) FileNotFoundError: [Errno 2] No such file or directory: 'marathon-data.csv'
In [ ]:
Copied!