Ma commited on
Commit
ba21260
·
verified ·
1 Parent(s): 2ac74c9

Upload durham_trees_analysis.py

Browse files
Files changed (1) hide show
  1. durham_trees_analysis.py +72 -0
durham_trees_analysis.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
12
+ # Install required packages
13
+ import subprocess
14
+
15
+ # Install datasets package
16
+ subprocess.run(["pip", "install", "datasets", "geopandas", "seaborn", "matplotlib", "mplcursors", "pandas"])
17
+
18
+ # Import libraries
19
+ from datasets import load_dataset
20
+ import seaborn as sns
21
+ import matplotlib.pyplot as plt
22
+ import mplcursors
23
+ import pandas as pd
24
+ import geopandas as gpd
25
+
26
+ # Load dataset
27
+ dataset = load_dataset("Ziyuan111/DurhamTrees")
28
+
29
+ # Convert dataset to pandas DataFrame
30
+ df = pd.DataFrame(dataset['train'])
31
+
32
+ # Interactive scatter plot with seaborn and mplcursors
33
+ def plot_interactive_scatter():
34
+ scatter = sns.scatterplot(data=df, x='X', y='Y', hue='species')
35
+ plt.xlabel('X')
36
+ plt.ylabel('Y')
37
+ plt.legend([],[], frameon=False)
38
+ cursor = mplcursors.cursor(hover=True)
39
+
40
+ @cursor.connect("add")
41
+ def on_add(sel):
42
+ sel.annotation.set_text(df.iloc[sel.target.index]['species'])
43
+ plt.show()
44
+ plt.savefig('interactive_scatter.png')
45
+ display(Image('interactive_scatter.png'))
46
+
47
+ # Plot tree planting sites with geopandas
48
+ def plot_tree_sites():
49
+ gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.X, df.Y))
50
+ durham_center = {'x': -78.898619, 'y': 35.994033} # Durham, NC coordinates
51
+ fig, ax = plt.subplots(figsize=(10, 10))
52
+ gdf.plot(ax=ax, color='green')
53
+ buffer = 0.05
54
+ ax.set_xlim([durham_center['x'] - buffer, durham_center['x'] + buffer])
55
+ ax.set_ylim([durham_center['y'] - buffer, durham_center['y'] + buffer])
56
+ ax.set_title('Tree Planting Sites in Durham')
57
+ ax.set_xlabel('Longitude')
58
+ ax.set_ylabel('Latitude')
59
+ ax.set_axis_off()
60
+ plt.show()
61
+ plt.savefig('tree_sites.png')
62
+ display(Image('tree_sites.png'))
63
+
64
+ # Print correlation matrix
65
+ def print_correlation_matrix():
66
+ correlation_matrix = df[['diameterin', 'carbonstorage_lb']].corr()
67
+ print(correlation_matrix)
68
+
69
+ # Call the functions
70
+ plot_interactive_scatter()
71
+ plot_tree_sites()
72
+ print_correlation_matrix()