Search⌘ K
AI Features

Solution: Plotting

Explore how to generate bar plots, stacked bar plots, and histograms from pandas DataFrames. Learn to visualize data effectively by customizing chart labels and saving plots as PNG images for analysis.

Solution 1

Given the following DataFrame df:

Snail Cat Dog Elephant Rabbit Horse Cheetah
Lifespan 2 12 10 50 9 25 10
Speed 0.1 45 45 40 50 55 100

a) Create a bar plot.

b) Create a stacked bar plot.

Create a bar plot

Python 3.10.4
import pandas as pd
df=pd.DataFrame({
"": ['Lifespan','Speed'],
"Snail": [2,0.1],
"Cat" : [12, 45],
"Dog" : [10,45],
"Elephant" : [50,40],
"Rabbit" : [9,50],
"Horse" : [25,55],
"Cheetah" : [10,100]
})
plt = df.plot.bar(rot=0)
plt.get_figure().savefig("output/graph.png")
  • Line 13: We create a bar chart of the data in the ...