0% found this document useful (0 votes)
3 views5 pages

Matplotlib Seaborn Visualizations

The document provides Python scripts using Matplotlib to create various visualizations including multi-panel plots, investment growth line plots, scatter plots, and line plots with error bars. Each section includes code examples and descriptions of the visualizations created, highlighting their purpose and features. The scripts demonstrate how to effectively visualize data relationships and uncertainties.

Uploaded by

vijayanayak0893
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Matplotlib Seaborn Visualizations

The document provides Python scripts using Matplotlib to create various visualizations including multi-panel plots, investment growth line plots, scatter plots, and line plots with error bars. Each section includes code examples and descriptions of the visualizations created, highlighting their purpose and features. The scripts demonstrate how to effectively visualize data relationships and uncertainties.

Uploaded by

vijayanayak0893
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Matplotlib and Seaborn Visualizations

1) Write a Python script using Matplotlib to create a multi-panel plot with 2 rows and 2 columns.

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(0, 10, 100)

y = np.sin(x)

fig, axs = plt.subplots(2, 2, figsize=(10, 8))

# Line plot

axs[0, 0].plot(x, y)

axs[0, 0].set_title('Line Plot')

# Scatter plot

axs[0, 1].scatter(x, y)

axs[0, 1].set_title('Scatter Plot')

# Histogram

axs[1, 0].hist(y, bins=20)

axs[1, 0].set_title('Histogram')

# Bar chart

axs[1, 1].bar([1, 2, 3], [3, 2, 5])


axs[1, 1].set_title('Bar Chart')

# Adjust layout

plt.tight_layout()

plt.show()

This script creates a 2x2 grid of subplots, where each subplot represents a different type of plot.

The first subplot shows a line plot of a sine wave. The second subplot shows a scatter plot of the same data.

The third subplot displays a histogram of the sine values, and the fourth subplot shows a simple bar chart.

The layout of the subplots is adjusted using tight_layout() to ensure that the labels do not overlap.

2) Develop a Python program that generates a simple line plot showing the growth of an investmen

import matplotlib.pyplot as plt

# Investment parameters

initial_amount = 1000

growth_rate = 0.05

years = 10

time = range(years + 1)

# Calculate the investment value over time

investment_value = [initial_amount * (1 + growth_rate) ** t for t in time]

# Plot

plt.plot(time, investment_value, marker='o')


plt.grid(True)

plt.title('Investment Growth Over Time')

plt.xlabel('Years')

plt.ylabel('Investment Value ($)')

plt.show()

This code simulates the growth of an investment over time, assuming an initial amount and an annual growth rate.

The line plot shows the value of the investment after each year for a period of 10 years. Grid lines are added to improve

readability.

The x-axis represents time (years), and the y-axis represents the investment value in dollars.

3) Write a Python program that takes two lists of numerical data and creates a scatter plot using Ma

import matplotlib.pyplot as plt

# Sample data

height = [150, 160, 170, 180, 190]

weight = [50, 60, 65, 75, 80]

# Scatter plot

plt.scatter(height, weight, color='red', marker='x', label='Data points')

plt.title('Height vs Weight')

plt.xlabel('Height (cm)')

plt.ylabel('Weight (kg)')

plt.legend()

plt.show()
This script creates a scatter plot comparing height and weight. The data points are represented by red 'x' markers.

The legend indicates the data points, and axis labels are customized to represent height in centimeters and weight in

kilograms.

Scatter plots are useful for visualizing relationships between two variables.

4) Create a Python script that generates a line plot with error bars, representing the uncertainty in m

import matplotlib.pyplot as plt

import numpy as np

# Data

x = np.linspace(0, 10, 10)

y = np.sin(x)

yerr = 0.2 # Error in measurements

# Line plot with error bars

plt.errorbar(x, y, yerr=yerr, fmt='-o', color='blue', ecolor='red', elinewidth=2, capsize=5)

plt.title('Line Plot with Error Bars')

plt.xlabel('X')

plt.ylabel('Y')

plt.show()

This code generates a line plot of the sine function with error bars. The error bars represent the uncertainty in the

y-values.

The 'yerr' parameter specifies the error magnitude, and the 'ecolor' and 'elinewidth' parameters control the appearance
of the error bars.

Error bars are commonly used to show the reliability of measurements in scientific data visualization.

You might also like