DV Exp-1.2
DV Exp-1.2
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')