0

Thanks in advance for the help!

I have a for loop in which I am iterating through multiple datasets and conducting an analysis. I'd like to have all of those data sets and graphs to show up on 1 window.

However, when I run the following code, I get 5 figures (located correctly from a subplot perspective) and 5 separate windows. I'd like for the 5 figures to be in 1 window and cannot seem to figure this one out.

data_list = {'close': close_data, 'volume': volume_data, 'high': high_data, 'low': low_data, 'open': open_data}

for key, value in data_list.items():
    
    tt = datetime
    yy = value
    y_data_ma = uniform_filter1d(value, size=ma_window)
    res = fit_sin(tt, yy)
    
    x_label = "time"
    y_label = key
    graph_name = "{symbol}".format(symbol=symbol) + " " + str(legend_name) + " data - time vs " +  y_label
    
    num = int(list(data_list.keys()).index(key) + 1)
    print(num)
    plt.figure()
    plt.subplot(3,2,num)
    #ax.figure("{symbol}".format(symbol=symbol) + " " + 'time' +" vs " +  y_label)
    plt.title(graph_name, fontsize="8")
    plt.xlabel("{}".format(x_label))
    plt.ylabel("{}".format(str(y_label)))
    
    plt.plot(datetime, yy, 'r.', ms=1, label = graph_name)
    plt.plot(datetime, y_data_ma, label = str(ma_window) + ' day moving average')
    plt.plot(datetime, res["fitfunc"](datetime), "r-", label="y fit curve =   " + str(res["equation"]), linewidth=2) 
    
    plt.tick_params(axis='both',which='major',labelsize=10)
    plt.legend(loc='best')
    
    
                        

plt.tight_layout()
plt.show()

1 Answers1

0
import datetime
import numpy as np

import matplotlib.pyplot as plt
import matplotlib.dates as mdates

base = datetime.datetime.today()
datetime = [(base - datetime.timedelta(days=x)).date() for x in range(5)]

data_list = {'close': np.array([1,2,3,4,5]), 'volume': 
np.array([100,200,300,400,500]), 'high': np.array([11,21,31,41,51]), 'low': np.array([21,32,33,24,15]), 'open': np.array([12,22,32,42,52])}

fig, axes = plt.subplots(3, 2, figsize=(10, 5))
for plot_i,ax in enumerate(axes.flatten()):
    if plot_i < len(data_list.items()):
        data=list(data_list.values())[plot_i]
        label=list(data_list.keys())[plot_i]
        ax.plot(datetime, data, 'r.', ms=5, label = label)
        ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%d'))
        ax.xaxis.set_major_locator(mdates.DayLocator())
        ax.legend()
    else:
        ax.set_visible(False)

plt.tight_layout()
plt.show()
guest1
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 25 '22 at 21:06