Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d340c13

Browse files
committed
some polar fixes
svn path=/trunk/matplotlib/; revision=3252
1 parent 23d60f3 commit d340c13

10 files changed

Lines changed: 105 additions & 61 deletions

File tree

CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2007-04-23 Fixed some problems with polar -- dded general polygon
2+
clipping to clip the lines a nd grids to the polar axes.
3+
Added support for set_rmax to easily change the maximum
4+
radial grid. Added support for polar legend
5+
16
2007-04-16 Added autofmt_xdate to handle adjusting the bottom and
27
rotating the tick labels for date plots when the ticks
38
often overlap

examples/clippath_test.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
from matplotlib.patches import RegularPolygon
33
import matplotlib.agg as agg
44

5-
fig = figure()
6-
ax = fig.add_subplot(111)
5+
fig = figure(figsize=(8,8))
6+
ax = fig.add_subplot(111, aspect='equal')
77
t = nx.arange(0.0, 4.0, 0.01)
88
s = 2*nx.sin(2*nx.pi*8*t)
99
line, = ax.plot(t, s)
1010
line2, = ax.plot(t, 0.5*s, '--')
1111

12+
13+
1214
markers, = ax.plot(t, 2*(nx.mlab.rand(len(t))-0.5), 'bo')
1315
path = agg.path_storage()
14-
poly = RegularPolygon( (2, 0.), numVertices=100, radius=1.5)
15-
#for i, xy in enumerate(ax.transData.seq_xy_tups(poly.get_verts())):
16+
poly = RegularPolygon( (2, 0.), numVertices=100, radius=1.5,
17+
facecolor='yellow', alpha=0.25)
18+
19+
ax.add_patch(poly)
1620
for i, xy in enumerate(ax.transData.seq_xy_tups(poly.get_verts())):
1721
if i==0: path.move_to(*xy)
1822
else: path.line_to(*xy)

examples/polar_demo.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,23 @@
3939
# See the pylab rgrids and thetagrids functions for
4040
# information on how to customize the grid locations and labels
4141

42-
from pylab import *
42+
import matplotlib.numerix as nx
43+
from pylab import figure, show, rc
4344

4445
# radar green, solid grid lines
45-
rc('grid', color='red', linewidth=1, linestyle='-')
46+
rc('grid', color='#316931', linewidth=1, linestyle='-')
4647
rc('xtick', labelsize=15)
4748
rc('ytick', labelsize=15)
49+
4850
# force square figure and square axes looks better for polar, IMO
49-
figure(figsize=(8,8))
50-
ax = axes([0.1, 0.1, 0.8, 0.8], polar=True, axisbg='#d5de9c')
51+
fig = figure(figsize=(8,8))
52+
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True, axisbg='#d5de9c')
5153

52-
r = arange(0,1,0.001)
53-
theta = 2*2*pi*r
54-
polar(theta, r, color='#ee8d18', lw=3)
54+
r = nx.arange(0, 3.0, 0.01)
55+
theta = 2*nx.pi*r
56+
ax.plot(theta, r, color='#ee8d18', lw=3)
57+
ax.set_rmax(2.0)
5558

56-
title("And there was much rejoicing!", fontsize=20)
57-
#savefig('polar_demo')
59+
ax.set_title("And there was much rejoicing!", fontsize=20)
60+
fig.savefig('polar_demo')
5861
show()

lib/matplotlib/axes.py

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@
3434
import lines
3535

3636
from matplotlib.mlab import meshgrid, detrend_none, detrend_linear, \
37-
window_none, window_hanning, linspace, prctile
37+
window_none, window_hanning, prctile, linspace
3838
from matplotlib.numerix.mlab import flipud, amin, amax, dot
3939

