Issue
I have two lists with corresponding values, for example: [A, B, C] and [d, e, f] where A, B, and C are datetime objects and d, e, and f are float objects and "d" is a value that occurs on "A". I have another list of datetime objects that I can bin into A, B, and C ranges, but I want to bin them to the corresponding d, e, and f values instead.
My code right now looks like this:
import csv
import matplotlib.pyplot as plt
import datetime
list1 = []
zip1 = []
zip2 = []
#opening files and setting lists as csv inputs
with open("whistles.csv") as whistlecsv:
whistlelist = csv.reader(whistlecsv, delimiter = ',')
for row in whistlelist:
list1.append(row[0])
with open("f10.7.csv") as fcsv:
flist = csv.reader(fcsv, delimiter = ',')
for row in flist:
zip2.append(float(row[1]))
zip1.append(row[0])
#converting strings to datetime objects
for i in range(0, len(list1)):
list1[i] = datetime.datetime.strptime(list1[i], "%Y-%m-%d %H:%M:%S")
for i in range(0, len(zip1)):
zip1[i] = datetime.datetime.strptime(zip1[i], "%Y%m%d %H%M%S")
#binning list1 into zip2 using corresponding zip1
for i in range(0, len(list1)):
for j in range(0, len(zip1)):
if int(datetime.datetime.strftime(zip1[j-1], "%Y%m%d%H%M%S")) < int(datetime.datetime.strftime(list1[i], "%Y%m%d%H%M%S")) < int(datetime.datetime.strftime(zip1[j], "%Y%m%d%H%M%S")):
zip2[j-1].append(list1[i])
plt.hist(list1, bins = zip2)
plt.show()
This returns "AttributeError: 'float' object has no attribute 'append'" I want to count the number of times elements of list1 appear within a set range of values specified by elements in zip1 and graph a frequency histogram mapping this count onto the corresponding values of zip2. How do I do that? This concerns the last paragraph of code, mainly.
I've tried zipping the lists zip1 and zip2, setting a specific float value for every date, then using a plt.hist() function to bin the list1 data by zip2:
matched = zip(zip1, zip2)
*zip1, *zip2 = zip(*matched)
plt.hist(list1, bins = *zip2)
plt.show()
I know this basically just zips and unzips a file, but I'm running out of ideas. I'm trying to show that zip1 and zip2 are corresponding values, so I should essentially be able to bin list1 by either list to generate a frequency histogram, but that's not the case.
The plt.hist() function returns an error saying that no simple shape can be generated when I try to bin the list1 data by the zip2 values, even after zipping the zip1 and zip2 files.
Please help! Thanks in advance.
Solution
If I have understood the problem, you can use numpy.histogram
on both of your datetime lists to calculate the number in each bin, and then use stairs
to plot those against your float values.
import datetime
import numpy as np
import matplotlib.pyplot as plt
dates = [datetime.datetime(2024, 1, d) for d in range(1, 32)]
date_bins = [datetime.datetime(2024, 1, 1), datetime.datetime(2024, 1, 12),
datetime.datetime(2024, 2, 1)]
float_bins = [3, 27, 42]
counts, _ = np.histogram(dates, bins=date_bins)
fig, ax = plt.subplots()
ax.stairs(counts, float_bins, fill=True)
ax.set_xticks(float_bins)
plt.show()
Edit: with a little extra work, bar
could be used instead of stairs
. This will work in older Matplotlib versions, though I would recommend updating to a newer version.
float_bin_widths = [high - low for high, low in zip(float_bins[1:], float_bins[:-1])]
fig, ax = plt.subplots()
ax.bar(float_bins[:-1], counts, width=float_bin_widths, align='edge')
ax.set_xticks(float_bins)
plt.show()
Answered By - RuthC
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.