Factor plot in Seaborn

With the help of seaborn.factorplot(), we can draw categorical point plots, box plots, violin plots, bar plots, or strip plots onto a seaborn.FacetGrid() by tuning the kind parameter. The default plot type for factorplot is point plot. Unlike other plotting functions in Seaborn, which support a wide variety of input data formats, factorplot supports pandas DataFrames as input only, while variable/column names can be supplied as string to x, y, hue, col, or row:

import seaborn as sns
import matplotlib.pyplot as plt


sns.set(style="ticks")

# Plot EOD stock closing price vs Date for each company.
# Color of plot elements is determined by company name (hue="Company"),
# plot panels are also arranged in columns accordingly (col="Company").
# The col_wrap parameter determines the number of panels per row (col_wrap=3).
g = sns.factorplot(x="Date", y="Close",
hue="Company", col="Company",
data=stock_df, col_wrap=3)

plt.show()

There are several issues in the preceding plot.

First, the aspect ratio (length divided by height) is slightly suboptimal for a time series chart. A wider plot would allow us to observe minute changes during the time period. We are going to adjust that using the aspect parameter.

Second, the lines and dots are too thick, thereby masking some details in the plot. We can reduce the size of these visual elements by tweaking the scale parameter.

Lastly, the ticks are too close to each other, and the tick labels are overlapping. After plotting, sns.factorplot() returns a FacetGrid, which was denoted as g in the code. We can further tweak the aesthetics of the plot, such as tick positions and labels, by calling the relevant functions in the FacetGrid object:

# Increase the aspect ratio and size of each panel
g = sns.factorplot(x="Date", y="Close",
hue="Company", col="Company",
data=stock_df,
col_wrap=3, size=3,
scale=0.5, aspect=1.5)

# Thinning of ticks (select 1 in 10)
locs, labels = plt.xticks()
g.set(xticks=locs[0::10], xticklabels=labels[0::10])

# Rotate the tick labels to prevent overlap
g.set_xticklabels(rotation=30)

# Reduce the white space between plots
g.fig.subplots_adjust(wspace=.1, hspace=.2)
plt.show()

# Create faceted plot separated by industry
g = sns.factorplot(x="Date", y="Close",
hue="Company", col="Industry",
data=stock_df, size=4,
aspect=1.5, scale=0.5)

locs, labels = plt.xticks()
g.set(xticks=locs[0::10], xticklabels=labels[0::10])
g.set_xticklabels(rotation=30)
plt.show()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset