08 02 Customizing Seaborn Plots - HannaAA17/Data-Scientist-With-Python-datacamp GitHub Wiki

Using Seaborn Styles

Setting styles

  • Seaborn has default configuration that can be applied with sns.set()
  • These styles can override matplot and pandas plots as well
  • Theme: sns.set_style(style), style=['white', 'dark', 'whitegrid', 'darkgrid', 'ticks']

Removing axes with despine()

  • sns.despine(): by default, right and top spines are removed.

Colors in Seaborn

Defining a color for a plot

  • Seaborn supports assigning colors to plots using matplotlib color codes:
    • sns.set(color_codes=True)
  • sns.distplot(df[col], color='g')

Palettes

  • set_palette() function to define a palette.
  • sns.palplot() function displays a palette.
  • sns.color_palette() returns the current palette.

Defining Custom palettes.

  • Circular colors = when the data is not ordered -sns.palplot(sns.color_palette('Paired', 12)) 各种不同颜色
  • Sequential colors = when the data has a consistent range from high to low
    • sns.palplot(sns.color_palette('Blues', 12)) 由浅到深的蓝色
  • Diverging colors = when both the low and high values are interesting
    • sns.palplot(sns.color_palette('BrBG',12)) 两种不同的颜色 深-浅-深

Customizing with matplotlib

Using matplotlib axes

# Create a figure and axes
fig, ax = plt.subplots()

# Plot the distribution of data
sns.distplot(df['fmr_3'], ax=ax)

# Create a more descriptive x axis label
ax.set(xlabel="3 Bedroom Fair Market Rent")

# Show the plot
plt.show()

Additional Customizations

  • xlabel, ylabel, xlim, title
  • ax.axvline(x=1000, label='') : 垂直于x轴的线
  • linestyle, linewidth