Python Forum
Mathplotlib - passing reference to axs to function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mathplotlib - passing reference to axs to function
#1
I have defined a series of subplots:
fig, ((ax1, ax3), (ax2, ax4), (ax5, ax7), (ax6, ax8)) = plt.subplots(4,2)
fig.subplots_adjust(left = .1, right = 1., bottom = .1, \
                    top = .9, wspace = .2, hspace = .5)
For several of these axes, I want to place vertical lines at events specified in a dictionary:
for ev in majorEvents.values():
    ax1.axvline(ev[6], ev[1], ev[2], color = ev[3], linestyle = ev[4],\
                marker = ev[5], label = ev[0])
In this case it was ax1. But I want to do this in several of the axes. I can certainly replicate this short code where I need it, but I would like to create a function that would do this for me. All I would have to do is pass the axes indicator: ax1, ax3, etc.

So the function I have is:
def showEvents(plotID):
    for ev in majorEvents.values():
        eval(plotID + '.axvlines(ev[6], ev[1], ev[2], color = ev[3], \
             linestyle = ev[4], marker = ev[5], label = ev[0])')
When I call the function,
showEvents('ax1') 
i get the following:
Error:
File "/Users/me/Python/Umbrella/umbrella.py", line 463, in <module> showEvents("ax1") File "/Users/me/Python/Umbrella/umbrella.py", line 122, in showEvents eval(plotID + '.axvlines(ev[6], ev[1], ev[2], color = ev[3], \ File "<string>", line 1, in <module> AttributeError: 'AxesSubplot' object has no attribute 'axvlines'
So passing a string with the reference to the axes to the function and then concatenating and evaluating the resultant does not work.

Not sure where to go from here.

Any suggests would be greatly appreciated.
Reply
#2
So plotID is passed to the function, but where does the function get axvlines? Probably need to pass that too.
Reply
#3
Why are you using eval?

def showEvents(ax, events):
    for ev in events.values():
        ax.axvlines(ev[6], ev[1], ev[2], color = ev[3], \
             linestyle = ev[4], marker = ev[5], label = ev[0])[

showEvents(ax1, majorEvents)
Reply
#4
Dean

I had previously tried what you are suggesting:

def showEvents(ax, events):
    for ev in events.values():
        ax.axvlines(ev[6], ev[1], ev[2], color = ev[3], \
             linestyle = ev[4], marker = ev[5], label = ev[0])
and then call it as
showEvents(ax1, allEvents)
and receive the error
Error:
ax.axvlines(ev[6], ev[1], ev[2], color = ev[3], \ AttributeError: 'AxesSubplot' object has no attribute 'axvlines'
I had hoped that doing the eval of the concatenation would resolve the issue. But it does not.
Reply
#5
Try axvline, not axvlines.
Reply
#6
Why didn't I catch that before??!! Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  passing dictionary to the function mark588 2 932 Dec-19-2022, 07:28 PM
Last Post: deanhystad
  reference from another function Frankduc 10 2,245 Mar-01-2022, 01:10 PM
Last Post: Frankduc
  How to automatically close mathplotlib output window? Pedroski55 2 1,890 Jun-17-2021, 09:00 AM
Last Post: Pedroski55
  Passing flags to python script, through a function xbit 4 3,874 Apr-20-2021, 06:32 AM
Last Post: ndc85430
  Passing argument from top-level function to embedded function JaneTan 2 2,203 Oct-15-2020, 03:50 PM
Last Post: deanhystad
  passing variable to function Rejoice 4 2,825 Sep-11-2020, 03:27 AM
Last Post: Pleiades
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,513 Sep-07-2020, 08:02 AM
Last Post: perfringo
  reference in pop function for linked list oloap 0 1,540 Mar-14-2020, 05:52 PM
Last Post: oloap
  Passing variable to another function JonnyDriller 10 3,683 Feb-05-2020, 03:46 AM
Last Post: JonnyDriller
  Passing an argument by reference Exsul 12 4,602 Aug-22-2019, 07:29 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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