Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit71cf4b5

Browse files
committed
Use Path.arc() to interpolate polar arcs.
Currently, in polar plots, arcs are rendered by linearly interpolatingin (theta, r) space before conversion to (x, y) space (if their_interpolation_steps attribute is >1 -- this attribute is described inthe docs as "an implementation detail not intended for public use").When _interpolation_steps == 1, this PR doesn't change anything -- thereis an example (specialty_plots/radar_chart) that explicitly relies onbeing able to set _interpolation_steps to 1 and get a "polygonal" polaraxes rather than a circular one.When _interpolation_steps > 1, however, this PR looks for paths segmentsthat are either at a constant theta or a constant r. It transformsconstant-theta segments to a single segment (because interpolationdoesn't help there) and transforms constant-r segments (circular arcs)to a Bezier approximation of a circle which is already implemented inPath.arc()).This greatly decreases the number of control points for such plots invector outputs (which are thus smaller), and also improves rasterizationby mplcairo (cairo appears to be not so good at filling areas with a lotof close vertices).The many changes in baseline images are due to, well, the change ofapproximation for drawing circles.
1 parent0851204 commit71cf4b5

File tree

40 files changed

+3395
-11093
lines changed

40 files changed

+3395
-11093
lines changed

‎lib/matplotlib/projections/polar.py

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
importmatplotlib.axisasmaxis
99
importmatplotlib.markersasmmarkers
1010
importmatplotlib.patchesasmpatches
11-
importmatplotlib.pathasmpath
11+
frommatplotlib.pathimportPath
1212
importmatplotlib.tickerasmticker
1313
importmatplotlib.transformsasmtransforms
1414
importmatplotlib.spinesasmspines
@@ -57,11 +57,57 @@ def transform_non_affine(self, tr):
5757

5858
deftransform_path_non_affine(self,path):
5959
# docstring inherited
60-
vertices=path.vertices
61-
iflen(vertices)==2andvertices[0,0]==vertices[1,0]:
62-
returnmpath.Path(self.transform(vertices),path.codes)
63-
ipath=path.interpolated(path._interpolation_steps)
64-
returnmpath.Path(self.transform(ipath.vertices),ipath.codes)
60+
ifnotlen(path)orpath._interpolation_steps==1:
61+
returnPath(self.transform_non_affine(path.vertices),path.codes)
62+
xys= []
63+
codes= []
64+
last_t=last_r=None
65+
fortrs,cinpath.iter_segments():
66+
trs=trs.reshape((-1,2))
67+
ifc==Path.LINETO:
68+
(t,r),=trs
69+
ift==last_t:# Same angle: draw a straight line.
70+
xys.extend(self.transform_non_affine(trs))
71+
codes.append(Path.LINETO)
72+
elifr==last_r:# Same radius: draw an arc.
73+
# The following is complicated by Path.arc() being
74+
# "helpful" and unwrapping the angles, but we don't want
75+
# that behavior here.
76+
last_td,td=np.rad2deg([last_t,t])
77+
ifself._use_rminandself._axisisnotNone:
78+
r= ((r-self._axis.get_rorigin())
79+
*self._axis.get_rsign())
80+
iflast_td<=td:
81+
whiletd-last_td>360:
82+
arc=Path.arc(last_td,last_td+360)
83+
xys.extend(arc.vertices[1:]*r)
84+
codes.extend(arc.codes[1:])
85+
last_td+=360
86+
arc=Path.arc(last_td,td)
87+
xys.extend(arc.vertices[1:]*r)
88+
codes.extend(arc.codes[1:])
89+
else:
90+
# The reverse version also relies on the fact that all
91+
# codes but the first one are the same.
92+
whilelast_td-td>360:
93+
arc=Path.arc(last_td-360,last_td)
94+
xys.extend(arc.vertices[::-1][1:]*r)
95+
codes.extend(arc.codes[1:])
96+
last_td-=360
97+
arc=Path.arc(td,last_td)
98+
xys.extend(arc.vertices[::-1][1:]*r)
99+
codes.extend(arc.codes[1:])
100+
else:# Interpolate.
101+
trs=cbook.simple_linear_interpolation(
102+
np.column_stack([(last_t,last_r),trs]),
103+
path._interpolation_steps)[1:]
104+
xys.extend(self.transform_non_affine(trs))
105+
codes.extend(Path.LINETOfor_intrs)
106+
else:# Not a straight line.
107+
xys.extend(self.transform_non_affine(trs))
108+
codes.extend(cfor_intrs)
109+
last_t,last_r=trs[-1]
110+
returnPath(xys,codes)
65111

66112
definverted(self):
67113
# docstring inherited
Binary file not shown.

‎lib/matplotlib/tests/baseline_images/test_axes/markevery_polar.svg

Lines changed: 704 additions & 1199 deletions
Loading
Binary file not shown.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp