0% found this document useful (0 votes)
7 views4 pages

DV Exp-1.2

The document contains a Python function that visualizes data from a CSV file using various plots such as line charts, scatter plots, heatmaps, and 3D plots. It handles errors related to file access and data validity, providing informative messages for empty files or insufficient numeric columns. The function also includes a brief analysis of the usefulness of each type of visualization created.

Uploaded by

a64394127
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)
7 views4 pages

DV Exp-1.2

The document contains a Python function that visualizes data from a CSV file using various plots such as line charts, scatter plots, heatmaps, and 3D plots. It handles errors related to file access and data validity, providing informative messages for empty files or insufficient numeric columns. The function also includes a brief analysis of the usefulness of each type of visualization created.

Uploaded by

a64394127
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/ 4

import matplotlib.

pyplot as plt
import seaborn as sns
import plotly.express as px
import pandas as pd
import numpy as np

def visualize_data(file_path):
try:
df = pd.read_csv(file_path)

if df.empty:
print("Error: The provided CSV file is empty.")
return

def create_line_chart(df):
plt.figure(figsize=(8, 6))
for column in df.select_dtypes(include=np.number).columns:
plt.plot(df[column], label=column)
plt.xlabel('Index')
plt.ylabel('Value')
plt.title('Line Chart')
plt.legend()
plt.show()

def create_scatter_plot(df):
plt.figure(figsize=(8, 6))
numeric_cols = df.select_dtypes(include=np.number).columns
if len(numeric_cols) >= 2 :
sns.scatterplot(x=numeric_cols[0], y=numeric_cols[1], data=df, hue=numeric_cols[2] if
len(numeric_cols) >=3 else None)
else:
print("Not enough numeric columns for scatter plot")
plt.title('Scatter Plot')
plt.show()

def create_heatmap(df):
plt.figure(figsize=(8, 6))
numeric_df = df.select_dtypes(include=np.number)
sns.heatmap(numeric_df.corr(), annot=True, cmap='coolwarm')
plt.title('Heatmap of Correlation')
plt.show()

def create_3d_plot(df):
numeric_cols = df.select_dtypes(include=np.number).columns
if len(numeric_cols) >= 3:
fig = px.scatter_3d(df, x=numeric_cols[0], y=numeric_cols[1], z=numeric_cols[2],
color=numeric_cols[2])
fig.show()
else:
print("Not enough numeric columns for 3D plot")

create_line_chart(df)
create_scatter_plot(df)
create_heatmap(df)
create_3d_plot(df)
print("Analysis:")
print("1. Line Chart: Useful for observing trends over time or index.")
print("2. Scatter Plot: Effective for identifying relationships between two variables.")
print("3. Heatmap: Good for visualizing the correlation matrix.")
print("4. 3D Plot: Suitable for exploring datasets with three or more dimensions.")

except FileNotFoundError:
print(f"Error: File not found at '{file_path}'.")
except pd.errors.EmptyDataError:
print(f"Error: The file '{file_path}' is empty.")
except Exception as e:
print(f"An unexpected error occurred: {e}")

visualize_data('/content/weatherHistory.csv')

You might also like