python - Using Matplotlib to create adjacent graphs -
how can make first image second using matplotlib?
each "column" blue graph represents inverse of corresponding green graph "column". think format informative.
edit: code should give idea of i'm doing.
import tkinter tk import numpy np matplotlib.figure import figure matplotlib.font_manager import fontproperties matplotlib.backends.backend_tkagg import figurecanvastkagg infoframe = tk.frame(tk.tk(), width=1200, height=750, padx=5, pady=5) infoframe.grid() graphcanvas = tk.canvas(infoframe) graphcanvas.grid(columnspan=5, rowspan=2, row=1) infograph = figure(figsize=(7, 6), dpi=100) firstgraph = infograph.add_subplot(2, 1, 2, axisbg="#9ddeff") secondgraph = infograph.add_subplot(2, 1, 1, axisbg="#b2f0b2") entries = ["one", "two"] types = ["x", "y"] _tkcolors = ["black", "yellow", "magenta", "cyan", "red", "green", "blue"] index = np.arange(len(types)) width = 0.3 firstlabeldata = [] secondlabeldata = [] iterator = 0 bardata = {'interval': 1, 'data': {'one': {'std': [0.0, 0.0], 'sum': [5, 4], 'mean': [5.0, 4.0]}, 'two': {'std': [0.0, 0.0], 'sum': [14, 2], 'mean': [14.0, 2.0]}}} entry in entries: firstplot = firstgraph.bar(index+(width*iterator), bardata["data"][entry]["sum"], width, color=_tkcolors[iterator % len(_tkcolors)], yerr=bardata["data"][entry]["std"]) secondplot = secondgraph.bar(index+(width*iterator), bardata["data"][entry]["sum"], width, color=_tkcolors[iterator % len(_tkcolors)], yerr=bardata["data"][entry]["std"]) firstlabeldata.append(firstplot[0]) secondlabeldata.append(secondplot[0]) iterator += 1 firstgraph.text(3.6, 18, "inverse graph 1", weight="bold") firstgraph.set_xlabel("over " + str(30) + " iterations") firstgraph.invert_yaxis() secondgraph.text(3.5, 18, "graph 1", weight="bold") fontp = fontproperties() fontp.set_size("small") secondgraph.legend(tuple(firstlabeldata), tuple(entries), prop=fontp, loc=2) graph = figurecanvastkagg(infograph, master=graphcanvas) graph.show() graph._tkcanvas.pack(side=tk.top, expand=1) infoframe.mainloop()
something like:
fig, ax = plt.subplots() ax.set_ylim([-5, 5]) ax.axhspan(0, 5, color='b', alpha=.5, zorder=-5) ax.axhspan(-5, 0, color='r', alpha=.5, zorder=-5) j, c in enumerate(['k', 'y', 'm']): t = np.random.rand(10) b = -np.random.rand(10) h = -b + t ax.bar(.3*j + np.arange(10), h, bottom=b, color=c, width=.3)
this little brittle in colored backgrounds finite in vertical direction. suspect there better way half-infinite patches, can't think of off top of head.
if want 2 idependent axes, work:
fig = plt.figure() top_axes = fig.add_axes([.1, .5, .8, .4], axisbg="#9ddeff") bottom_axes = fig.add_axes([.1, .1, .8, .4], sharex=top_axes, axisbg="#b2f0b2") bottom_axes.invert_yaxis() top_axes.xaxis.set_visible(false) j, c in enumerate(['k', 'y', 'm']): b = np.random.rand(10) t = np.random.rand(10) top_axes.bar(.3*j + np.arange(10), t, color=c, width=.3) bottom_axes.bar(.3*j + np.arange(10), b, color=c, width=.3)
there sillyness going on 0 label on yaxis (because being double drawn), should not hard fix (might need fancy formatter).
Comments
Post a Comment