I've a .csv file of data of form
Date | Company 1 | Company 2 | ... | Company n |
---|---|---|---|---|
01.01.2021 | 100 | 20 | ... | 123 |
02.01.2021 | 50 | 1 | ... | 455 |
... | ... | ... | ... | ... |
8.11.2021 | 20 | 23 | ... | 122 |
The company names I've saved in a pickle file.
My aim is now to switch the plots between the different companies, e.g. by clicking with left mousebutton.
I tried the following code based on the article here
import pickle
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import itertools#
with open("file1.pickle", 'rb') as f:
tickers = pickle.load(f)
df = pd.read_csv('file2.csv')
df.set_index('Date')
fig = plt.figure()
plt.ylim(ymax=500, ymin=0)
cyc = itertools.cycle()
i = 0
for ticker in tickers:
if(i < 2):
cyc.append(df[ticker])
i = i 1
ax = fig.add_subplot(111)
ax.xaxis.set_major_locator(mdates.MonthLocator(interval=2))
line, = ax.plot(next(cyc))
def onclick(event):
line.set_ydata(next(cyc))
fig.canvas.draw()
fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
But first I get following error: TypeError: cycle expected 1 argument, got 0
And second I don't know, if I'm on the right way in general. Maybe their is a natural much more easy way to solve my problem.
CodePudding user response:
As you can read in error message - it needs .cycle(list)
but you have only .cycle()
.
Even article in your link has .cycle( (y1,y2) )
.
You can't create empty cycle
and later append()
to cycle
.
You have to first create list with data and next use it with cycle()
data = [ df[tickers[0]], df[tickers[1]] ]
cyc = itertools.cycle(data)
BTW:
I don't know what you have in tickers
but I think if you want to do it with for
-loop then you don't need i
data = []
for ticker in tickers[:2]:
data.append(df[ticker])
or using list comprehension
data = [ df[ticker] for ticker in tickers[:2] ]