0% found this document useful (0 votes)
17 views18 pages

16 Mark Ds

Uploaded by

dummyy0223
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)
17 views18 pages

16 Mark Ds

Uploaded by

dummyy0223
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/ 18

1

https://youtu.be/YUGq8e5JIOI?si=2wfBk_gisuT6OAzX

3
import pandas as pd
import numpy as np

# Define the dictionary data


exam_data = {
'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura',
'Kevin', 'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']
}

# Define the index labels


labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

# Create a DataFrame from the dictionary data with index labels


df = pd.DataFrame(exam_data, index=labels)

# Display the DataFrame


print(df)
Output
attempts name qualify score
a 1 Anastasia yes 12.5
b 3 Dima no 9.0
c 2 Katherine yes 16.5
d 3 James no NaN
e 2 Emily no 9.0
f 3 Michael yes 20.0
g 1 Matthew yes 14.5
h 1 Laura no NaN
i 2 Kevin no 8.0
j 1 Jonas yes 19.0

4
import pandas as pd

# Define the dictionary data


data = {
'X': [78, 85, 96, 80, 86],
'Y': [84, 94, 89, 83, 86],
'Z': [86, 97, 96, 72, 83]
}

# Create a DataFrame from the dictionary data


df = pd.DataFrame(data)

# Display the DataFrame


print(df)

OUTPUT
X Y Z
0 78 84 86
1 85 94 97
2 96 89 96
3 80 83 72
4 86 86 83

5
import numpy as np

# Define the array


array = np.array([1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9])

# Get the unique elements of the array


unique_elements = np.unique(array)

# Display the unique elements


print("Unique elements of the array:")
print(unique_elements)

OUTPUT
Unique elements of the array:
[1 2 3 4 5 6 7 8 9]

6
import numpy as np

# Define the arrays


array1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
array2 = np.array([4, 5, 6, 7, 8, 9, 10, 11, 12])

# Find the set difference between the two arrays


set_difference = np.setdiff1d(array1, array2)

# Display the set difference


print("Set difference between the two arrays:")
print(set_difference)

OUTPUT
Set difference between the two arrays:
[1 2 3]
7
Matplotlib is a popular Python library used for creating static, animated, and interactive
visualizations in a variety of formats. It provides a flexible platform for generating plots,
charts, and graphs, making it widely used in data analysis and scientific computing.

The two main interfaces used by Matplotlib are:

1. Pyplot Interface: This is a state-based interface similar to MATLAB, which provides a


simple way to create plots with a series of function calls. It allows users to generate figures
and customize them interactively.

2. Object-Oriented Interface: This approach gives more control over the figure and axes
objects, allowing for greater customization and flexibility. It is more suitable for complex plots
and when integrating Matplotlib with other libraries or frameworks.

Both interfaces can be used together, depending on the specific needs of the user.

8
Line Plot

A line plot is a graphical representation that connects individual data points with straight
lines. It is widely used in data visualization to depict trends over time or relationships
between variables. The main characteristics of line plots include:

1. Axes:

The x-axis typically represents the independent variable, such as time or categories.

The y-axis represents the dependent variable, showing how it changes with respect to the
x-axis.

2. Data Points:

Each point on the plot corresponds to a data value. Points are plotted based on their x and y
coordinates.

3. Trend Visualization:

Line plots are effective for visualizing trends, as the connected points highlight the
progression or change of data over intervals.
4. Multiple Lines:

Multiple datasets can be plotted on the same graph, allowing for easy comparison between
different groups or conditions.

5. Use Cases:

Commonly used in time series analysis, financial data, and any context where changes over
time need to be illustrated.

Scatter Plot

A scatter plot displays individual data points on a two-dimensional plane, showing the
relationship between two quantitative variables. It is particularly useful for identifying
patterns, correlations, and distributions in data. Key aspects of scatter plots include:

1. Axes:

The x-axis represents one variable, while the y-axis represents another variable.

2. Data Representation:

Each dot represents an observation, with its position determined by the values of the two
variables being plotted.

3. Correlation Identification:

Scatter plots can reveal the nature of the relationship between variables (positive, negative,
or no correlation) by examining the overall pattern of the points.

4. Outliers:

They are useful for spotting outliers—points that fall far away from the general cluster of
data.
5. Use Cases:

