plots Flashcards
basic bar chart
plt.bar(x, y) # x is list of categoryNames # y is list of quantity to visualize
basic pie chart
plt.pie(y, labels = x) # x is list of categoryNames # y is list of quantity to visualize
Pareto chart assuming df.col1 on x axis, [‘col1_quantity’], and [‘cumpercentage’]
from matplotlib.ticker import PercentFormatter
fig, ax = plt.subplots(figsize=(20,10))
ax.bar(df[‘col1’], df[“col1_quantity”], color=”C0”)
ax2 = ax.twinx()
ax2.plot(df[‘col1’], df[“cumpercentage”], color=”C1”, marker=”D”, ms=7)
ax2.yaxis.set_major_formatter(PercentFormatter())
ax.tick_params(axis=”y”, colors=”C0”)
ax2.tick_params(axis=”y”, colors=”C1”)
plt.title(‘pareto chart’)
plt.show()
histogram with relative frequency for an array
plt. hist(arr, bins=10, density=True)
plt. ylabel(‘Relative Frequency’)
plt. xticks(intervals)
histogram with absolute frequency for an array
plt. figure(figsize = (20,10))
plt. hist(arr, bins=10)
plt. ylabel(‘Absolute Frequency’)
plt. xticks(intervals)
create side by side bar chart for two level variables. Specify what you need to do with df first.
eg, col1 is gender, col2 is country, and col3 is the quantity to visualize
g = sns.catplot(x='col1', y='col3', hue='col2', data=df, kind='bar') g.fig.set_size_inches(20,10) # need to melt df first
adjust seaboard settings
sns.set_context(“notebook”, rc={“font.size”:16,”axes.titlesize”:20,”axes.labelsize”:16})
basic scatter plot for arr1 and arr2
plt. scatter(arr1, arr2)
plt. title(“arr1 vs arr2”)
plt. show()
basic box plot for one array. Add optional argument to make it vertical
sns.boxplot(arr1, orient=’v’ )
basic violin plot for one array
sns.violinplot(arr1)
basic structure to create two subplots side by side
f, axes = plt.subplots(1, 2)
sns. boxplot(arr1, ax=axes[0] )
sns. boxplot(arr2, ax=axes[1])
name the x-axis using appropriate font size
plt.xlabel(‘TEXT’, fontsize = 20)
line graph with thickness of line, name of line, and color
plt.plot(x, yhat, lw=4, c=’orange’, label =’regression line’)
imports to make plots in seaboard
import seaborn as sns
sns.set()
scatter plot with regression line. independent variable is x1, dependent variable is yhat.
plt.scatter(x1,y)
fig = plt.plot(x1,yhat, lw=4, c=’orange’, label =’regression line’)
plt.show()