Plotting directl y u sing pandas P YTH ON FOR R U SE R S Daniel Chen Instr u ctor
Plotting in P y thon Q u ickl y sho w data pa � erns Plo � ing methods in P y thon : Pandas Seaborn Matplotlib PYTHON FOR R USERS
Pandas plot method plot() method hist : histogram box : bo x plot Works on the pandas kde : Kernel Densit y DataFrame and Series objects Estimation plot Pass plot the kind density : same as ‘ kde ’ arg u ment area : area plot kind of plots : pie : pie plot line : line plot ( defa u lt ) scatter : sca � er plot bar : v ertical bar plot hexbin : he x bin plot barh : hori z ontal bar plot PYTHON FOR R USERS
Uni v ariate : Histogram import matplotlib.pyplot as plt iris['sepal_length'].plot(kind='hist') plt.show() PYTHON FOR R USERS
Uni v ariate : Bar plot cts = iris['species'].value_counts() cts.plot(kind='bar') plt.show() PYTHON FOR R USERS
Bi v ariate : Scatter plot iris.plot(kind='scatter', x='Sepal.Length', y='Sepal.Width' plt.show() PYTHON FOR R USERS
Bi v ariate : Bo x plots iris.plot(kind='box') plt.show() PYTHON FOR R USERS
Bi v ariate : Bo x plots iris.boxplot(by='Species', column='Sepal.Length') plt.show() PYTHON FOR R USERS
Let ' s practice ! P YTH ON FOR R U SE R S
Seaborn P YTH ON FOR R U SE R S Daniel Chen Instr u ctor
Seaborn barplots : barplot histograms : displot bo x plot : boxplot sca � er plot : regplot Seaborn bene � ts colored points b y data facet plots b y data PYTHON FOR R USERS
Seaborn histograms import seaborn as sns import matplotlib.pyplot as plt sns.distplot(iris['sepal_length']) plt.show() PYTHON FOR R USERS
Seaborn co u nt plot sns.countplot('species', data=iris) plt.show() PYTHON FOR R USERS
Seaborn bo x plots sns.boxplot(x='species', y='sepal_length', data=iris) plt.show() PYTHON FOR R USERS
Seaborn scatterplots sns.regplot(x='sepal_length', y='sepal_width', data=iris) plt.show() PYTHON FOR R USERS
Seaborn scatterplots w/ o u t regression line sns.regplot(x='sepal_length', y='sepal_width', data=iris, fit_reg=False) plt.show() PYTHON FOR R USERS
Seaborn facets sns.lmplot(x='sepal_length', y='sepal_width', data=iris, fit_reg=False, col='species') plt.show() PYTHON FOR R USERS
Seaborn FacetGrid g = sns.FacetGrid(iris, col="species") g = g.map(plt.hist, "sepal_length") plt.show() PYTHON FOR R USERS
Let ' s practice ! P YTH ON FOR R U SE R S
Matplotlib P YTH ON FOR R U SE R S Daniel Chen Instr u ctor
Matplotlib plots import matplotlib.pyplot as plt plt.hist(iris['sepal_length']) plt.show() PYTHON FOR R USERS
Matplotlib scatter plt.scatter(iris['sepal_length'], iris['sepal_width']) plt.show() PYTHON FOR R USERS
Polishing u p the fig u re fig, ax = plt.subplots() ax.scatter(iris['sepal_length'], iris['sepal_width']) ax.set_title('Sepal Length') ax.set_xlabel('Sepal Length') ax.set_ylabel('Sepal Width') plt.show() PYTHON FOR R USERS
Rotating a x is ticks fig, ax = plt.subplots() ax.scatter(iris['sepal_length'], iris['sepal_width']) ax.set_title('Sepal Length') ax.set_xlabel('Sepal Length') ax.set_ylabel('Sepal Width') plt.xticks(rotation=45) # rotate the x-axis ticks plt.show() PYTHON FOR R USERS
Parts of a matplotlib fig u re PYTHON FOR R USERS
Parts of a matplotlib fig u re 2 PYTHON FOR R USERS
Fig u res and a x es fig, ax = plt.subplots() ax.scatter(iris['sepal_length'], iris['sepal_width']) plt.show() PYTHON FOR R USERS
M u ltiple a x es fig, (ax1, ax2) = plt.subplots(1, 2) ax1.scatter(iris['sepal_length'], iris['sepal_width']) ax2.hist(iris['sepal_length']) plt.show() PYTHON FOR R USERS
Remember fig, ax = plt.subplots() sns.regplot(x='sepal_length', y='sepal_width', data=iris, fit_reg=False, ax=ax) plt.show() PYTHON FOR R USERS
Clearing the fig u re fig, (ax1, ax2) = plt.subplots(1, 2) ax1.scatter(iris['sepal_length'], iris['sepal_width']) ax2.hist(iris['sepal_length']) plt.show() plt.clf() PYTHON FOR R USERS
Let ' s practice ! P YTH ON FOR R U SE R S
Recommend
More recommend