Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Create a Simple Bar Chart | Bar Charts
Visualization in Python with matplotlib

book
Create a Simple Bar Chart

Tâche

Swipe to start coding

You are given dataframe data with rainfall information for Indian cities/districts. Your tasks are:

  1. Filter the observations from data so that only rainfall data for 'NEW DELHI' will remain. Save this information within the new_delhi variable.
  2. Create Axes and Figure objects (use fix, ax variables).
  3. Initialize a bar chart with Month column values on the x-axis and Rainfall column values on the y-axis (new_delhi data).

Solution

# Import the libraries
import pandas as pd
import matplotlib.pyplot as plt

# Load the data
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/ed80401e-2684-4bc4-a077-99d13a386ac7/rainfall+in+india.csv', index_col = 0)

# Filter to certain city
new_delhi = data.loc['NEW DELHI']

# Create Axes and Figure objects
fig, ax = plt.subplots()

# Initialize the bar chart
ax.bar(new_delhi['Month'], new_delhi['Rainfall'])

# Display the plot
plt.show()

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 2
# Import the libraries
import pandas as pd
import matplotlib.pyplot as plt

# Load the data
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/ed80401e-2684-4bc4-a077-99d13a386ac7/rainfall+in+india.csv', index_col = 0)

# Filter to certain city
new_delhi = data.loc['___']

# Create Axes and Figure objects
fig, ax = plt.___()

# Initialize the bar chart
ax.bar(new_delhi['___'], ___)

# Display the plot
plt.show()
toggle bottom row
some-alt