Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Invalid index
#1
I'm trying to plot five series as subplots:
import pandas as pd
import matplotlib.pyplot as plt
import datetime

rawdate_start = '2007-01-01' #input('Please enter start date as YYYY-MM-DD: ')
rawdate_end = '2019-01-01' #input('Please enter end date as YYYY-MM-DD: ')
markets = ['ES', 'GC', 'CL', 'EC', 'US']

subplot_count = 1
fig, ax = plt.subplots (3,2)
plt.figure(facecolor='lightgrey')
ax_indices = {1:'0,0',2:'0,1',3:'0,2',4:'1,0',5:'1,1',6:'1,2'}
print(ax_indices[subplot_count])
for market in markets:
    columnname_0 = market+'_Close'
    df_name = market
    market = pd.read_csv(r'C:\Users\drkle\{}(daily).csv'.format(market), parse_dates=["Date"], index_col="Date")
    market_cut = market.drop(['Open','High','Low','Vol','OI'],axis=1)
    market_cut = market_cut[rawdate_start:rawdate_end]
    market_cut.columns = [columnname_0]
#    ax[ax_indices[subplot_count]].plot(market_cut[columnname_0])
    ax[0,0].plot(market_cut[columnname_0])
    subplot_count += 1
plt.show()
This plots all lines in the upper left subplot. If I comment out Line 22 and uncomment out Line 21, then I get "IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices."

Is there a way I can make that work? Thanks!
Reply
#2
not able to test, but try to replace line 12 with:
ax_indices = {1:(0,0),2:(0,1),3:(0,2),4:(1,0),5:(1,1),6:(1,2)}
and line 21 with
ax[*ax_indices[subplot_count]].plot(market_cut[columnname_0])
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Dec-22-2020, 07:29 PM)buran Wrote: not able to test, but try to replace line 12 with:
ax_indices = {1:(0,0),2:(0,1),3:(0,2),4:(1,0),5:(1,1),6:(1,2)}
and line 21 with
ax[*ax_indices[subplot_count]].plot(market_cut[columnname_0])

I get a syntax error. Could it be because the comma is missing? When unpacked, those numbers take the format 'd d' (where d is 0, 1, or 2).
Reply
#4
In addtion to buran's change of line 12, try to change line 21 with
ax[ax_indices[subplot_count][0],ax_indices[subplot_count][1]].plot(market_cut[columnname_0])
Reply
#5
I'll try that, MK_CodingSpace.

This also works: replace Line 12 with
axL_index = {1:0,2:0,3:1,4:1,5:2,6:2}
axR_index = {1:0,2:1,3:0,4:1,5:0,6:1}
and replace Line 21 with
    ax[axL_index[subplot_count],axR_index[subplot_count]].plot(market_cut[columnname_0])
Thanks everyone!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  zlib decompress error: invalid code lengths set / invalid block type DreamingInsanity 0 6,828 Mar-29-2020, 12:44 PM
Last Post: DreamingInsanity

Forum Jump:

User Panel Messages

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