Datasets:
Upload durham_trees_analysis.py
Browse files- durham_trees_analysis.py +78 -0
durham_trees_analysis.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""durham_trees_analysis
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1OjlRC7F_UICGJM59jzSoy1o2crZxqXCl
|
8 |
+
"""
|
9 |
+
|
10 |
+
# Save this code as a file named 'durham_trees_analysis.py'
|
11 |
+
# Import display function and Image class
|
12 |
+
from IPython.display import display, Image
|
13 |
+
# Install required packages
|
14 |
+
import subprocess
|
15 |
+
import seaborn as sns
|
16 |
+
import matplotlib.pyplot as plt
|
17 |
+
# Install datasets package
|
18 |
+
subprocess.run(["pip", "install", "datasets", "geopandas", "seaborn", "matplotlib", "mplcursors", "pandas"])
|
19 |
+
|
20 |
+
# Import libraries
|
21 |
+
from datasets import load_dataset
|
22 |
+
import seaborn as sns
|
23 |
+
import matplotlib.pyplot as plt
|
24 |
+
import mplcursors
|
25 |
+
import pandas as pd
|
26 |
+
import geopandas as gpd
|
27 |
+
|
28 |
+
# Load dataset
|
29 |
+
dataset = load_dataset("Ziyuan111/DurhamTrees")
|
30 |
+
|
31 |
+
# Convert dataset to pandas DataFrame
|
32 |
+
df = pd.DataFrame(dataset['train'])
|
33 |
+
|
34 |
+
# Interactive scatter plot with seaborn and mplcursors
|
35 |
+
def plot_interactive_scatter():
|
36 |
+
scatter = sns.scatterplot(data=df, x='X', y='Y', hue='species')
|
37 |
+
plt.xlabel('X')
|
38 |
+
plt.ylabel('Y')
|
39 |
+
plt.legend([],[], frameon=False)
|
40 |
+
cursor = mplcursors.cursor(hover=True)
|
41 |
+
|
42 |
+
@cursor.connect("add")
|
43 |
+
def on_add(sel):
|
44 |
+
sel.annotation.set_text(df.iloc[sel.target.index]['species'])
|
45 |
+
plt.show()
|
46 |
+
plt.savefig('interactive_scatter.png')
|
47 |
+
display(Image('interactive_scatter.png'))
|
48 |
+
|
49 |
+
# Plot tree planting sites with geopandas
|
50 |
+
def plot_tree_sites():
|
51 |
+
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.X, df.Y))
|
52 |
+
durham_center = {'x': -78.898619, 'y': 35.994033} # Durham, NC coordinates
|
53 |
+
fig, ax = plt.subplots(figsize=(10, 10))
|
54 |
+
gdf.plot(ax=ax, color='green')
|
55 |
+
buffer = 0.05
|
56 |
+
ax.set_xlim([durham_center['x'] - buffer, durham_center['x'] + buffer])
|
57 |
+
ax.set_ylim([durham_center['y'] - buffer, durham_center['y'] + buffer])
|
58 |
+
ax.set_title('Tree Planting Sites in Durham')
|
59 |
+
ax.set_xlabel('Longitude')
|
60 |
+
ax.set_ylabel('Latitude')
|
61 |
+
ax.set_axis_off()
|
62 |
+
plt.show()
|
63 |
+
plt.savefig('tree_sites.png')
|
64 |
+
display(Image('tree_sites.png'))
|
65 |
+
|
66 |
+
# Print correlation matrix
|
67 |
+
def print_correlation_matrix():
|
68 |
+
correlation_matrix = df[['vocs', 'coremoved_ozperyr', 'coremoved_dolperyr']].corr()
|
69 |
+
print(correlation_matrix)
|
70 |
+
|
71 |
+
# Call the functions
|
72 |
+
plot_interactive_scatter()
|
73 |
+
plot_tree_sites()
|
74 |
+
print_correlation_matrix()
|
75 |
+
|
76 |
+
|
77 |
+
# Show the plot
|
78 |
+
plt.show()
|