Udemy Visualization Flashcards
When using jupyter notebook, how can you see plots that you create with matplotlib?
%matplotlib inline
Object-oriented way to make an x, y line plot using matplotlib
fig = plt.figure() #essentially creates an empty canvas
axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])
axes.plot(x, y)-===============================
Object-oriented way to create subplots using matplotlib
fig, axes = plt.subplots(nrows=1, ncols=2)
axes[0].plot(x,y)
axes[0].set_title(‘First Plot’)
axes[1].plot(y, x)
axes[1].set_title(‘Second Plot’)
What usually takes cares of overlapping plots?
plt.tight_layout()
How can you control the figure size?
fig = plt.figure(figsize=(width, height))
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(width, height))
How do you save a figure?
fig.savefig(‘my_picture.png’, dpi=200)
Object-oriented way to add y and x label and title
ax. set_ylabel(‘Y’)
ax. set_xlabel(‘X’)
ax. set_title(‘Title’)
How to add a legend
Add label to each plot and follow with ax.legend()
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax. plot(x, x**2, label =‘X Squared’)
ax. plot(x, x**3, label =‘X Cubed’)
ax.legend()
How do you change position of the legend?
number 0-10 for different positions. Use 0 to make it find the “best” location
ax.legend(loc = number)
How do you add color to a line plot?
color can be color name, e.g. ‘green’, or RGB Hex Code starting with
ax.plot(x, y, color = ‘color’)
How do you modify the line width and transparency?
alpha defines transparency
ax.plot(x, y, linewidth = num, alpha = num)
or
ax.plot(x, y, lw = num, alpha = num)
How do you modify the linestyle?
style looks like ‘–’ or ‘:’ . More styles available
ax.plot(x, y, linestyle = ‘style’)
or
ax.plot(x, y, ls = ‘style’)
How do you add markers to each point on a line plot?
How do you modify the marker size?
Several marker styles available
ax.plot(x, y, marker = ‘marker’, markersize = num)
How do you set the max num in the x and y axes?
ax. set_xlim([lower, upper])
ax. set_ylim([lower, upper])
seaborn is built on top of
matplotlib
How do you make a histogram with seaborn?
This will also plot a kde on top unless set kde = False
sns.distplot(df[col1], bins=num)
Use seasborn to compare distributions of two variables
kind=scatter is default, can also use ‘reg’ for regression line, etc
sns.jointplot(x=’col1’, y=’col2’, data=df, kind=’scatter’)
How do you plot pairwise relationships across entire dataframe in Seaborn?
will produce scatter plots for all pairs of numerical values
sns.pairplot(df)
sns.pairplot(df, hue=’col2’)
Draw dash mark for every single point in col1 to show distribution using Seaborn
sns.rugplot(df[‘col1’])
Draw KDE plot using Seaborn
sns.kdeplot(df[‘col1’])
Create a barplot using Seaborn
x is categorical, y is numerical
sns.barplot(x=’col1’, y=’col2’, data=df)
Make a barplot of the counts of a categorical variable using Seaborn
sns.countplot(x=’col1’, data=df)
Make a boxplot using Seaborn
x is categorical, y is numerical
sns.boxplot(x=’col1’, y=’col2’, data=df, hue=’col3’)
Create violinplot using Seaborn
x is categorical, y is numerical
sns.violinplot(x=’col1’, y=’col2’, data=df)
Create a striplot using seaborn
x is categorical, y is numerical
sns.stripplot(x=’col1’, y=’col2’, data=df, jitter=True)
Create a swarmplot using Seaborn
Combo of striplot and violin plot
sns.swarmplot(x=’col1’, y=’col2’, data=df)
what does sns.factorplot do?
It allows you to create any kind of plot with its kind= option
for a heatmap to work properly, data should be in what form?
Matrix form. Can either use df.corr() or pivot table method to turn df into matrix
Heatmap and clustermap using seaborn
sns. heatmap(matrix)
sns. clustermap(matrix)
Create a simple regression plot with Seaborn
sns.lmplot(x=’col1’, y=’col2’, data=df)
how can you look up the palettes used in seaborn?
Google matplotlib colormap