2
votes

I have a lot of scatter functions to plot using matplotlib, either on one y axis or two y axis. To ease this, I create my own scatter function: draw_scatter. I indicate in argument on which y axis I want the data to be plotted.

I also indicate fig_param which specifies fig, ax1, ax2 and the function returns a tuple (fig, ax1, ax2) to use the same elements for the next set of data.

I don't like having in argument ax1, ax2 but I don't find how to avoid it. Is there any built function giving ax1 and ax2 if available ? I could call it in my function

I will have then a function to specify the x_label, legend ... of the fig.

Thank you

import numpy as np
import matplotlib.pyplot as plt

def draw_scatter(fig_param, ax_selec, X, Y, **kwargs):
    """Draw a figure, scatter type, on one or two axis, with selection in argument 
       Input: fig_param(fig,ax1,ax2), ax2 is None if only one axis wanted, 
              simple array X, simple array Y, parameters dict 
       Output: (fig,ax1,ax2), in order to be used again for next elements to be drawn on the same figure""" 

    fig, ax1, ax2 = fig_param

    if kwargs.get('marker_color'):  
        marker_color = kwargs['marker_color']
    else:
        marker_color='k'        
    if kwargs.get('marker_size'): 
        marker_size = kwargs['marker_size']
    else:
        marker_size = 4    

    if ax_selec == 1:
        ax1.scatter(X, Y, color=marker_color, s=marker_size)     
    else:
       ax2.scatter(X, Y, color=marker_color, s=marker_size)       
    return (fig, ax1, ax2)  

x = np.arange(0, 10.0, 1)
y1 = 2*x
y2 = np.sin(x)

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()  # -Set to None if only one y axis is needed 

param = {'marker_color':'blue',
         'marker_size' : 20} 
result_fig = draw_scatter((fig,ax1,ax2), 1, x, y1,**param)

param = {'marker_color':'red'} 
result_fig = draw_scatter(result_fig, 2, x, y2, **param)
2
Does any of the below answer solves your problem? In that case, you may mark the best answer as acceptedSheldore

2 Answers

0
votes

Well, unless you declare some ax1 and ax2 and fig as some local variables inside the function scope, the changes and updates will be applied to the global variables.

That being said, in your present example, you don't need to pass the ax1, ax2, fig to your function. Read this to get more insights into the global variables.

def draw_scatter(ax_selec, X, Y, **kwargs):
    if kwargs.get('marker_color'):  # Il faudrait pouvoir supprimer ou activé tickle sur le x. prevoir un else 
        marker_color = kwargs['marker_color']
    else:
        marker_color='k'        
    if kwargs.get('marker_size'):  # Il faudrait pouvoir supprimer ou activé tickle sur le x. prevoir un else 
        marker_size = kwargs['marker_size']
    else:
        marker_size = 4    

    if ax_selec == 1:
        ax1.scatter(X, Y, color=marker_color, s=marker_size)     
    else:
        ax2.scatter(X, Y, color=marker_color, s=marker_size)       
    return (fig, ax1, ax2)  

x = np.arange(0, 10.0, 1)
y1 = 2*x
y2 = np.sin(x)

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()  # -Set to None if only one y axis is needed 

param = {'marker_color':'blue',
         'marker_size' : 20} 
result_fig = draw_scatter( 1, x, y1,**param)

param = {'marker_color':'red'} 
result_fig = draw_scatter(2, x, y2, **param)

enter image description here

0
votes

Alternatively of going global, I think I can use inside my function plt.gcf().axes

import numpy as np
import matplotlib.pyplot as plt

def draw_scatter(fig, ax_selec, X, Y, **kwargs):

    axes_list = plt.gcf().axes

    if kwargs.get('marker_color'): 
        marker_color = kwargs['marker_color']
    else:
        marker_color='k'        
    if kwargs.get('marker_size'):  
        marker_size = kwargs['marker_size']
    else:
        marker_size = 4    

    if ax_selec == 1:
        axes_list[0].scatter(X, Y, color=marker_color, s=marker_size)     
    else:
       axes_list[1].scatter(X, Y, color=marker_color, s=marker_size)       

x = np.arange(0, 10.0, 1)
y1 = 2*x
y2 = np.sin(x)

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()  # -Set to None if only one y axis is needed 

param = {'marker_color':'blue',
         'marker_size' : 20} 
draw_scatter(fig, 1, x, y1,**param)

param = {'marker_color':'red'} 
draw_scatter(fig, 2, x, y2, **param)