Bar Chart
Plot a horizontal bar chart of the data contained in the df Pandas DataFrame.
lab val 0 A 10 1 B 30 2 C 20
import matplotlib.pyplot as plt import seaborn as sns sns.barplot(x="lab", y="val", data=df) plt.show()
Count Plot
Pet owners were surveyed to determine their preference for dogs, cats or both. Visualize the response data contained in the list pets.
import matplotlib.pyplot as plt import seaborn as sns pets = ["cats", "cats", "dogs", "both", "dogs", "both", "cats", "cats", "cats", "dogs", "dogs", "dogs", "dogs", "cats", "cats", "both"] sns.countplot(pets) plt.show()
Histogram
Plot a histogram of the pH variable in the wine_quality DataFrame using 40 bins.
import matplotlib.pyplot as plt import seaborn as sns sns.distplot(wine_quality['pH'], hist=True, rug=False, kde=False, bins=40) plt.show()
Plot a histogram of the Ca variable in the glass DataFrame.
import matplotlib.pyplot as plt import seaborn as sns sns.distplot(glass['Ca'], hist=True, rug=False, kde=False) plt.show()
Box Plot
Plot boxplots for the sepal_length numerical variable and group according to species.
The data is contained in the iris DataFrame.
The boxplots should be ordered as follows: “virginica”, “versicolor” and “setosa”.
import matplotlib.pyplot as plt import seaborn as sns sns.set_context(rc={"font.size":18}) ax = sns.boxplot( x="species", y="sepal_length", data=iris, order=["virginica", "versicolor", "setosa"]) plt.show()
Scatter Plot
Use a + to indicate the points on the scatter plot.
import matplotlib.pyplot as plt import seaborn as sns ax = sns.scatterplot(x="citric acid", y="pH", data=wine, marker="+") plt.show()
Add a green vertical reference line to the plot at x=50.
import matplotlib.pyplot as plt import seaborn as sns sns.scatterplot('x', 'y', data=df) plt.axvline(50, color='green') plt.show()
Label the x axis as Sepal Length (cm) and the y axis as Sepal Width (cm)
import matplotlib.pyplot as plt import seaborn as sns sns.scatterplot(x="sepal_length", y="sepal_width", data=df) plt.xlabel("Sepal Length (cm)") plt.ylabel("Sepal Width (cm)") plt.show()
Set the overall font size to be used in plots to 20.
import matplotlib.pyplot as plt import seaborn as sns sns.set_context(rc={"font.size":20}) ax = sns.scatterplot(x="total_bill", y="tip", data=tips) plt.show()
Facet Grid
Initialize a 2×2 grid of facets using the tips dataset. The time variable should determine the number of columns and the smoker variable should determine the number of rows.
import matplotlib.pyplot as plt import seaborn as sns g = sns.FacetGrid(tips, col="time", row="smoker") plt.show()
Line Plot
Plot the monthly dam level height contained in the dam_level DataFrame.
--dam_level date level 2019-01-01 48.6 2019-02-01 46.7 2019-03-01 44.8
import matplotlib.pyplot as plt import seaborn as sns sns.lineplot(x='date',y='level', data=dam_level) plt.xticks(rotation=45) plt.show()
Plot a line plot of Score as a function of Overall rank. The variables are contained in the happiness DataFrame.
import matplotlib.pyplot as plt import seaborn as sns ax = sns.lineplot(x="Overall rank", y="Score", data=happiness) plt.show()
Joint Plot
Plot the regression and kernel density fits for the BPM and Popularity variables in the top50 DataFrame.
import matplotlib.pyplot as plt import seaborn as sns g = sns.jointplot(x="BPM",y="Popularity",data=top50,kind="reg") plt.show()
Pair Plot
Create a pair plot of the song_metrics dataset.
import matplotlib.pyplot as plt import seaborn as sns sns.pairplot(song_metrics) plt.show()
Kernel Density Estimation / KDE Plot
Plot the distributions of the Mg and Al variables contained in the glass DataFrame on the same plot using kernel density estimation. Shade the distributions.
import matplotlib.pyplot as plt import seaborn as sns sns.kdeplot(glass['Mg'], shade=True) sns.kdeplot(glass['Al'], shade=True) plt.show()