4040
from matplotlib import rcParams
41-
from patches import Patch, Rectangle, Circle, Polygon, Arrow, Wedge, Shadow, FancyArrow, bbox_artist
41+
from patches import Patch, Rectangle, Circle, Polygon, RegularPolygon,\
42+
Arrow, Wedge, Shadow, FancyArrow, bbox_artist
4243
import table
4344
from text import Text, TextWithDash, Annotation, _process_text_args
4445
from transforms import Bbox, Point, Value, Affine, NonseparableTransformation
@@ -2803,6 +2804,7 @@ def xcorr(self, x, y, normed=False, detrend=detrend_none, usevlines=False,
28032804
kwargs.setdefault('marker', 'o')
28042805
kwargs.setdefault('linestyle', 'None')
28052806
a, = self.plot(lags, c, **kwargs)
2807+
b = None
28062808
return lags, c, a, b
28072809
xcorr.__doc__ = dedent(xcorr.__doc__) % artist.kwdocd
28082810

@@ -5224,7 +5226,7 @@ class PolarAxes(Axes):
52245226
52255227
"""
52265228

5227-
RESOLUTION = 200
5229+
RESOLUTION = 100
52285230

52295231
def __init__(self, *args, **kwarg):
52305232
"""
@@ -5291,13 +5293,15 @@ def cla(self):
52915293
self._set_artist_props(self.title)
52925294

52935295

5294-
self.thetas = linspace(0,2*math.pi, self.RESOLUTION)
5296+
self.thetas = linspace(0, 2*math.pi, self.RESOLUTION)
5297+
52955298
verts = zip(self.thetas, ones(self.RESOLUTION))
52965299
self.axesPatch = Polygon(
52975300
verts,
52985301
facecolor=self._axisbg,
52995302
edgecolor=rcParams['axes.edgecolor'],
53005303
)
5304+
53015305

53025306

53035307
self.axesPatch.set_figure(self.figure)
@@ -5313,10 +5317,19 @@ def cla(self):
53135317
self.rformatter = ScalarFormatter()
53145318
self.rformatter.set_view_interval(self.rintv)
53155319
self.rformatter.set_data_interval(self.rintd)
5316-
self.rlocator = AutoLocator()
5320+
5321+
class RadialLocator(AutoLocator):
5322+
'enforce strictly positive radial ticks'
5323+
5324+
def __call__(self):
5325+
ticks = AutoLocator.__call__(self)
5326+
return [t for t in ticks if t>0]
5327+
5328+
self.rlocator = RadialLocator()
53175329
self.rlocator.set_view_interval(self.rintv)
53185330
self.rlocator.set_data_interval(self.rintd)
53195331

5332+
53205333
angles = arange(0, 360, 45)
53215334
radii = arange(0.2, 1.1, 0.2)
53225335
self.set_thetagrids(angles)
@@ -5334,8 +5347,8 @@ def get_children(self):
53345347
children.extend(self.texts)
53355348
children.extend(self.artists)
53365349
children.extend(self.images)
5337-
#if self.legend_ is not None:
5338-
# children.append(self.legend_)
5350+
if self.legend_ is not None:
5351+
children.append(self.legend_)
53395352
children.extend(self.collections)
53405353
children.append(self.title)
53415354
children.append(self.axesPatch)
@@ -5351,12 +5364,15 @@ def grid(self, b):
53515364
self._gridOn = b
53525365

53535366
def regrid(self, rmax):
5367+
rmax = float(rmax)
53545368
self.axesPatch.xy = zip(self.thetas, rmax*ones(self.RESOLUTION))
5369+
53555370
val = rmax*math.sqrt(2)
53565371
self.viewLim.intervaly().set_bounds(val, val)
53575372

53585373
ticks = self.rlocator()
53595374
self.set_rgrids(ticks)
5375+
self.rformatter.set_locs(ticks)
53605376

53615377
for t in self.thetagridlabels:
53625378
t.set_y(1.05*rmax)
@@ -5372,7 +5388,7 @@ def autoscale_view(self, scalex=True, scaley=True):
53725388
self.rintv.set_bounds(rmin, rmax)
53735389
self.regrid(rmax)
53745390

5375-
def set_rgrids(self, radii, labels=None, angle=22.5, **kwargs):
5391+
def set_rgrids(self, radii, labels=None, angle=22.5, rpad=0.05, **kwargs):
53765392
"""
53775393
set the radial locations and labels of the r grids
53785394
@@ -5383,6 +5399,9 @@ def set_rgrids(self, radii, labels=None, angle=22.5, **kwargs):
53835399
53845400
if labels is None, the self.rformatter will be used
53855401
5402+
rpad is a fraction of the max of radii which will pad each of
5403+
the radial labels in the radial direction.
5404+
53865405
Return value is a list of lines, labels where the lines are
53875406
matplotlib.Line2D instances and the labels are matplotlib.Text
53885407
instances
@@ -5393,12 +5412,21 @@ def set_rgrids(self, radii, labels=None, angle=22.5, **kwargs):
53935412
ACCEPTS: sequence of floats
53945413
"""
53955414

5415+
5416+
5417+
rmin = nx.amin(radii)
5418+
if rmin<=0:
5419+
raise ValueError('radial grids must be strictly positive')
5420+
5421+
rpad = rpad * max(radii)
53965422
popall(self.rgridlines)
5397-
theta = linspace(0,2*math.pi, self.RESOLUTION)
5423+
5424+
theta = linspace(0., 2*math.pi, self.RESOLUTION)
53985425
ls = rcParams['grid.linestyle']
53995426
color = rcParams['grid.color']
54005427
lw = rcParams['grid.linewidth']
54015428

5429+
rmax = self.get_rmax()
54025430
for r in radii:
54035431
r = ones(self.RESOLUTION)*r
54045432
line = Line2D(theta, r, linestyle=ls, color=color, linewidth=lw)
@@ -5407,7 +5435,6 @@ def set_rgrids(self, radii, labels=None, angle=22.5, **kwargs):
54075435
line.set_transform(self.transData)
54085436
self.rgridlines.append(line)
54095437

5410-
54115438
popall(self.rgridlabels)
54125439

54135440

@@ -5418,7 +5445,7 @@ def set_rgrids(self, radii, labels=None, angle=22.5, **kwargs):
54185445
if labels is None:
54195446
labels = [self.rformatter(r,0) for r in radii]
54205447
for r,l in zip(radii, labels):
5421-
t = Text(angle/180.*math.pi, r, l,
5448+
t = Text(angle/180.*math.pi, r+rpad, l,
54225449
fontproperties=props, color=color,
54235450
horizontalalignment='center', verticalalignment='center')
54245451
t.set_transform(self.transData)
@@ -5460,7 +5487,8 @@ def set_thetagrids(self, angles, labels=None, fmt='%d', frac = 1.1,
54605487
color = rcParams['grid.color']
54615488
lw = rcParams['grid.linewidth']
54625489

5463-
r = linspace(0, self.get_rmax(), self.RESOLUTION)
5490+
rmax = self.get_rmax()
5491+
r = linspace(0., rmax, self.RESOLUTION)
54645492
for a in angles:
54655493
theta = ones(self.RESOLUTION)*a/180.*math.pi
54665494
line = Line2D(theta, r, linestyle=ls, color=color, linewidth=lw)
@@ -5472,7 +5500,7 @@ def set_thetagrids(self, angles, labels=None, fmt='%d', frac = 1.1,
54725500
color = rcParams['xtick.color']
54735501

54745502
props=FontProperties(size=rcParams['xtick.labelsize'])
5475-
r = frac*self.get_rmax()
5503+
r = frac*rmax
54765504
if labels is None:
54775505
labels = [fmt%a for a in angles]
54785506
for a,l in zip(angles, labels):
@@ -5498,51 +5526,59 @@ def draw(self, renderer):
54985526
self.transData.freeze() # eval the lazy objects
54995527
self.transAxes.freeze() # eval the lazy objects
55005528

5501-
tverts = self.transData.seq_xy_tups(self.axesPatch.get_verts())
5529+
verts = self.axesPatch.get_verts()
5530+
tverts = self.transData.seq_xy_tups(verts)
55025531

5503-
def make_clippath():
5504-
clippath = agg.path_storage()
5505-
for i, xy in enumerate(tverts):
5506-
if i==0: clippath.move_to(*xy)
5507-
else: clippath.line_to(*xy)
5508-
clippath.close_polygon()
5509-
return clippath
5532+
#for i,v,t in zip(range(len(verts)), verts, tverts):
5533+
# print i,v,t
5534+
5535+
55105536

5537+
l,b,w,h = self.figure.bbox.get_bounds()
55115538
clippath = agg.path_storage()
55125539
for i, xy in enumerate(tverts):
5513-
if i==0: clippath.move_to(*xy)
5514-
else: clippath.line_to(*xy)
5540+
x,y = xy
5541+
y = h-y
5542+
if i==0: clippath.move_to(x, y)
5543+
else: clippath.line_to(x, y)
55155544
clippath.close_polygon()
55165545

55175546
#self._update_axes()
55185547
if self.axison:
55195548
if self._frameon: self.axesPatch.draw(renderer)
55205549

55215550
if self._gridOn:
5522-
for l in self.rgridlines+self.thetagridlines:
5523-
#l.set_clip_path(make_clippath())
5524-
#l.set_clip_path(clippath)
5551+
for l in self.rgridlines:
5552+
l.set_clip_path(clippath)
55255553
l.draw(renderer)
55265554

5555+
for l in self.thetagridlines:
5556+
l.set_clip_path(clippath)
5557+
l.draw(renderer)
5558+
55275559
for line in self.lines:
5528-
#line.set_clip_path(make_clippath())
55295560
line.set_clip_path(clippath)
55305561

5531-
for t in self.thetagridlabels+self.rgridlabels:
5532-
t.draw(renderer)
5533-
55345562
artists = []
55355563
artists.extend(self.lines)
55365564
artists.extend(self.texts)
55375565
artists.extend(self.collections)
55385566
artists.extend(self.patches)
55395567
artists.extend(self.artists)
5568+
55405569
dsu = [ (a.zorder, a) for a in artists]
55415570
dsu.sort()
55425571

55435572
for zorder, a in dsu:
55445573
a.draw(renderer)
55455574

5575+
5576+
for t in self.thetagridlabels+self.rgridlabels:
5577+
t.draw(renderer)
5578+
5579+
if self.legend_ is not None:
5580+
self.legend_.draw(renderer)
5581+
55465582
self.title.draw(renderer)
55475583

55485584

@@ -5614,12 +5650,6 @@ def toggle_log_lineary(self):
56145650
'toggle between log and linear axes ignored for polar'
56155651
pass
56165652

5617-
def legend(self, *args, **kwargs):
5618-
"""
5619-
LEGEND(*args, **kwargs)
5620-
Not implemented for polar yet -- use figlegend
5621-
"""
5622-
raise NotImplementedError('legend not implemented for polar yet -- use figlegend')
56235653

56245654
def table(self, *args, **kwargs):
56255655
"""

lib/matplotlib/legend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def get_handles(ax):
328328

329329
inv = ax.transAxes.inverse_xy_tup
330330
for handle in handles:
331-
print 'autolegend', handle, type(handle)
331+
332332
if isinstance(handle, Line2D):
333333

334334
xdata = handle.get_xdata(orig=False)

lib/matplotlib/mpl-data/matplotlibrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
# the default backend; one of GTK GTKAgg GTKCairo FltkAgg QtAgg TkAgg
2828
# Agg Cairo GD GDK Paint PS PDF SVG Template
2929
backend : TkAgg
30-
numerix : numpy # numpy, Numeric or numarray
30+
numerix : Numeric # numpy, Numeric or numarray
3131
units : True
3232
#interactive : False # see http://matplotlib.sourceforge.net/interactive.html
3333
#toolbar : toolbar2 # None | classic | toolbar2

lib/matplotlib/patches.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,14 +444,14 @@ def __init__(self, xy, numVertices, radius=5, orientation=0,
444444
def get_verts(self):
445445
theta = 2*pi/self.numVertices*arange(self.numVertices) + \
446446
self.orientation
447-
r = self.radius
448-
x, y = self.x, self.y
447+
r = float(self.radius)
448+
x, y = map(float, self.xy)
449449

450450
xs = x + r*cos(theta)
451451
ys = y + r*sin(theta)
452452

453-
xs = self.convert_xunits(xs)
454-
ys = self.convert_yunits(ys)
453+
#xs = self.convert_xunits(xs)
454+
#ys = self.convert_yunits(ys)
455455

456456

457457
self.verts = zip(xs, ys)

lib/matplotlib/widgets.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,8 +734,6 @@ class MultiCursor:
734734
"""
735735
def __init__(self, canvas, axes, useblit=True, **lineprops):
736736
self.canvas = canvas
737-
self.canvas.mpl_connect('motion_notify_event', self.onmove)
738-
self.canvas.mpl_connect('draw_event', self.clear)
739737
self.axes = axes
740738
self.lines = [ax.axvline(1, visible=False, **lineprops) for ax in axes]
741739

@@ -744,6 +742,9 @@ def __init__(self, canvas, axes, useblit=True, **lineprops):
744742
self.background = None
745743
self.needclear = False
746744

745+
self.canvas.mpl_connect('motion_notify_event', self.onmove)
746+
self.canvas.mpl_connect('draw_event', self.clear)
747+
747748

748749
def clear(self, event):
749750
'clear the cursor'

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@
125125
raise RuntimeError("You must install one or more of numpy, Numeric, and numarray to build matplotlib")
126126

127127

128-
#NUMERIX = ['numpy']
129-
rc['numerix'] = NUMERIX[-1]
128+
NUMERIX = ['numpy']
129+
#rc['numerix'] = NUMERIX[-1]
130130

131131
ext_modules = []
132132

0 commit comments

Comments
 (0)