CLASS 12 Matplotlib BASIC NOTES FOR BOARD EXAM

 


Data Visualization is the graphical representation of data to help users understand patterns, trends, and insights. Visualization tools like Matplotlib are commonly used to transform raw data into visual forms, which makes analysis more accessible and meaningful.

Purpose of Plotting

Plotting data has several key purposes:

  1. Simplifies Data Understanding: Visuals make data easier to interpret than raw numbers.
  2. Reveals Patterns and Trends: Allows us to spot trends, outliers, and relationships in the data.
  3. Enables Better Decision-Making: Data-driven decisions are easier to make with clear, visualized insights.
  4. Communicates Information Effectively: Plots can simplify complex data, making it easier to present findings to others.

Common Types of Plots Using Matplotlib

Matplotlib is a Python library used to create a variety of static, animated, and interactive plots.

1. Line Plot

A line plot is used to display data points connected by a continuous line. It is ideal for visualizing trends over time or a sequence.

Code Example:

 

Saving the Plot:

 

 

 2. Bar Graph

A bar graph displays data using rectangular bars, ideal for comparing quantities across categories.

Code Example:


 

3. Histogram

A histogram shows the distribution of data by grouping values into bins and plotting the frequency of values in each bin. It’s ideal for understanding data distribution.

Code Example:

 

 

 

In Python, with libraries like Matplotlib, you can customize plots by adding labels, titles, and legends to make them more informative. Here’s a quick guide on how to do it.

1. Adding Labels to the Axes

Use plt.xlabel() and plt.ylabel() to label the x and y axes:

import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [10, 15, 13, 18, 16] # Plotting plt.plot(x, y) # Adding labels plt.xlabel('X-Axis Label') plt.ylabel('Y-Axis Label')

2. Adding a Title

Use plt.title() to add a title to your plot:


plt.title('Sample Plot Title')

3. Adding a Legend

Use plt.legend() to add a legend, which is especially useful when you have multiple lines or datasets in a single plot. You can either set the label within the plot function or pass it to legend().


# Plotting with labels for legend plt.plot(x, y, label='Dataset 1') # Display legend plt.legend()

Complete Example

Here’s a complete example putting it all together:


import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y1 = [10, 15, 13, 18, 16] y2 = [8, 14, 12, 17, 15] # Plotting plt.plot(x, y1, label='Dataset 1', color='blue') plt.plot(x, y2, label='Dataset 2', color='green') # Adding labels, title, and legend plt.xlabel('X-Axis') plt.ylabel('Y-Axis') plt.title('Customized Plot with Labels and Legend') plt.legend() # Show plot plt.show()

 

Adding Labels to a Plot

To add labels to the x-axis and y-axis in a Matplotlib plot, you can use plt.xlabel() and plt.ylabel(). Here’s a basic example that demonstrates how to add these labels to a plot.

Example: Adding Labels to a Plot

import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # Creating a plot plt.plot(x, y, color='blue', marker='o', linestyle='--') # Adding labels to the axes plt.xlabel('Time (s)') # Label for the x-axis plt.ylabel('Distance (m)') # Label for the y-axis # Adding a title to the plot plt.title('Distance vs Time') # Show the plot plt.show()

Explanation

  • plt.xlabel('Time (s)'): Sets the label for the x-axis, here labeled as “Time (s)”.
  • plt.ylabel('Distance (m)'): Sets the label for the y-axis, here labeled as “Distance (m)”.
  • plt.title('Distance vs Time'): Adds a title to the plot, here titled as “Distance vs Time”.

This customization makes the plot clearer, especially in conveying what each axis represents. You can replace "Time (s)" and "Distance (m)" with any other descriptive text that fits your data.

output



Adding a Title to a Plot

 To add a title to a plot in Matplotlib, you can use the plt.title() function. This helps provide context for what the plot represents.

Example: Adding a Title to a Plot


import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [3, 7, 8, 5, 10] # Creating a plot plt.plot(x, y, color='purple', marker='x', linestyle='-') # Adding a title to the plot plt.title('Sales Growth Over Time') # Show the plot plt.show()

Explanation

  • plt.title('Sales Growth Over Time'): This adds a title to the plot, here titled as “Sales Growth Over Time.”

Adding a Legend to a Plot

In Matplotlib, a legend can be added to clarify the meaning of multiple lines or datasets in a plot. Use the plt.legend() function to add this, which will display labels defined in label attributes within the plot.

Example: Adding a Legend to a Plot

import matplotlib.pyplot as plt # Sample data for two datasets x = [1, 2, 3, 4, 5] y1 = [3, 7, 8, 5, 10] y2 = [2, 6, 7, 4, 9] # Creating multiple plots with labels plt.plot(x, y1, color='purple', marker='o', linestyle='-', label='Product A') plt.plot(x, y2, color='orange', marker='x', linestyle='--', label='Product B') # Adding labels, title, and legend plt.xlabel('Time (Months)') plt.ylabel('Sales') plt.title('Sales Comparison Over Time') # Displaying the legend plt.legend() # This will show labels for Product A and Product B # Show the plot plt.show()

Explanation

  • label='Product A' and label='Product B': These labels are assigned to each plot, defining what each line represents.
  • plt.legend(): This command displays the legend box, which shows the labels for Product A and Product B with their respective line styles.



Post a Comment

0 Comments