Unit V Anseer Key
Unit V Anseer Key
16marks
. Surface Plot
A surface plot is a three-dimensional plot that displays a surface defined by a function z=f(x,y)z = f(x, y).
It is used to visualize the relationship between three variables.
import numpy as np
# Generate data
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Plot a surface
ax.plot_surface(x, y, z, cmap='viridis')
# Labels
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
ax.set_title('Surface Plot')
plt.show()
2. 3D Scatter Plot
Example:
import numpy as np
# Data
np.random.seed(0)
x = np.random.rand(50)
y = np.random.rand(50)
z = np.random.rand(50)
# Plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Labels
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis")
ax.set_zlabel("Z-axis")
plt.show()
---
Both plots utilize the projection='3d' argument to create the 3D axes and allow visualization in three
dimensions.
2. Explain about various visualization charts like line plots, scatter plots and histograms using Matplotlib
with an example.
1. Line Plot
A line plot is used to display data points connected by straight lines. It's useful for showing trends over
time or continuous data.
# Sample data
x = [0, 1, 2, 3, 4, 5]
plt.plot(x, y, marker='o')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.title('Line Plot')
plt.show()
2. Scatter Plot
A scatter plot is used to display individual data points as dots. It's useful for observing the relationship
between two variables.import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [5, 7, 2, 4, 9]
plt.scatter(x, y, color='r')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.title('Scatter Plot')
plt.show()
3. Histogram
A histogram is used to represent the distribution of data. It divides data into bins and displays the
frequency of each bin.
# Sample data
data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
# Create a histogram
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram')
plt.show()
3. Briefly explain about visualization with Seaborn. Give an example working code segment that
represents a 2D kernel density plot for any data.
Seaborn is a powerful Python visualization library based on Matplotlib that provides a high-level
interface for drawing attractive and informative statistical graphics. Seaborn simplifies the process of
creating complex visualizations and offers various built-in themes and color palettes to make your plots
aesthetically pleasing.
Statistical Plots: Provides advanced plot types like violin plots, box plots, and pair plots.
A kernel density plot is used to estimate the probability density function of a continuous random
variable. A 2D kernel density plot visualizes the joint distribution of two variables.
import numpy as np
np.random.seed(42)
x = np.random.normal(size=100)
y = np.random.normal(size=100)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
1. sns.kdeplot:
Parameters:
2. Data: The data is randomly generated from a normal distribution for simplicity.
---
Use Case
2D Kernel Density Plots are particularly useful for visualizing joint distributions, such as:
4. Develop a code snippet that projects our globe as a 2-D flat surface (using cylindrical project) and
convey information about the location of any three major Indian cities I the map(using scatter plot)
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 6))
llcrnrlon=-180, urcrnrlon=180)
m.drawcoastlines(color='gray')
m.drawcountries(color='black')
m.fillcontinents(color='lightyellow', lake_color='lightblue')
m.drawmapboundary(fill_color='lightblue')
cities = {
plt.legend(loc='lower left')
plt.show()
---
Explanation:
1. Basemap Setup:
The Basemap object is initialized with a cylindrical projection (projection='cyl'), suitable for global maps.
The llcrnrlat, urcrnrlat, llcrnrlon, urcrnrlon define the latitude and longitude bounds.
2. Cities Data:
Three cities (New Delhi, Mumbai, and Chennai) are defined with their respective latitude and longitude.
3. Scatter Plot:
4. Map Features:
Coastlines, country borders, continents, and map boundaries are drawn for clarity.