Commonly used in statistics, research, and various scientific fields to assess relationships,
such as between height and weight or temperature and sales.

Comparison

Purpose: Line plots emphasize trends over time, while scatter plots focus on relationships
between two variables.

Data Type: Line plots are typically used for continuous data, whereas scatter plots can
handle both continuous and discrete data.

Visualization: Line plots connect data points to illustrate a flow, while scatter plots display
points independently, emphasizing their distribution.

Conclusion

Both line plots and scatter plots are essential tools in data analysis and visualization.
Understanding their strengths and applications enables effective communication of insights
derived from data, aiding in decision-making across various fields.

9
import numpy as np

# Define the original array


original_array = np.array([1, 2, 3, 4, 5])

# Define the values to append


values_to_append = np.array([6, 7, 8, 9, 10])

# Append the values to the end of the array


appended_array = np.append(original_array, values_to_append)

# Display the appended array


print("Appended array:")
print(appended_array)

OUTPUT
Appended array:
[ 1 2 3 4 5 6 7 8 9 10]
10
import numpy as np

# Define the array


array = np.array([1, 2, 3, 4, 5])

# Convert the array to a floating type


floating_array = array.astype(float)

# Display the floating array


print("Floating array:")
print(floating_array)

Output
Floating array:
[1. 2. 3. 4. 5.]

11
import numpy as np

# Define the arrays


array1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
array2 = np.array([4, 5, 6, 7, 8, 9, 10, 11, 12])

# Find the common values between the two arrays


common_values = np.intersect1d(array1, array2)

# Display the common values


print("Common values between the two arrays:")
print(common_values)
Output
Common values between the two arrays:
[4 5 6 7 8 9]

12
Repeated of 5

131. Setting Up Matplotlib

First, ensure you have Matplotlib installed. If not, you can install it using pip:

pip install matplotlib

2. Adding Text to Plots

You can add simple text to your plots using the text() function. This function allows you to
specify the exact location for the text.
Example of Adding Text

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4]
y = [10, 15, 7, 10]

# Creating a line plot


plt.plot(x, y)
plt.title('Sample Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')

# Adding text to the plot


plt.text(2, 15, 'Highest Point', fontsize=12, color='red', ha='center')

plt.show()

Explanation:

Coordinates (2, 15): The position of the text on the plot.

fontsize and color: Customize the appearance of the text.

ha='center': Horizontal alignment of the text.

3. Annotating Points

Annotations provide context to specific data points, often with an arrow pointing to the point
of interest. This is done using the annotate() function.

Example of Annotating a Point

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4]
y = [10, 15, 7, 10]

# Creating a line plot


plt.plot(x, y)
plt.title('Sample Line Plot with Annotations')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')

# Annotating the peak point


plt.scatter(2, 15, color='red') # Highlight the point
plt.annotate('Peak Point', xy=(2, 15), xytext=(3, 17),
arrowprops=dict(arrowstyle='->', color='blue'))

plt.show()

Explanation:

xy: The coordinates of the point being annotated.

xytext: The location where the annotation text will appear.

arrowprops: Customize the arrow's appearance.

4. Customizing Text and Annotations

You can further customize text and annotations using various parameters.

Example with Customization

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4]
y = [10, 15, 7, 10]

plt.plot(x, y)
plt.title('Customized Text and Annotations')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')

# Adding customized text


plt.text(3, 8, 'Text with Background', fontsize=14, color='black',
bbox=dict(facecolor='yellow', alpha=0.5))

# Annotating with a box and arrow


plt.annotate('Annotated Point', xy=(1, 10), xytext=(2, 12),
arrowprops=dict(facecolor='blue', shrink=0.05),
bbox=dict(boxstyle='round,pad=0.3', edgecolor='blue', facecolor='lightblue'))

plt.show()

Explanation:

bbox: Adds a background box to the text with specified properties like facecolor and alpha
(transparency).
arrowprops: Further customize the arrow's color and style.

5. Using Different Fonts and Styles

You can also change the font properties and styles of the text and annotations.

Example with Font Customization

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4]
y = [10, 15, 7, 10]

plt.plot(x, y)
plt.title('Font Customization in Annotations')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')

# Adding text with custom font properties


plt.text(3, 8, 'Custom Font Text', fontsize=14, fontweight='bold',
fontfamily='serif', color='green')

# Annotating with custom font properties


