Data Science Lab Part 2
Data Science Lab Part 2
Aim:
To Visualizing Geographic Data with Basemap
One common type of visualization in data science is that of geographic data. Matplotlib's
main tool for this type of visualization is the Basemap toolkit, which is one of several Matplotlib
toolkits which lives under the mpl_toolkits namespace. Admittedly, Basemap feels a bit clunky
to use, and often even simple visualizations take much longer to render than you might hope.
More modern solutions such as leaflet or the Google Maps API may be a better choice for more
intensive map visualizations. Still, Basemap is a useful tool for Python users to have in their
virtual toolbelts. In this section, we'll show several examples of the type of map visualization
that is possible with this toolkit.
Installation of Basemap is straightforward; if you're using conda you can type this and the
package will be downloaded:
Code:
m = Basemap(projection='lcc',
resolution=None,
width=8E6, height=8E6,
lat_0=45, lon_0=-100,)
m.etopo(scale=0.5, alpha=0.5)
m(-122.3, 47.6)
plt.plot(x, y, 'ok',markersize=5)
fontsize=12);
from mpl_toolkits.basemap
import Basemap
import matplotlib.pyplot as
pltfig = plt.figure(figsize =
(12,12)) m =
Basemap()
m.drawcoastlines()
m.drawcoastlines(linewidth=1.0,
linestyle='dashed', color='red')
plt.title("Coastlines", fontsize=20)
plt.show()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
AIM
To convert a Python list to an array of machine values (also known as a "byte array" or "packed
binary data") and vice versa, you can use the structmodule. This module helps in packing and
unpacking Python values into C structs, which are represented as Python bytes objects.
Algorithm
:
Step-by-Step Algorithm
Explanation:
Python Program to Convert List to Array of Machine Values and Vice Versa
import struct
def list_to_machine_values(input_list):
# 'f' format character indicates that we're using 4-byte float values
# Adjust the format according to the data type in your list ('i' for
int,
'd' for double, etc.)
format_string = f'{len(input_list)}f'
def machine_values_to_list(machine_values):
# Calculate the number of floats in the byte array
number_of_floats = len(machine_values) // struct.calcsize('f')
# Example usage
input_list = [1.5, 2.5, 3.5, 4.5]
print("Original list:", input_list)
Sample Output
When you run this code, you should get an output similar to:
1. Original List: The original Python list of floats [1.5, 2.5, 3.5, 4.5].
2. Machine Values (Byte Array): The packed byte array (a sequence of
bytes) that represents the list of floats in memory.
3. Converted Back to List: The unpacked list after converting back from the byte
array, matching the original input.
RESULT:
Thus the progam to convert a Python list to an array of machine values computation for
was successfully completed.
PYTHON PROGRAM TO DISPLAY MULTIPLE TYPES OF
CHATS USING MATPOTLIB PACKAGE
AIM:
ALGORITHM:
Below is a Python program that demonstrates how to create a line chart, bar chart, scatter
plot, and histogram using matplotlib.
# Example data
x = np.arange(1, 11)
y = np.random.randint(1, 20, size=10)
z = np.random.randint(1, 100, size=10)
# Line Chart
axs[0, 0].plot(x, y, marker='o', linestyle='-', color='b', label='Line
Chart')
axs[0, 0].set_title('Line Chart')
axs[0, 0].set_xlabel('X-axis')
axs[0, 0].set_ylabel('Y-axis')
axs[0, 0].legend()
# Bar Chart
axs[0, 1].bar(x, y, color='g', label='Bar Chart')
axs[0, 1].set_title('Bar Chart')
axs[0, 1].set_xlabel('Categories')
axs[0, 1].set_ylabel('Values')
axs[0, 1].legend()
# Scatter Plot
axs[1, 0].scatter(x, z, color='r', label='Scatter Plot')
axs[1, 0].set_title('Scatter Plot')
axs[1, 0].set_xlabel('X-axis')
axs[1, 0].set_ylabel('Random Values')
axs[1, 0].legend()
# Histogram
axs[1, 1].hist(z, bins=5, color='m', edgecolor='black', label='Histogram')
axs[1, 1].set_title('Histogram')
axs[1, 1].set_xlabel('Bins') axs[1,
1].set_ylabel('Frequency') axs[1, 1].legend()
Explanation
1. Importing Libraries:
o matplotlib.pyplot is imported for plotting various types of charts.
o numpyis used to generate example data.
2. Creating Data:
o xrepresents the X-axis values from 1 to 10.
o yis an array of random integers (for the line chart and bar chart).
o zis another array of random integers (for the scatter plot and histogram).
3. Creating Subplots:
o plt.subplots(2, 2, figsize=(10, 8)) creates a 2x2 grid of subplots with a specific figure
size.
4. Plotting Different Charts:
o Line Chart: plot()function is used with markers, linestyle, and color.
o Bar Chart: bar()function creates a bar chart.
o Scatter Plot: scatter() function creates a scatter plot.
o Histogram: hist()function creates a histogram with specified bins.
5. Customization and Display:
o Titles, labels, and legends are added to each subplot for clarity.
o plt.tight_layout() adjusts the subplots to prevent overlap.
o plt.show() displays the created plots.
Output
When you run the program, it will display a window with four different types of charts arranged in a 2x2 grid:
RESULT:
Thus the progam to convert a Python list to an array of machine values computation for was successfully
completed