""" plot using a variety of cm vs inches conversions. The example shows how default unit instrospection works (ax1), how various keywords can be used to set the x and y units to override the defaults (ax2, ax3, ax4) and how one can set the xlimits using scalars (ax3, current units assumed) or units (conversions applied to get the numbers to current units) """ from basic_units import cm, inch from pylab import figure, show, nx cms = cm *nx.arange(0, 10, 2) bottom=0*cm width=0.8*cm fig = figure() ax1 = fig.add_subplot(2,2,1) ax1.bar(cms, cms, bottom=bottom) ax2 = fig.add_subplot(2,2,2) ax2.bar(cms, cms, bottom=bottom, width=width, xunits=cm, yunits=inch) ax3 = fig.add_subplot(2,2,3) ax3.bar(cms, cms, bottom=bottom, width=width, xunits=inch, yunits=cm) ax3.set_xlim(3, 6) # scalars are interpreted in current units ax4 = fig.add_subplot(2,2,4) ax4.bar(cms, cms, bottom=bottom, width=width, xunits=inch, yunits=inch) #fig.savefig('simple_conversion_plot.png')
""" plot using a variety of cm vs inches conversions. The example shows how default unit instrospection works (ax1), how various keywords can be used to set the x and y units to override the defaults (ax2, ax3, ax4) and how one can set the xlimits using scalars (ax3, current units assumed) or units (conversions applied to get the numbers to current units) """ from basic_units import cm, inch from pylab import figure, show, nx cms = cm *nx.arange(0, 10, 2) fig = figure() ax1 = fig.add_subplot(2,2,1) ax1.plot(cms, cms) ax2 = fig.add_subplot(2,2,2) ax2.plot(cms, cms, xunits=cm, yunits=inch) ax3 = fig.add_subplot(2,2,3) ax3.plot(cms, cms, xunits=inch, yunits=cm) ax3.set_xlim(3, 6) # scalars are interpreted in current units ax4 = fig.add_subplot(2,2,4) ax4.plot(cms, cms, xunits=inch, yunits=inch) #fig.savefig('simple_conversion_plot.png') ax4.set_xlim(3*cm, 6*cm) # cm are converted to inches
from pylab import figure, nx, show # you can specify the xypoint and the xytext in different # positions and coordinate systems, and optionally turn on a # connecting line and mark the point with a marker. Annotations # work on polar axes too. In the example below, the xy point is # in native coordinates (xycoords defaults to 'data'). For a # polar axes, this is in (theta, radius) space. The text in this # example is placed in the fractional figure coordinate system. # Text keyword args like horizontal and vertical alignment are # respected fig = figure() ax = fig.add_subplot(111, polar=True) r = nx.arange(0,1,0.001) theta = 2*2*nx.pi*r line, = ax.plot(theta, r, color='#ee8d18', lw=3) ind = 800 thisr, thistheta = r[ind], theta[ind] ax.plot([thistheta], [thisr], 'o') ax.annotate('a polar annotation', xy=(thistheta, thisr), # theta, radius xytext=(0.05, 0.05), # fraction, fraction textcoords='figure fraction', arrowprops=dict(facecolor='black', shrink=0.05), horizontalalignment='left', verticalalignment='bottom', ) fig.savefig('../figures/annotation_demo.png') fig.savefig('../figures/annotation_demo.ps') show()
from pylab import figure, show, nx Ntests = 3 t = nx.arange(0.0, 1.0, 0.05) s = nx.sin(2*nx.pi*t) fig = figure() ax = fig.add_subplot(Ntests, 1, 1) N = 100 x, y = 0.9*nx.mlab.rand(2,N) area = nx.pi*(10 * nx.mlab.rand(N))**2 # 0 to 10 point radiuses ax.scatter(x,y,s=area, marker='^', c='r', label='scatter') ax.legend() ax = fig.add_subplot(Ntests, 1, 2) ax.vlines(t, [0], nx.sin(2*nx.pi*t), label='vlines') ax.legend() ax = fig.add_subplot(Ntests, 1, 3) ax.plot(t, s, 'b-', lw=2, label='a line') ax.legend() fig.savefig('legend_unit') show()
Some examples of how to annotate various artists. See matplotlib.text.Annotation for details """ from pylab import figure, show, nx from matplotlib.patches import Rectangle, CirclePolygon, Ellipse from matplotlib.text import Annotation fig = figure() ax = fig.add_subplot(111, autoscale_on=False, xlim=(-1, 5), ylim=(-1, 5)) rect = Rectangle((0.5, 0.5), 1, 3, alpha=0.3) ax.add_patch(rect) t = nx.arange(0.0, 5.0, 0.01) s = nx.sin(2 * nx.pi * t) line, = ax.plot(t, s, lw=3, color="purple") a = Annotation(rect, "A: rect", loc=("outside right", "outside top"), color="blue") ax.add_artist(a) b = Annotation(rect, "B: rect", loc=("inside left", "inside top"), autopad=8, color="blue") ax.add_artist(b) c = Annotation(rect, "C: rect", loc=("center", "center"), color="blue") ax.add_artist(c) d = Annotation(ax, "D: axes", loc=("inside right", "inside bottom"), color="red") ax.add_artist(d)
#coding: UTF-8 from pylab import figure, nx, show fig = figure() ax = fig.add_subplot(111) t = nx.arange(0.0,3.01,0.01) s = nx.sin(2*nx.pi*t) c = nx.sin(4*nx.pi*t) ax.fill(t, s, 'b', t, c, 'g', alpha=0.2) show()
#!/usr/bin/env python from basic_units import cm, inch from pylab import figure, show, nx N = 5 menMeans = (150 * cm, 160 * cm, 146 * cm, 172 * cm, 155 * cm) menStd = (20 * cm, 30 * cm, 32 * cm, 10 * cm, 20 * cm) fig = figure() ax = fig.add_subplot(111) ind = nx.arange(N) # the x locations for the groups width = 0.35 # the width of the bars p1 = ax.bar(ind, menMeans, width, color='r', bottom=0 * cm, yerr=menStd) womenMeans = (145 * cm, 149 * cm, 172 * cm, 165 * cm, 200 * cm) womenStd = (30 * cm, 25 * cm, 20 * cm, 31 * cm, 22 * cm) p2 = ax.bar(ind + width, womenMeans, width, color='y', bottom=0 * cm, yerr=womenStd) ax.set_title('Scores by group and gender') ax.set_xticks(ind + width) ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5')) ax.legend((p1[0], p2[0]), ('Men', 'Women')) ax.yaxis.set_units(inch) ax.autoscale_view()
from basic_units import radians, degrees, cos from pylab import figure, show, nx x = nx.arange(0, 15, 0.01) * radians fig = figure() fig.subplots_adjust(hspace=0.3) ax = fig.add_subplot(211) ax.plot(x, cos(x), xunits=radians) ax = fig.add_subplot(212) ax.plot(x, cos(x), xunits=degrees) fig.savefig('../figures/radian_demo.png') fig.savefig('../figures/radian_demo.ps') show()
from pylab import figure, nx, show # unit area ellipse rx, ry = 3., 1. area = rx * ry * nx.pi theta = nx.arange(0, 2 * nx.pi + 0.01, 0.1) verts = zip(rx / area * nx.cos(theta), ry / area * nx.sin(theta)) x, y, s, c = nx.mlab.rand(4, 30) s *= 10**2. fig = figure() ax = fig.add_subplot(111) ax.scatter(x, y, s, c, marker=None, verts=verts) show()
from matplotlib.widgets import MultiCursor from pylab import figure, show, nx t = nx.arange(0.0, 2.0, 0.01) s1 = nx.sin(2*nx.pi*t) s2 = nx.sin(4*nx.pi*t) fig = figure() ax1 = fig.add_subplot(211) ax1.plot(t, s1) ax2 = fig.add_subplot(212, sharex=ax1) ax2.plot(t, s2) multi = MultiCursor(fig.canvas, (ax1, ax2), color='r', lw=1) show()
from matplotlib.widgets import MultiCursor from pylab import figure, show, nx t = nx.arange(0.0, 2.0, 0.01) s1 = nx.sin(2 * nx.pi * t) s2 = nx.sin(4 * nx.pi * t) fig = figure() ax1 = fig.add_subplot(211) ax1.plot(t, s1) ax2 = fig.add_subplot(212, sharex=ax1) ax2.plot(t, s2) multi = MultiCursor(fig.canvas, (ax1, ax2), color='r', lw=1) show()
from __future__ import print_function from pylab import figure, show, nx Ntests = 3 t = nx.arange(0.0, 1.0, 0.05) s = nx.sin(2 * nx.pi * t) # scatter creates a RegPolyCollection fig = figure() ax = fig.add_subplot(Ntests, 1, 1) N = 100 x, y = 0.9 * nx.mlab.rand(2, N) area = nx.pi * (10 * nx.mlab.rand(N))**2 # 0 to 10 point radiuses ax.scatter(x, y, s=area, marker='^', c='r', label='scatter') ax.legend() # vlines creates a LineCollection ax = fig.add_subplot(Ntests, 1, 2) ax.vlines(t, [0], nx.sin(2 * nx.pi * t), label='vlines') ax.legend() # vlines creates a LineCollection ax = fig.add_subplot(Ntests, 1, 3) ax.plot(t, s, 'b-', lw=2, label='a line') ax.legend() fig.savefig('legend_unit') show()
#!/usr/bin/env python from basic_units import cm, inch from pylab import figure, show,nx N = 5 menMeans = (150*cm, 160*cm, 146*cm, 172*cm, 155*cm) menStd = ( 20*cm, 30*cm, 32*cm, 10*cm, 20*cm) fig = figure() ax = fig.add_subplot(111) ind = nx.arange(N) # the x locations for the groups width = 0.35 # the width of the bars p1 = ax.bar(ind, menMeans, width, color='r', bottom=0*cm, yerr=menStd) womenMeans = (145*cm, 149*cm, 172*cm, 165*cm, 200*cm) womenStd = (30*cm, 25*cm, 20*cm, 31*cm, 22*cm) p2 = ax.bar(ind+width, womenMeans, width, color='y', bottom=0*cm, yerr=womenStd) ax.set_title('Scores by group and gender') ax.set_xticks(ind+width) ax.set_xticklabels( ('G1', 'G2', 'G3', 'G4', 'G5') ) ax.legend( (p1[0], p2[0]), ('Men', 'Women') ) ax.yaxis.set_units(inch) ax.autoscale_view() #savefig('barchart_demo') show()