plt.annotate('Annotated Point', xy=(1, 10), xytext=(2, 12),
fontsize=12, fontstyle='italic',
arrowprops=dict(arrowstyle='->', color='orange'))

plt.show()

14
Importing Required Libraries

To create 3D plots, you need to import the necessary modules:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

3. Creating a 3D Scatter Plot

A 3D scatter plot visualizes points in three-dimensional space based on their x, y, and z


coordinates.

Example Code for 3D Scatter Plot

# Sample data for 3D scatter plot


x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Creating a scatter plot


ax.scatter(x, y, z, c='r', marker='o')

ax.set_title('3D Scatter Plot')


ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')

plt.show()

Explanation:

np.random.rand(100): Generates 100 random numbers between 0 and 1 for x, y, and z


coordinates.

scatter(): Creates a scatter plot with specified color and marker style.

4. Creating a 3D Line Plot

A 3D line plot connects points in 3D space with lines.

Example Code for 3D Line Plot

# Sample data for 3D line plot


z = np.linspace(0, 1, 100)
x = z * np.sin(25 * z)
y = z * np.cos(25 * z)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Creating a line plot


ax.plot(x, y, z, label='3D Line', color='b')

ax.set_title('3D Line Plot')


ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')
ax.legend()
plt.show()

Explanation:

np.linspace(0, 1, 100): Generates 100 evenly spaced values between 0 and 1 for z.

plot(): Creates a 3D line connecting the specified x, y, and z points.

5. Creating a 3D Surface Plot

A surface plot represents a three-dimensional surface, defined by a grid of points.

Example Code for 3D Surface Plot

# Sample data for 3D surface plot


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')

# Creating a surface plot


ax.plot_surface(x, y, z, cmap='viridis')

ax.set_title('3D Surface Plot')


ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')

plt.show()

Explanation:

np.meshgrid(): Creates a rectangular grid out of two given one-dimensional arrays (x and y).

plot_surface(): Generates a 3D surface plot and applies a color map.

6. Creating a 3D Wireframe Plot

A wireframe plot is a 3D representation of the surface using lines instead of solid colors.

Example Code for 3D Wireframe Plot

# Sample data for 3D wireframe plot


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')

# Creating a wireframe plot


ax.plot_wireframe(x, y, z, color='black')

ax.set_title('3D Wireframe Plot')


ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')

plt.show()

Explanation:

plot_wireframe(): Creates a wireframe representation of the surface defined by the x, y, and


z coordinates.

15
Repaeted of 1

16
Repeated Of 10

17
import numpy as np

# Define the array


array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])

# Reverse the array


reversed_array = array[::-1]

# Display the reversed array


print("Reversed array:")
print(reversed_array)

Output
Reversed array:
[9 8 7 6 5 4 3 2 1]

18
Repeated of 12 and 5
19
Repaeted Of 6

20
1. Understanding Geographic Data

Geographic data refers to information that is associated with specific locations on the Earth.
This data can include:

Points: Specific locations (e.g., cities, landmarks).

Lines: Features such as roads, rivers, or borders.

Polygons: Areas defined by boundaries (e.g., countries, states).

Geographic data is often represented in coordinate systems, commonly using latitude and
longitude for positioning.

2. Base Maps

A base map provides context for the geographic data being displayed. It can be a simple
outline of geographical features, or it can include detailed representations of terrain, roads,
and labels.

Popular Libraries for Base Maps

Matplotlib with Basemap: Provides simple visualizations with customizable base maps.

GeoPandas: Allows for easy manipulation of geospatial data and plotting.

Folium: Built on Leaflet.js, useful for interactive maps.

3. Using GeoPandas for Geographic Data

GeoPandas extends the capabilities of Pandas to allow for spatial operations on geometric
types. Here’s how to use it to visualize geographic data with a base map.

Installation

Ensure you have GeoPandas and other necessary libraries installed:

pip install geopandas matplotlib

Example: Visualizing World Countries with Base Map


1. Import Libraries

import geopandas as gpd


import matplotlib.pyplot as plt

2. Load Geographic Data

You can load geographic data from various sources. For example, GeoPandas comes with
built-in datasets like the world countries shapefile.

# Load world shapefile


world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))

3. Visualizing the Base Map

# Plotting the world map


world.plot()
plt.title('World Countries')
plt.show()

4. Adding Data to the Base Map

You can overlay additional data on the base map. For instance, let’s visualize country
populations.

Example: Adding Population Data

1. Add Population Data

Make sure the data is available in a suitable format, such as a CSV file. You can merge this
data with the geographic dataset based on a common key (like country names).

# Sample data - Replace with actual population data


population_data = {
'name': ['United States', 'Canada', 'Mexico', 'Brazil'],
'population': [331002651, 37742154, 128932753, 212559417]
}

population_df = pd.DataFrame(population_data)
2. Merge DataFrames

# Merging geographic data with population data


world = world.merge(population_df, how='left', left_on='name', right_on='name')

3. Visualizing with Color Based on Population

You can use a color map to represent the population sizes.

# Plotting the map with population data


fig, ax = plt.subplots(1, 1, figsize=(15, 10))
world.boundary.plot(ax=ax, linewidth=1)
world.plot(column='population', ax=ax, legend=True,
legend_kwds={'label': "Population by Country",
'orientation': "horizontal"},
cmap='OrRd')

plt.title('World Population Distribution')


plt.show()

5. Customizing Base Maps

You can customize the base map using different styles, layers, and additional features.

Adding Background Layers

Using other libraries such as contextily, you can add tiled web maps (like OpenStreetMap)
for more context.

pip install contextily

import contextily as ctx

# Reproject to Web Mercator


world = world.to_crs(epsg=3857)

# Plotting with a base map


fig, ax = plt.subplots(figsize=(15, 10))
world.boundary.plot(ax=ax, linewidth=1)
world.plot(column='population', ax=ax, legend=True,
cmap='OrRd')

# Add a basemap
ctx.add_basemap(ax, crs=world.crs.to_string())
plt.title('World Population with Base Map')
plt.show()

21
1. Line Plot

A line plot displays data points connected by lines, making it ideal for showing trends over
time.

Example Code for Line Plot

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

# Creating the line plot


plt.plot(x, y, marker='o', linestyle='-', color='b')
plt.title('Line Plot Example')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.grid()
plt.show()

2. Scatter Plot

A scatter plot visualizes the relationship between two numerical variables using points.

Example Code for Scatter Plot

import matplotlib.pyplot as plt


import numpy as np

# Sample data
x = np.random.rand(50)
y = np.random.rand(50)

# Creating the scatter plot


plt.scatter(x, y, color='r', alpha=0.5)
plt.title('Scatter Plot Example')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.grid()
plt.show()

3. Bar Chart
Bar charts are used to compare quantities across different categories.

Example Code for Bar Chart

import matplotlib.pyplot as plt

# Sample data
categories = ['A', 'B', 'C', 'D']
values = [4, 7, 1, 8]

# Creating the bar chart


plt.bar(categories, values, color='c')
plt.title('Bar Chart Example')
plt.xlabel('Categories')
plt.ylabel('Values')
plt.grid(axis='y')
plt.show()

4. Histogram

Histograms show the distribution of a dataset by grouping data into bins.

Example Code for Histogram

import matplotlib.pyplot as plt


import numpy as np

# Sample data
data = np.random.randn(1000)

# Creating the histogram


plt.hist(data, bins=30, color='m', alpha=0.7)
plt.title('Histogram Example')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.grid()
plt.show()

5. Box Plot

Box plots summarize the distribution of a dataset based on five statistics: minimum, first
quartile, median, third quartile, and maximum.

Example Code for Box Plot

import matplotlib.pyplot as plt


import numpy as np
# Sample data
data = [np.random.normal(0, std, 100) for std in range(1, 4)]

# Creating the box plot


plt.boxplot(data, vert=True, patch_artist=True)
plt.title('Box Plot Example')
plt.xlabel('Dataset')
plt.ylabel('Value')
plt.xticks([1, 2, 3], ['Dataset 1', 'Dataset 2', 'Dataset 3'])
plt.grid(axis='y')
plt.show()

6. 3D Plot

Matplotlib can also handle three-dimensional data visualization, useful for complex datasets.

Example Code for 3D Scatter Plot

import matplotlib.pyplot as plt


from mpl_toolkits.mplot3d import Axes3D
import numpy as np

# Sample data
x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)

# Creating a 3D scatter plot


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, color='g')

ax.set_title('3D Scatter Plot Example')


ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')
plt.show()

You might also like