Visualizing Data Trends with Matplotlib
This challenge will guide you through creating basic but informative data visualizations using the matplotlib library in Python. Visualizing data is crucial for understanding patterns, identifying outliers, and communicating insights effectively. You will practice generating a line plot and a bar chart from provided datasets.
Problem Description
Your task is to create two distinct visualizations using Python's matplotlib.pyplot module:
- A Line Plot: Represent the trend of a dataset over time. This plot should clearly show the progression of values.
- A Bar Chart: Compare the magnitudes of different categories. This chart should allow for easy comparison between discrete items.
Key Requirements:
- Use the
matplotlib.pyplotmodule for all plotting. - Label your axes appropriately (e.g., "Time", "Value", "Category", "Count").
- Provide a clear title for each plot.
- Ensure the plots are displayed correctly.
Expected Behavior:
When your Python script is run, two separate plots should appear: a line plot and a bar chart.
Edge Cases/Considerations:
- Ensure your data is in a format that
matplotlibcan readily use (e.g., lists or NumPy arrays). - Consider what happens if the input lists are empty. While this specific challenge may not test this, it's good practice to think about.
Examples
Example 1: Line Plot Data
Input:
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
sales = [150, 180, 220, 200, 250, 280]
Output: A line plot showing the sales values against the months.
- X-axis: Months ('Jan' to 'Jun')
- Y-axis: Sales figures
- Title: "Monthly Sales Trend"
- Plot Type: Line plot
Explanation: The line plot visually connects the sales figures for each month, illustrating the upward trend in sales over the first half of the year.
Example 2: Bar Chart Data
Input:
products = ['Apples', 'Bananas', 'Oranges', 'Grapes']
inventory = [120, 250, 90, 180]
Output: A bar chart showing the inventory for each product.
- X-axis: Product names
- Y-axis: Inventory count
- Title: "Fruit Inventory Levels"
- Plot Type: Bar chart
Explanation: The bar chart displays a distinct bar for each fruit, with the height of the bar representing its inventory count, making it easy to compare which fruits have more stock.
Constraints
- The input data will be provided as Python lists of equal length.
- The numerical data in the lists will consist of integers.
- The total number of data points for each plot will not exceed 20.
- You are expected to import
matplotlib.pyplotasplt.
Notes
- You will need to have
matplotlibinstalled. If not, you can install it using pip:pip install matplotlib. - For the line plot, you'll likely use
plt.plot(). - For the bar chart, you'll likely use
plt.bar(). - Remember to call
plt.xlabel(),plt.ylabel(), andplt.title()to label your plots. - Finally,
plt.show()is necessary to display the generated plots.