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)