Matplotlib Seaborn Visualizations
Matplotlib Seaborn Visualizations
1) Write a Python script using Matplotlib to create a multi-panel plot with 2 rows and 2 columns.
import numpy as np
y = np.sin(x)
# Line plot
axs[0, 0].plot(x, y)
# Scatter plot
axs[0, 1].scatter(x, y)
# Histogram
axs[1, 0].set_title('Histogram')
# 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
# Investment parameters
initial_amount = 1000
growth_rate = 0.05
years = 10
time = range(years + 1)
# Plot
plt.xlabel('Years')
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
# Sample data
# Scatter plot
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 numpy as np
# Data
y = np.sin(x)
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.