(Oct-01-2021, 08:24 PM)deanhystad Wrote: [ -> ]You can plot a single point at a time, but plotting all the points at once is sooooooo much faster. When you plot a bar at a time matplotlib has to create new axis and new labels and layout the plot area and who knows what else. When you plot all points at once, all the work of laying out the plot is performed once.
When writing Python your goal should be to write as little code as possible. If you find yourself using a for loop you should consider that you might be doing something wrong. If you have multiple if statements it is likely you are doing something wrong. Your last example should not have a for loop or any if statements. You should have pandas selecting the ranges for the different plots, either by selection subsets of the dataframe or subsets of the series (the latter is going to be faster).
That's exactly what I'm aiming to do... just don't fully understand it yet. I continue to work on it (including reading links like snippsat provided, which aren't totally sinking in yet but getting better).
Having said all this, then, here are two versions that both work. The first was adapted from my initial attempt and is clearly not where I want to be (for loop, multiple if statements). The second is adapted from your solution (I commented out the sort since output was same without it). Both take roughly 0.75 seconds on my computer. Any thoughts on why yours is not much faster?
%%timeit
import matplotlib.pyplot as plt
import pandas as pd
fig=plt.figure()
fig, ax = plt.subplots(2,2)
df = pd.read_csv("C:/Users/Mark/Desktop/SPX_2021_copy.csv")
my_dict = dict(df['DTE'].value_counts())
dict_250 = {}
dict_501 = {}
dict_751 = {}
dict_2000 = {}
for key in my_dict:
if key < 251:
dict_250[key]=my_dict[key]
elif (key > 250 and key < 501):
dict_501[key]=my_dict[key]
elif (key > 500 and key < 751):
dict_751[key]=my_dict[key]
elif key > 750:
dict_2000[key]=my_dict[key]
ax[0,0].bar(dict_250.keys(),dict_250.values())
ax[0,1].bar(dict_501.keys(),dict_501.values())
ax[1,0].bar(dict_751.keys(),dict_751.values())
ax[1,1].bar(dict_2000.keys(),dict_2000.values())
%%timeit
import matplotlib.pyplot as plt
import pandas as pd
def plot_range(plot, min_, max_, data):
'''Essentially plot.bar(data[min_:max_])'''
data = data[(data.index >= min_) & (data.index <= max_)]
plot.bar(data.index, data.values)
df = pd.read_csv("C:/Users/Mark/Desktop/SPX_2021_copy.csv")
counts = df['DTE'].value_counts()#.sort_index(ascending=True)
fig, ax = plt.subplots(2,2)
plot_range(ax[0, 0], 0, 250, counts)
plot_range(ax[0, 1], 251, 500, counts)
plot_range(ax[1, 0], 501, 750, counts)
plot_range(ax[1, 1], 751, 10000, counts)
#plt.show()