graphs in matplotlib


Matplotlib offers a wide range of graphs and customization options for data visualization. 


Line Plot:

Line plots represent data points as a series of connected data points with lines. They are commonly used to show trends over time.

Real-Life Example: Showing the temperature changes over a week.

Options:

plt.plot(x, y, label='label'): Create a line plot.

plt.xlabel('x-label') and 

plt.ylabel('y-label'): Add axis labels.

plt.title('Title'): Add a title.

plt.legend(): Display labels for multiple lines.

plt.grid(True): Add a grid.


example:

import matplotlib.pyplot as plt

days = [1, 2, 3, 4, 5, 6, 7]

temperatures = [75, 76, 74, 73, 78, 80, 79]

plt.plot(days, temperatures, label='Temperature')

plt.xlabel('Days')

plt.ylabel('Temperature (°F)')

plt.title('Weekly Temperature Changes')

plt.legend()

plt.grid(True)

plt.show()



Bar Chart:

Bar charts are used to represent data as bars of different heights. They are useful for comparing categories.

Real-Life Example: Comparing the sales of different products.

Options:

plt.bar(x, height, label='label'): Create a bar chart.

plt.xticks(x, labels): Customize x-axis labels.


example:

import matplotlib.pyplot as plt

products = ['Product A', 'Product B', 'Product C', 'Product D']

sales = [120, 200, 90, 150]

plt.bar(products, sales, label='Sales')

plt.xlabel('Products')

plt.ylabel('Sales')

plt.title('Product Sales Comparison')

plt.legend()

plt.show()



Histogram:

Histograms are used to visualize the distribution of a dataset. They represent data as bars, with each bar representing a range of values.

Options:

plt.hist(data, bins, label='label'): Create a histogram.


Real-Life Example: Analyzing the distribution of exam scores.

import matplotlib.pyplot as plt


exam_scores = [78, 85, 92, 60, 72, 88, 95, 66, 78, 84, 91, 70, 75, 89, 96]

bins = [60, 70, 80, 90, 100]


plt.hist(exam_scores, bins, edgecolor='black', label='Scores')

plt.xlabel('Score Range')

plt.ylabel('Frequency')

plt.title('Exam Score Distribution')

plt.legend()

plt.show()


Scatter Plot:

Scatter plots are used to visualize the relationship between two variables. They display data points as individual dots on a 2D plane.

Options:

plt.scatter(x, y, label='label'): Create a scatter plot.

Real-Life Example: Visualizing the relationship between a student's study hours and exam scores.

import matplotlib.pyplot as plt


study_hours = [2, 3, 4, 5, 6, 7, 8]

exam_scores = [65, 72, 80, 85, 88, 92, 95]


plt.scatter(study_hours, exam_scores, label='Scores', color='red')

plt.xlabel('Study Hours')

plt.ylabel('Exam Scores')

plt.title('Study Hours vs. Exam Scores')

plt.legend()

plt.show()


Pie Chart:

Pie charts represent data as a circle divided into slices, with each slice representing a portion of the whole.

Options:

x data (slices).

labels for slice labels.

colors for slice colors.

explode to emphasize a slice.

autopct for displaying percentages.

title.

plt.pie(sizes, labels=labels, autopct='%1.1f%%'): Create a pie chart with percentage labels.


Real-Life Example: Showing the distribution of expenses in a monthly budget.

import matplotlib.pyplot as plt


expenses = [500, 300, 200, 400]

categories = ['Housing', 'Food', 'Transportation', 'Entertainment']


plt.pie(expenses, labels=categories, autopct='%1.1f%%')

plt.title('Monthly Budget Allocation')

plt.show()



assignment with subplot:




import matplotlib.pyplot as plt

plt.figure(figsize=(10,5))


plt.subplot(2, 3, 1)

days = [1, 2, 3, 4, 5, 6, 7]

temperatures = [75, 76, 74, 73, 78, 80, 79]

plt.plot(days, temperatures, label='Temperature')

plt.xlabel('Days')

plt.ylabel('Temperature (°F)')

plt.title('Weekly Temperature Changes')

plt.legend()

plt.grid(True)


plt.subplot(2, 3, 2)

products = ['Product A', 'Product B', 'Product C', 'Product D']

sales = [120, 200, 90, 150]

plt.bar(products, sales, label='Sales')

plt.xlabel('Products')

plt.ylabel('Sales')

plt.title('Product Sales Comparison')

plt.legend()

plt.grid(True)


plt.subplot(2, 3, 3)

exam_scores = [78, 85, 92, 60, 72, 88, 95, 66, 78, 84, 91, 70, 75, 89, 96]

bins = [60, 70, 80, 90, 100]

plt.hist(exam_scores, bins, edgecolor='black', label='Scores')

plt.xlabel('Score Range')

plt.ylabel('Frequency')

plt.title('Exam Score Distribution')

plt.legend()


plt.subplot(2, 3, 4)

study_hours = [2, 3, 4, 5, 6, 7, 8]

exam_scores = [65, 72, 80, 85, 88, 92, 95]

plt.scatter(study_hours, exam_scores, label='Scores', color='red')

plt.xlabel('Study Hours')

plt.ylabel('Exam Scores')

plt.title('Study Hours vs. Exam Scores')

plt.legend()

plt.show()





0 Comments