Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Inflation Rate Visualization | Economic Data Analysis with Python
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python for Economists

bookChallenge: Inflation Rate Visualization

You are ready to put your skills into practice by visualizing inflation rates for several countries across a decade. This challenge requires you to work with a hardcoded pandas DataFrame containing inflation data, and then use matplotlib to create a clear, informative line plot. Each country's inflation rate should be shown as a distinct line on the same plot, with a legend to identify them and properly labeled axes. This kind of visualization is essential for economists to quickly compare inflation trends across countries and time periods.

12345678910111213141516171819202122232425262728293031
import pandas as pd import matplotlib.pyplot as plt # Hardcoded inflation data for several countries over 10 years data = { "Year": [2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021], "USA": [2.1, 1.5, 1.6, 0.1, 1.3, 2.1, 2.4, 1.8, 1.2, 4.7], "Germany": [2.0, 1.5, 0.9, 0.2, 0.5, 1.5, 1.9, 1.4, 0.5, 3.1], "Japan": [0.0, 0.4, 2.7, 0.8, -0.1, 0.5, 1.0, 0.5, 0.0, -0.2], "Brazil": [5.4, 6.2, 6.3, 9.0, 8.7, 3.4, 3.7, 3.7, 3.2, 8.7] } df = pd.DataFrame(data) def plot_inflation_rates(df): """ Plots inflation rates for each country over time. """ plt.figure(figsize=(10, 6)) for country in df.columns: if country != "Year": plt.plot(df["Year"], df[country], marker="o", label=country) plt.xlabel("Year") plt.ylabel("Inflation Rate (%)") plt.title("Inflation Rates by Country (2012-2021)") plt.legend() plt.grid(True) plt.tight_layout() plt.show() # Call the function to display the plot plot_inflation_rates(df)
copy
Aufgabe

Swipe to start coding

Write a function called plot_inflation_rates that takes a pandas DataFrame with a "Year" column and one column per country (containing inflation rates). The function should:

  • Create a line plot using matplotlib.
  • Plot each country's inflation rate as a separate line on the same plot.
  • Add a legend to identify each country.
  • Label the x-axis as Year and the y-axis as Inflation Rate (%).
  • Display the plot.

Use the following DataFrame for testing:

  • Year: [2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021].
  • USA: [2.1, 1.5, 1.6, 0.1, 1.3, 2.1, 2.4, 1.8, 1.2, 4.7].
  • Germany: [2.0, 1.5, 0.9, 0.2, 0.5, 1.5, 1.9, 1.4, 0.5, 3.1].
  • Japan: [0.0, 0.4, 2.7, 0.8, -0.1, 0.5, 1.0, 0.5, 0.0, -0.2].
  • Brazil: [5.4, 6.2, 6.3, 9.0, 8.7, 3.4, 3.7, 3.7, 3.2, 8.7].

Lösung

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 7
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you explain how the plot helps compare inflation trends between countries?

What do the inflation trends suggest about each country's economy during this period?

Can you help me modify the plot to focus on just two countries?

close

bookChallenge: Inflation Rate Visualization

Swipe um das Menü anzuzeigen

You are ready to put your skills into practice by visualizing inflation rates for several countries across a decade. This challenge requires you to work with a hardcoded pandas DataFrame containing inflation data, and then use matplotlib to create a clear, informative line plot. Each country's inflation rate should be shown as a distinct line on the same plot, with a legend to identify them and properly labeled axes. This kind of visualization is essential for economists to quickly compare inflation trends across countries and time periods.

12345678910111213141516171819202122232425262728293031
import pandas as pd import matplotlib.pyplot as plt # Hardcoded inflation data for several countries over 10 years data = { "Year": [2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021], "USA": [2.1, 1.5, 1.6, 0.1, 1.3, 2.1, 2.4, 1.8, 1.2, 4.7], "Germany": [2.0, 1.5, 0.9, 0.2, 0.5, 1.5, 1.9, 1.4, 0.5, 3.1], "Japan": [0.0, 0.4, 2.7, 0.8, -0.1, 0.5, 1.0, 0.5, 0.0, -0.2], "Brazil": [5.4, 6.2, 6.3, 9.0, 8.7, 3.4, 3.7, 3.7, 3.2, 8.7] } df = pd.DataFrame(data) def plot_inflation_rates(df): """ Plots inflation rates for each country over time. """ plt.figure(figsize=(10, 6)) for country in df.columns: if country != "Year": plt.plot(df["Year"], df[country], marker="o", label=country) plt.xlabel("Year") plt.ylabel("Inflation Rate (%)") plt.title("Inflation Rates by Country (2012-2021)") plt.legend() plt.grid(True) plt.tight_layout() plt.show() # Call the function to display the plot plot_inflation_rates(df)
copy
Aufgabe

Swipe to start coding

Write a function called plot_inflation_rates that takes a pandas DataFrame with a "Year" column and one column per country (containing inflation rates). The function should:

  • Create a line plot using matplotlib.
  • Plot each country's inflation rate as a separate line on the same plot.
  • Add a legend to identify each country.
  • Label the x-axis as Year and the y-axis as Inflation Rate (%).
  • Display the plot.

Use the following DataFrame for testing:

  • Year: [2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021].
  • USA: [2.1, 1.5, 1.6, 0.1, 1.3, 2.1, 2.4, 1.8, 1.2, 4.7].
  • Germany: [2.0, 1.5, 0.9, 0.2, 0.5, 1.5, 1.9, 1.4, 0.5, 3.1].
  • Japan: [0.0, 0.4, 2.7, 0.8, -0.1, 0.5, 1.0, 0.5, 0.0, -0.2].
  • Brazil: [5.4, 6.2, 6.3, 9.0, 8.7, 3.4, 3.7, 3.7, 3.2, 8.7].

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 7
single

single

some-alt