Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Where's the endless loop?
#9
(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()
Reply


Messages In This Thread
Where's the endless loop? - by Mark17 - Oct-01-2021, 02:08 PM
RE: Where's the endless loop? - by Mark17 - Oct-01-2021, 02:41 PM
RE: Where's the endless loop? - by Mark17 - Oct-01-2021, 03:02 PM
RE: Where's the endless loop? - by Mark17 - Oct-01-2021, 03:25 PM
RE: Where's the endless loop? - by deanhystad - Oct-01-2021, 03:36 PM
RE: Where's the endless loop? - by snippsat - Oct-01-2021, 04:51 PM
RE: Where's the endless loop? - by Mark17 - Oct-01-2021, 06:22 PM
RE: Where's the endless loop? - by deanhystad - Oct-01-2021, 08:24 PM
RE: Where's the endless loop? - by Mark17 - Oct-04-2021, 04:30 PM
RE: Where's the endless loop? - by deanhystad - Oct-04-2021, 04:37 PM
RE: Where's the endless loop? - by Mark17 - Oct-04-2021, 04:44 PM
RE: Where's the endless loop? - by deanhystad - Oct-04-2021, 04:51 PM
RE: Where's the endless loop? - by Mark17 - Oct-04-2021, 05:51 PM
RE: Where's the endless loop? - by deanhystad - Oct-04-2021, 06:25 PM
RE: Where's the endless loop? - by Mark17 - Oct-04-2021, 06:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PyRun_SimpleFile calling multiprocessing Python Class cause endless init loop Xeno 2 1,131 Sep-19-2022, 02:32 AM
Last Post: Xeno
  Endless printing of a table djwilson0495 2 1,874 Aug-10-2020, 01:42 PM
Last Post: djwilson0495
  while with try and except gets stuck in an endless loop? pcarra 3 4,764 Mar-27-2019, 07:50 PM
Last Post: pcarra

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020