import numpy as np import matplotlib.pyplot as plt from matplotlib.ticker import AutoMinorLocator def plot_prettier(dpi=150, fontsize=17): plt.rcParams['figure.dpi'] = dpi plt.rc("savefig", dpi=dpi) plt.rc('font', size=fontsize) plt.rc('xtick', direction='in') plt.rc('ytick', direction='in') plt.rc('xtick.major', pad=5) plt.rc('xtick.minor', pad=5) plt.rc('ytick.major', pad=5) plt.rc('ytick.minor', pad=5) plt.rc('lines', dotted_pattern = [2., 2.]) # plt.rc('text', usetex=True) plot_prettier() def add_minor_ticks(plot, ticks, bot=True, tp=True, lft=True, rght=True, xticks=None, yticks=None): plot.tick_params(which = 'minor', bottom=bot, top=tp, left=lft, right=rght) plot.tick_params(bottom=True, top=True, left=True, right=True) if (bot or tp): if (xticks != None): plot.xaxis.set_minor_locator(AutoMinorLocator(int(xticks))) else: plot.xaxis.set_minor_locator(AutoMinorLocator(int(ticks))) if (lft or rght): if (yticks != None): plot.yaxis.set_minor_locator(AutoMinorLocator(int(yticks))) else: plot.yaxis.set_minor_locator(AutoMinorLocator(int(ticks))) example_x = np.linspace(0, 2 * np.e, 100) example_y = np.exp(-example_x) ex_fig = plt.figure() ex_plt = ex_fig.add_subplot() ex_fig.subplots_adjust(top=0.88,bottom=0.13,left=0.125,right=0.9,hspace=0.2,wspace=0.2) ex_plt.plot(example_x, example_y, '--', label=r"$ e^{-x} $") ex_plt.set_xlim(left=0) ex_plt.set_ylim(bottom=0) ex_plt.set_xlabel("x value") ex_plt.set_ylabel("y value") add_minor_ticks(ex_plt, 5) # lets get some randomly varied examples reduced_y = example_y[::10] rand_var_y = reduced_y + (np.random.normal(0, .1, size=reduced_y.shape) * reduced_y) ex_plt.errorbar(example_x[::10], rand_var_y, yerr=0.1*reduced_y, fmt=".", label="data") ex_fig.legend() plt.show()