Update README.md
Browse files
README.md
CHANGED
@@ -25,3 +25,63 @@ plt.tight_layout()
|
|
25 |
|
26 |
# Show the plot
|
27 |
plt.show()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
# Show the plot
|
27 |
plt.show()
|
28 |
+
|
29 |
+
# Python3 Examples for Bar/Pie Chart
|
30 |
+
### ๐ Python Code to Create a Pie Chart Based on Year Distribution
|
31 |
+
|
32 |
+
``python
|
33 |
+
|
34 |
+
from datasets import load_dataset
|
35 |
+
import matplotlib.pyplot as plt
|
36 |
+
|
37 |
+
# Load the dataset
|
38 |
+
ds = load_dataset("ajsbsd/hj")
|
39 |
+
|
40 |
+
# Assuming the dataset is in the 'train' split and contains a 'Year' column
|
41 |
+
year_counts = {}
|
42 |
+
|
43 |
+
# Count occurrences per year
|
44 |
+
for entry in ds['train']:
|
45 |
+
year = entry.get('Year', None)
|
46 |
+
if year is not None:
|
47 |
+
year_counts[year] = year_counts.get(year, 0) + 1
|
48 |
+
|
49 |
+
# Sort by year for consistent ordering
|
50 |
+
sorted_years = sorted(year_counts.items())
|
51 |
+
|
52 |
+
years, counts = zip(*sorted_years)
|
53 |
+
|
54 |
+
# Plot pie chart
|
55 |
+
plt.figure(figsize=(8, 8))
|
56 |
+
plt.pie(counts, labels=years, autopct='%1.1f%%', startangle=140)
|
57 |
+
plt.title('Distribution of Incidents by Year')
|
58 |
+
plt.axis('equal') # Equal aspect ratio ensures the pie is circular
|
59 |
+
plt.show()
|
60 |
+
|
61 |
+
### ๐ Python Code to Create a Bar Graph of Homicides Per Year
|
62 |
+
|
63 |
+
import matplotlib.pyplot as plt
|
64 |
+
import pandas as pd
|
65 |
+
|
66 |
+
# Data from your CSV
|
67 |
+
data = {
|
68 |
+
'Year': [2024, 2023, 2022, 2021, 2020, 2019, 2018, 2017, 2016, 2015],
|
69 |
+
'Homicides': [219, 257, 261, 276, 254, 211, 211, 257, 262, 174]
|
70 |
+
}
|
71 |
+
|
72 |
+
# Convert to DataFrame
|
73 |
+
df = pd.DataFrame(data)
|
74 |
+
|
75 |
+
# Plot bar graph
|
76 |
+
plt.figure(figsize=(10, 6))
|
77 |
+
plt.bar(df['Year'].astype(str), df['Homicides'], color='skyblue')
|
78 |
+
plt.title('Homicides Per Year')
|
79 |
+
plt.xlabel('Year')
|
80 |
+
plt.ylabel('Number of Homicides')
|
81 |
+
plt.xticks(rotation=45)
|
82 |
+
plt.tight_layout()
|
83 |
+
|
84 |
+
# Show the plot
|
85 |
+
plt.show()
|
86 |
+
|
87 |
+
```
|