自制散点函数,内置函数可提供ax1和ax2(如果有)

时间:2019-06-12 14:59:39

标签: python matplotlib

我有很多散点函数可以使用matplotlib在一个y轴或两个y轴上进行绘制。为了缓解这种情况,我创建了自己的分散函数:draw_scatter。我在参数中指出要在哪个y轴上绘制数据。

我还指示了fig_param,它指定了fig,ax1,ax2,并且该函数返回一个元组(fig,ax1,ax2),以便对下一组数据使用相同的元素。

我不喜欢在参数ax1,ax2中使用,但是我不知道如何避免这种情况。有可用的内置函数给ax1和ax2吗?我可以在函数中调用它

然后我将具有一个功能来指定无花果的x_label,图例...。

谢谢

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 个答案:

答案 0 :(得分:0)

好吧,除非您在功能范围内内部声明了一些ax1ax2fig作为某些局部变量,否则更改和更新将应用于全局变量。

话虽如此,在您当前的示例中,您不需要将ax1, ax2, fig传递给函数。阅读this以获得对全局变量的更多见解。

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

答案 1 :(得分:0)

或者走向全球化,我认为我可以在函数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)