def eventplot(): """ copied from http://matplotlib.org/xkcd/examples/pylab_examples/eventplot_demo.html""" import numpy as np from smartplotlib import subplot np.random.seed(0) # create random data data1 = np.random.random([6, 50]) # set different colors for each set of positions colors1 = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 0], [1, 0, 1], [0, 1, 1]]) # set different line properties for each set of positions # note that some overlap lineoffsets1 = np.array([-15, -3, 1, 1.5, 6, 10]) linelengths1 = [5, 2, 1, 1, 3, 1.5] ax1, ax2 = subplot.iteraxes(2,1, title=["horizontal eventplot", "vertical eventplot"], orientation=["horizontal", "vertical"], figure="way 1" ) # clear the figure ax1.fclear() ax1.eventplot(data1, colors=colors1, lineoffsets=lineoffsets1, linelengths=linelengths1) ax1.axes() ax2.eventplot(data1, colors=colors1, lineoffsets=lineoffsets1, linelengths=linelengths1) ax2.axes() ax1.go("show", "draw") ###### # another way to do the same axs = subplot(figure="way 2") ## # data are both the same, the only things that will change # is the orienatation and title axs.eventplot.update(positions=data1, colors=colors1, lineoffsets=lineoffsets1, linelengths=linelengths1) for a in axs.iteraxes(2,1, title=["horizontal eventplot", "vertical eventplot"], orientation=["horizontal", "vertical"]): a.go("axes","eventplot") axs.go("show", "draw") return axs
def histogram(): import numpy as np from smartplotlib import dataplot, subplot, alias data1 = np.random.normal(size=(1000,)) data2 = np.random.normal(scale=0.8, size=(1000,)) dp = dataplot(data1, figure="histograms") ## clear the figure dp.go("fclear") # make a copy of dp.histogram h = dp.histogram.derive(bins=20, rwidth=0.8, align="mid") # set default for errorbar and turn of label so it does not # a appear twice h.errorbar.update(color="k", linestyle="none", label=None) ### # histogram version does not accept several set of data # but one can still stack by calling back the histogram plot h1 = h(axes=221, density=False, ylabel="Stacked Histogram", label="some data" ) h1.go("axes", "bar", "errorbar") ### # make a second histogram on the same figure # calling histogram again assure that the same bins are used for # both, because they are now set in h1 as an array h1(data2, stacked=True, color="green", label="more data").bar() h1.legend() h2 = h(density=True, ylabel="Fitted Density", axes=222 ) h2.go("axes", "bar", "errorbar") ## # fit the distribution an plot # label=True will generate a label with fit result fit = dp.distribfit(label=True) fit.plot(); fit.derive(min=-fit["scale"]+fit["loc"], max=+fit["scale"]+fit["loc"], label=None, npoints=2, color="red" ).plot() for s in dp.stat.iter(fstat=["-std","+std"]): s().axvline(color="red", linestyle=":") h2.legend() # should print fir result #### # make a second density histogram #h2.histogram(data2, stacked=True, color="green").bar() #### # Age pyramid like histogram h3 = h( ylabel="+- Histogram", axes=223 ) h3.go("axes", "bar", "errorbar") #### # make a opposite histogram, since stacked is false one must # put the counter to 0 h3(data2, stacked=False, count=0, amplitude=-1, color="green").bar() #### # Side by side histogram # since we are going to make to histogram, put the rwidth to 0.4 # and since align="mid" (="center") shift the histograms by -0.2 h4 = h( ylabel="Side by Side Histogram", axes=224, rwidth=0.4, align="mid", roffset=-0.2 ) h4.go("axes", "bar", "errorbar") #### # make a opposite histogram, since stacked is false one must # put the counter to 0 h4(data2, stacked=False, color="green").bar() h4.go("show", "draw") return h1, h2, h3, h4 a = subplot(figure="matplotlib.hist", go=["fclear","axes"], sharey=h, sharex=h, axes=211 ) a.hist([data1,data2], bins=bins, color=["blue", "green"], stacked=True, align=align) a2 = subplot(figure="matplotlib.hist", sharey=h2, sharex=h2, axes=212) a2.hist([data1,data2], bins=bins, color=["blue", "green"], stacked=False, align=align) a.go("show", "draw") return h