# Load the example dataset for Anscombe's quartet df = sns.load_dataset("anscombe")
# Show the results of a linear regression within each dataset sns.lmplot( data=df, x="x", y="y", col="dataset", hue="dataset", col_wrap=2, palette="muted", ci=None, height=4, scatter_kws={"s": 50, "alpha": 1}, ) plt.show()
# Define the palette as a list to specify exact values palette = sns.color_palette("rocket_r")
# Plot the lines on two facets sns.relplot( data=dots, x="time", y="firing_rate", hue="coherence", size="choice", col="align", kind="line", size_order=["T1", "T2"], palette=palette, height=5, aspect=0.75, facet_kws=dict(sharex=False), ) plt.show()
示例 6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
sns.set_theme(style="whitegrid")
penguins = sns.load_dataset("penguins")
# Draw a nested barplot by species and sex g = sns.catplot( data=penguins, kind="bar", x="species", y="body_mass_g", hue="sex", palette="dark", alpha=0.6, height=6, ) g.despine(left=True) g.set_axis_labels("", "Body mass (g)") g.legend.set_title("") plt.show()
示例 7
1 2 3 4 5 6 7 8 9
sns.set_theme(style="ticks", palette="pastel")
# Load the example tips dataset tips = sns.load_dataset("tips")
# Draw a nested boxplot to show bills by day and time sns.boxplot(x="day", y="total_bill", hue="smoker", palette=["m", "g"], data=tips) sns.despine(offset=10, trim=True) plt.show()
示例 8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
sns.set_theme(style="whitegrid")
# Load the example tips dataset tips = sns.load_dataset("tips")
# Draw a nested violinplot and split the violins for easier comparison sns.violinplot( data=tips, x="day", y="total_bill", hue="smoker", split=True, inner="quart", linewidth=1, palette={"Yes": "b", "No": ".85"}, ) sns.despine(left=True) plt.show()
# Compute a correlation matrix and convert to long-form corr_mat = df.corr().stack().reset_index(name="correlation")
# Draw each cell as a scatter point with varying size and color g = sns.relplot( data=corr_mat, x="level_0", y="level_1", hue="correlation", size="correlation", palette="vlag", hue_norm=(-1, 1), edgecolor=".7", height=10, sizes=(50, 250), size_norm=(-0.2, 0.8), )
# Tweak the figure to finalize g.set(xlabel="", ylabel="", aspect="equal") g.despine(left=True, bottom=True) g.ax.margins(0.02) for label in g.ax.get_xticklabels(): label.set_rotation(90) for artist in g.legend.legendHandles: artist.set_edgecolor(".7") plt.show()
示例 10
1 2 3 4 5 6 7 8
sns.set_theme(style="ticks")
rs = np.random.RandomState(11) x = rs.gamma(2, size=1000) y = -0.5 * x + rs.normal(size=1000)
# "Melt" the dataset to "long-form" or "tidy" representation iris = pd.melt(iris, "species", var_name="measurement")
# Initialize the figure f, ax = plt.subplots() sns.despine(bottom=True, left=True)
# Show each observation with a scatterplot sns.stripplot( data=iris, x="value", y="measurement", hue="species", dodge=True, alpha=0.25, zorder=1, )
# Show the conditional means, aligning each pointplot in the # center of the strips by adjusting the width allotted to each # category (.8 by default) by the number of hue levels sns.pointplot( data=iris, x="value", y="measurement", hue="species", join=False, dodge=0.8 - 0.8 / 3, palette="dark", markers="d", scale=0.75, errorbar=None, )
# Load the planets dataset and initialize the figure planets = sns.load_dataset("planets") g = sns.JointGrid(data=planets, x="year", y="distance", marginal_ticks=True)
# Set a log scaling on the y axis g.ax_joint.set(yscale="log")
# Create an inset legend for the histogram colorbar cax = g.figure.add_axes([0.15, 0.55, 0.02, 0.2])
# Load the penguins dataset penguins = sns.load_dataset("penguins")
# Show the joint distribution using kernel density estimation g = sns.jointplot( data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species", kind="kde", ) plt.show()
# Create the data rs = np.random.RandomState(1979) x = rs.randn(500) g = np.tile(list("ABCDEFGHIJ"), 50) df = pd.DataFrame(dict(x=x, g=g)) m = df.g.map(ord) df["x"] += m
# Initialize the FacetGrid object pal = sns.cubehelix_palette(10, rot=-0.25, light=0.7) g = sns.FacetGrid(df, row="g", hue="g", aspect=15, height=0.5, palette=pal)
# Draw the densities in a few steps g.map(sns.kdeplot, "x", bw_adjust=0.5, clip_on=False, fill=True, alpha=1, linewidth=1.5) g.map(sns.kdeplot, "x", clip_on=False, color="w", lw=2, bw_adjust=0.5)
# passing color=None to refline() uses the hue mapping g.refline(y=0, linewidth=2, linestyle="-", color=None, clip_on=False)
# Define and use a simple function to label the plot in axes coordinates deflabel(x, color, label): ax = plt.gca() ax.text( 0, 0.2, label, fontweight="bold", color=color, ha="left", va="center", transform=ax.transAxes, )
g.map(label, "x")
# Set the subplots to overlap g.figure.subplots_adjust(hspace=-0.25)
# Remove axes details that don't play well with overlap g.set_titles("") g.set(yticks=[], ylabel="") g.despine(bottom=True, left=True) plt.show()
# Simulate data from a bivariate Gaussian n = 10000 mean = [0, 0] cov = [(2, 0.4), (0.4, 0.2)] rng = np.random.RandomState(0) x, y = rng.multivariate_normal(mean, cov, n).T
# Draw a combo histogram and scatterplot with density contours f, ax = plt.subplots(figsize=(6, 6)) sns.scatterplot(x=x, y=y, s=5, color=".15") sns.histplot(x=x, y=y, bins=50, pthresh=0.1, cmap="mako") sns.kdeplot(x=x, y=y, levels=5, color="w", linewidths=1) plt.show()
# Load the example Titanic dataset df = sns.load_dataset("titanic")
# Make a custom palette with gendered colors pal = dict(male="#6495ED", female="#F08080")
# Show the survival probability as a function of age and sex g = sns.lmplot( x="age", y="survived", col="sex", hue="sex", data=df, palette=pal, y_jitter=0.02, logistic=True, truncate=False, ) g.set(xlim=(0, 80), ylim=(-0.05, 1.05)) plt.show()
# Draw the heatmap with the mask and correct aspect ratio sns.heatmap( corr, mask=mask, cmap=cmap, vmax=0.3, center=0, square=True, linewidths=0.5, cbar_kws={"shrink": 0.5}, ) plt.show()
# Set up the figure f, ax = plt.subplots(figsize=(8, 8)) ax.set_aspect("equal")
# Draw a contour plot to represent each bivariate density sns.kdeplot( data=iris.query("species != 'versicolor'"), x="sepal_width", y="sepal_length", hue="species", thresh=0.1, ) plt.show()
示例 24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
sns.set_theme(style="whitegrid")
# Load the diamonds dataset diamonds = sns.load_dataset("diamonds")
# Plot the distribution of clarity ratings, conditional on carat sns.displot( data=diamonds, x="carat", hue="cut", kind="kde", height=6, multiple="fill", clip=(0, None), palette="ch:rot=-.25,hue=1,light=.75", ) plt.show()
# Load the penguins dataset penguins = sns.load_dataset("penguins")
# Plot sepal width as a function of sepal_length across days g = sns.lmplot( data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species", height=5 )
# Use more informative axis labels than are provided by default g.set_axis_labels("Snoot length (mm)", "Snoot depth (mm)") plt.show()
示例 27
1 2 3 4 5 6 7 8 9
sns.set_theme(style="white")
df = sns.load_dataset("penguins")
g = sns.PairGrid(df, diag_sharey=False) g.map_upper(sns.scatterplot, s=15) g.map_lower(sns.kdeplot) g.map_diag(sns.kdeplot, lw=2) plt.show()
示例 28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
sns.set_theme(style="whitegrid")
# Load the example Titanic dataset titanic = sns.load_dataset("titanic")
# Set up a grid to plot survival probability against several variables g = sns.PairGrid( titanic, y_vars="survived", x_vars=["class", "sex", "who", "alone"], height=5, aspect=0.5, )
# Draw a seaborn pointplot onto each Axes g.map(sns.pointplot, scale=1.3, errwidth=4, color="xkcd:plum") g.set(ylim=(0, 1)) sns.despine(fig=g.fig, left=True) plt.show()
# Load the dataset crashes = sns.load_dataset("car_crashes")
# Make the PairGrid g = sns.PairGrid( crashes.sort_values("total", ascending=False), x_vars=crashes.columns[:-3], y_vars=["abbrev"], height=10, aspect=0.25, )
# Draw a dot plot using the stripplot function g.map( sns.stripplot, size=10, orient="h", jitter=False, palette="flare_r", linewidth=1, edgecolor="w", )
# Use the same x axis limits on all columns and add better labels g.set(xlim=(0, 25), xlabel="Crashes", ylabel="")
# Use semantically meaningful titles for the columns titles = [ "Total crashes", "Speeding crashes", "Alcohol crashes", "Not distracted crashes", "No previous crashes", ]
for ax, title inzip(g.axes.flat, titles): # Set a different title for each axes ax.set(title=title)
# Make the grid horizontal instead of vertical ax.xaxis.grid(False) ax.yaxis.grid(True)
# Set up the matplotlib figure f, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(7, 5), sharex=True)
# Generate some sequential data x = np.array(list("ABCDEFGHIJ")) y1 = np.arange(1, 11) sns.barplot(x=x, y=y1, palette="rocket", ax=ax1) ax1.axhline(0, color="k", clip_on=False) ax1.set_ylabel("Sequential")
# Center the data to make it diverging y2 = y1 - 5.5 sns.barplot(x=x, y=y2, palette="vlag", ax=ax2) ax2.axhline(0, color="k", clip_on=False) ax2.set_ylabel("Diverging")
# Randomly reorder the data to make it qualitative y3 = rs.choice(y1, len(y1), replace=False) sns.barplot(x=x, y=y3, palette="deep", ax=ax3) ax3.axhline(0, color="k", clip_on=False) ax3.set_ylabel("Qualitative")
# Finalize the plot sns.despine(bottom=True) plt.setp(f.axes, yticks=[]) plt.tight_layout(h_pad=2) plt.show()
# Set up the matplotlib figure f, axes = plt.subplots(3, 3, figsize=(9, 9), sharex=True, sharey=True)
# Rotate the starting point around the cubehelix hue circle for ax, s inzip(axes.flat, np.linspace(0, 3, 10)): # Create a cubehelix colormap to use with kdeplot cmap = sns.cubehelix_palette(start=s, light=1, as_cmap=True)
# Generate and plot a random bivariate dataset x, y = rs.normal(size=(2, 50)) sns.kdeplot( x=x, y=y, cmap=cmap, fill=True, clip=(-5, 5), cut=10, thresh=0, levels=15, ax=ax, ) ax.set_axis_off()
# Initialize the matplotlib figure f, ax = plt.subplots(figsize=(6, 15))
# Load the example car crash dataset crashes = sns.load_dataset("car_crashes").sort_values("total", ascending=False)
# Plot the total crashes sns.set_color_codes("pastel") sns.barplot(x="total", y="abbrev", data=crashes, label="Total", color="b")
# Plot the crashes where alcohol was involved sns.set_color_codes("muted") sns.barplot(x="alcohol", y="abbrev", data=crashes, label="Alcohol-involved", color="b")
# Add a legend and informative axis label ax.legend(ncol=2, loc="lower right", frameon=True) ax.set(xlim=(0, 24), ylabel="", xlabel="Automobile collisions per billion miles") sns.despine(left=True, bottom=True) plt.show()
# Load the example exercise dataset exercise = sns.load_dataset("exercise")
# Draw a pointplot to show pulse as a function of three categorical factors g = sns.catplot( data=exercise, x="time", y="pulse", hue="kind", col="diet", capsize=0.2, palette="YlGnBu_d", errorbar="se", kind="point", height=6, aspect=0.75, ) g.despine(left=True) plt.show()
# Generate an example radial datast r = np.linspace(0, 10, num=100) df = pd.DataFrame({"r": r, "slow": r, "medium": 2 * r, "fast": 4 * r})
# Convert the dataframe to long-form or "tidy" format df = pd.melt(df, id_vars=["r"], var_name="speed", value_name="theta")
# Set up a grid of axes with a polar projection g = sns.FacetGrid( df, col="speed", hue="speed", subplot_kws=dict(projection="polar"), height=4.5, sharex=False, sharey=False, despine=False, )
# Draw a scatterplot onto each axes in the grid g.map(sns.scatterplot, "theta", "r") plt.show()
# Make an example dataset with y ~ x rs = np.random.RandomState(7) x = rs.normal(2, 1, 75) y = 2 + 1.5 * x + rs.normal(0, 2, 75)
# Plot the residuals after fitting a linear model sns.residplot(x=x, y=y, lowess=True, color="g") plt.show()
示例 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
sns.set_theme(style="white")
# Load the example mpg dataset mpg = sns.load_dataset("mpg")
# Plot miles per gallon against horsepower with other semantics sns.relplot( x="horsepower", y="mpg", hue="origin", size="weight", sizes=(40, 400), alpha=0.5, palette="muted", height=6, data=mpg, ) plt.show()
示例 38
1 2 3 4 5 6 7 8 9
sns.set_theme(style="whitegrid", palette="muted")
# Load the penguins dataset df = sns.load_dataset("penguins")
# Draw a categorical scatterplot to show each observation ax = sns.swarmplot(data=df, x="body_mass_g", y="sex", hue="species") ax.set(ylabel="") plt.show()
# Create a random dataset across several variables rs = np.random.default_rng(0) n, p = 40, 8 d = rs.normal(0, 2, (n, p)) d += np.log(np.arange(1, p + 1)) * -5 + 10
# Show each distribution with both violins and points sns.violinplot(data=d, palette="light:g", inner="points", orient="h") plt.show()
# Load the example flights dataset and convert to long-form flights_long = sns.load_dataset("flights") flights = flights_long.pivot("month", "year", "passengers")
# Draw a heatmap with the numeric values in each cell f, ax = plt.subplots(figsize=(9, 6)) sns.heatmap(flights, annot=True, fmt="d", linewidths=0.5, ax=ax) plt.show()
# Load the brain networks example dataset df = sns.load_dataset("brain_networks", header=[0, 1, 2], index_col=0)
# Select a subset of the networks used_networks = [1, 5, 6, 7, 8, 12, 13, 17] used_columns = df.columns.get_level_values("network").astype(int).isin(used_networks) df = df.loc[:, used_columns]
# Create a categorical palette to identify the networks network_pal = sns.husl_palette(8, s=0.45) network_lut = dict(zip(map(str, used_networks), network_pal))
# Convert the palette to vectors that will be drawn on the side of the matrix networks = df.columns.get_level_values("network") network_colors = pd.Series(networks, index=df.columns).map(network_lut)
# Draw the full plot g = sns.clustermap( df.corr(), center=0, cmap="vlag", row_colors=network_colors, col_colors=network_colors, dendrogram_ratio=(0.1, 0.2), cbar_pos=(0.02, 0.32, 0.03, 0.2), linewidths=0.75, figsize=(12, 13), )
# Plot each year's time series in its own facet g = sns.relplot( data=flights, x="month", y="passengers", col="year", hue="year", kind="line", palette="crest", linewidth=4, zorder=5, col_wrap=3, height=2, aspect=1.5, legend=False, )
# Iterate over each subplot to customize further for year, ax in g.axes_dict.items(): # Add the title as an annotation within the plot ax.text(0.8, 0.85, year, transform=ax.transAxes, fontweight="bold")
# Plot every year's time series in the background sns.lineplot( data=flights, x="month", y="passengers", units="year", estimator=None, color=".7", linewidth=1, ax=ax, )
# Reduce the frequency of the x axis ticks ax.set_xticks(ax.get_xticks()[::2])
# Tweak the supporting aspects of the plot g.set_titles("") g.set_axis_labels("", "Passengers") g.tight_layout() plt.show()
# Load the example dataset of brain network correlations df = sns.load_dataset("brain_networks", header=[0, 1, 2], index_col=0)
# Pull out a specific subset of networks used_networks = [1, 3, 4, 5, 6, 7, 8, 11, 12, 13, 16, 17] used_columns = df.columns.get_level_values("network").astype(int).isin(used_networks) df = df.loc[:, used_columns]
# Compute the correlation matrix and average over networks corr_df = df.corr().groupby(level="network").mean() corr_df.index = corr_df.index.astype(int) corr_df = corr_df.sort_index().T
# Set up the matplotlib figure f, ax = plt.subplots(figsize=(11, 6))
# Draw a violinplot with a narrower bandwidth than the default sns.violinplot(data=corr_df, palette="Set3", bw=0.2, cut=1, linewidth=1)
# Finalize the figure ax.set(ylim=(-0.7, 1.05)) sns.despine(left=True, bottom=True) plt.show()