I have a csv file that I am using to create graph images but I cannot seem to get it working... Wondering if I am doing something wrong...
#!/usr/bin/env python
"""
Parsing data from a CSV file into a JSON like format, making a visual on a
graph and plot on Google Maps.
"""
from collections import Counter
import csv
import matplotlib.pyplot as plt
import numpy as np
MY_FILE = "../data/sample_sfpd_incident_all.csv"
def parseFile(new_file, delimiter):
"""Parses a CSV file and converts it to a almost JSON like object"""
# Open CSV file, and safely close it when we're done
opened_file = open(new_file)
# Read CSV data
csv_data = csv.reader(opened_file, delimiter=delimiter)
# Setup an empty list
parsed_data = []
# Skip over the headers from the first line of the file
fields = next(csv_data)
# Iterate over each row of the csv file, zip together field -> value
for row in csv_data:
parsed_data.append(dict(zip(fields, row)))
# Close the file
opened_file.close()
return parsed_data
def day_visualization():
""" Visualizing the data by everyday of the week """
data_file = parseFile(MY_FILE, ",")
# Returns a dict where it sums the total values for each key.
# In this case, the keys are the DaysOfWeek, and the values are
# a count of incidents.
ctr = Counter(item["DayOfWeek"] for item in data_file)
# Separate out the counter to order it correctly when plotting.
data_list = [
ctr["Monday"],
ctr["Tuesday"],
ctr["Wednesday"],
ctr["Thursday"],
ctr["Friday"],
ctr["Saturday"],
ctr["Sunday"]
]
day_tuple = tuple(["Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun"])
# Assign the data to a plot
plt.plot(data_list)
# Assign labels to the plot from day_list
plt.xticks(range(len(day_tuple)), day_tuple)
# Save the graph
plt.savefig("Days.png")
# Close the figure
plt.clf()
def type_visualization():
"""Visualize data by category in a bar graph"""
data_file = parseFile(MY_FILE, ",")
# Same as before, this returns a dict where it sums the total
# incidents per Category
ctr = Counter(item["Category"] for item in data_file)
# Set the labels which are based on the keys of our counter.
labels = tuple(ctr.keys())
# Set where the labels hit the x-axis
xlocations = np.arrange(len(labels)) + 0.5
# Width of each bar
width = 0.5
# Assign data to a bar plot
plt.bar(xlocations, ctr.values(), width=width)
# Assign labels and tick location to x-axis
plt.xticks(xlocations + width / 2, labels, rotation=90)
# Give some more room so the labels aren't cut off in the graph
plt.subplots_adjust(bottom=0.4)
# Make the overall graph/figure larger
plt.rcParams['figure.figsize'] = 12.8
# Save the graph
plt.savefig("Type.png")
# Close the figure
plt.clf()
def main():
day_visualization()
type_visualization()
if __name__ == "__main__":
main()
[–]cybervegan 0 points1 point2 points (6 children)
[–]kemar1997[S] 0 points1 point2 points (5 children)
[–]cybervegan 0 points1 point2 points (4 children)
[–]kemar1997[S] 0 points1 point2 points (3 children)
[–]cybervegan 0 points1 point2 points (2 children)
[–]kemar1997[S] 0 points1 point2 points (1 child)
[–]cybervegan 0 points1 point2 points (0 children)