Last active
March 5, 2020 16:55
-
-
Save miracleyoo/00bf752e8e5f07ae851393363504a330 to your computer and use it in GitHub Desktop.
[Matplotlib_Python_Draw_2D_and_Bar] #python #matplotlib #draw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib | |
matplotlib.use('Agg') # Call in python file | |
# %matplotlib inline # Call in Jupyter Notebook | |
# Draw line plot | |
def draw2D(X, Y, order, xname, yname, params, xlim=None, ylim=None, rcparams=None, legend_loc=0, show=False): | |
title = params['title'] | |
colors = params['colors'] | |
markers = params['markers'] | |
linewidth = params['linewidth'] | |
markersize = params['markersize'] | |
figsize = params['figsize'] | |
if "dpi" in params.keys(): | |
dpi = params['dpi'] | |
else: | |
dpi = None | |
if rcparams is None: | |
rcparams = { | |
'figure.autolayout': True, | |
'legend.fontsize': 15, | |
'axes.labelsize': 25, | |
'axes.titlesize': 25, | |
'xtick.labelsize': 25, | |
'ytick.labelsize': 25, | |
} | |
matplotlib.rcParams.update(rcparams) | |
# X = np.array(X) | |
# Y = np.array(Y) | |
fig = plt.figure(facecolor='white',figsize=figsize,dpi=dpi) | |
plt.title(title) | |
plt.ylabel(yname) | |
if ylim is not None: | |
plt.ylim(ylim[0], ylim[1]) | |
plt.xlabel(xname) | |
if xlim is not None: | |
plt.xlim(xlim[0], xlim[1]) | |
for i, type_name in enumerate(order): | |
plt.plot(X[i], Y[i], colors[i], label=type_name, linewidth=linewidth, markersize=markersize, marker=markers[i]) | |
plt.grid() | |
if len(order)>1: | |
plt.legend(loc=legend_loc) | |
fig.canvas.draw() | |
# grab the pixel buffer and dump it into a numpy array | |
im = np.array(fig.canvas.renderer._renderer)[:, :, :-1] | |
if show: | |
plt.show() | |
else: | |
plt.close() | |
return im[:, :, (2, 1, 0)] | |
# Draw bar plot | |
def draw_barchart(X, Y, order, xname, yname, params, xlim=None, ylim=None, show=False): | |
title = params['title'] | |
colors = params['colors'] | |
barwidth = params['barwidth'] | |
figsize = params['figsize'] | |
xticks = params['xticks'] | |
if "dpi" in params.keys(): | |
dpi = params['dpi'] | |
else: | |
dpi = None | |
rcparams = { | |
'figure.autolayout': True, | |
'legend.fontsize': 20, | |
'axes.labelsize': 30, | |
'axes.titlesize': 35, | |
'xtick.labelsize': 30, | |
'ytick.labelsize': 30} | |
matplotlib.rcParams.update(rcparams) | |
X = np.array(X) | |
Y = np.array(Y) | |
fig = plt.figure(facecolor='white',figsize=figsize) | |
plt.title(title) | |
plt.ylabel(yname) | |
if ylim is not None: | |
plt.ylim(ylim[0], ylim[1]) | |
plt.xlabel(xname) | |
if xlim is not None: | |
plt.xlim(xlim[0], xlim[1]) | |
plt.xticks(X[0] + barwidth, xticks) | |
offset = 0 | |
for i, type_name in enumerate(order): | |
plt.bar(offset + X[i], Y[i], barwidth, color=colors[i], label=type_name) | |
offset += barwidth | |
plt.grid() | |
if len(order)>1: | |
plt.legend() | |
fig.canvas.draw() | |
# grab the pixel buffer and dump it into a numpy array | |
im = np.array(fig.canvas.renderer._renderer)[:, :, :-1] | |
if show: | |
plt.show() | |
else: | |
plt.close() | |
return im[:, :, (2, 1, 0)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment