)Uc@ spdZddlmZddlZddlZddlZddlmZddl j Z ddl j Z ddljZddljZddljZddljZddljZddljZddljZddljZddljZddl j!Z!ddl"m#Z#dej$fdYZ%dfd YZ&d ej'e&fd YZ(d e(fd YZ)dS(sQ These are classes to support contour plotting and labelling for the axes class i(tdivisionN(tma(tBlockingContourLabelert ClabelTextcB seZdZdZRS(s Unlike the ordinary text, the get_rotation returns an updated angle in the pixel coordinate assuming that the input rotation is an angle in data coordinate (or whatever transform set). cC shtjj|}|j}|j\}}|jtj|gtj||gg}|dS(Ni(ttexttTextt get_rotationt get_transformt get_positionttransform_anglestnptarray(tselftanglettranstxtyt new_angles((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pyR(s  (t__name__t __module__t__doc__R(((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pyR"stContourLabelercB seZdZdZdZdZdZdZdZdZ dZ d Z dd d Z d Zd ZdZdZdZddZdZRS(s3Mixin to provide labelling capability to ContourSetcO s|jdd}|jdd}|jdd}|jdd|_|jdd}|jd t|_|jd t|_|jd t|_t|d kr|j }t t|}nt|dkrt |d } gg}}xFt |j D]5\} } | | kr |j | |j | q q Wt|t| krd t| } | d7} | t|j 7} t| qn td||_||_tj|_|dkrt|jj} njt|tttgkrtdn@t|tkr@t|jj} n|jj||} | gt||_|dkr||_tj|j|j|_ nWt!j"|dt|j}t t|j|_ t#j$d|dt!j%|_g|_&|jr<dGHdGH|s dGHnt'|}|||n|j(|||j)|_*|j&|_+|j,|_-t.j/d|j)|_0|j0S(s} call signature:: clabel(cs, **kwargs) adds labels to line contours in *cs*, where *cs* is a :class:`~matplotlib.contour.ContourSet` object returned by contour. :: clabel(cs, v, **kwargs) only labels contours listed in *v*. Optional keyword arguments: *fontsize*: See http://matplotlib.sf.net/fonts.html *colors*: - if *None*, the color of each label matches the color of the corresponding contour - if one string color, e.g. *colors* = 'r' or *colors* = 'red', all labels will be plotted in this color - if a tuple of matplotlib color args (string, float, rgb, etc), different labels will be plotted in different colors in the order specified *inline*: controls whether the underlying contour is removed or not. Default is *True*. *inline_spacing*: space in pixels to leave on each side of label when placing inline. Defaults to 5. This spacing will be exact for labels at locations where the contour is straight, less so for labels on curved contours. *fmt*: a format string for the label. Default is '%1.3f' Alternatively, this can be a dictionary matching contour levels with arbitrary strings to use for each contour level (i.e., fmt[level]=string) *manual*: if *True*, contour labels will be placed manually using mouse clicks. Click the first button near a contour to add a label, click the second button (or potentially both mouse buttons at once) to finish adding labels. The third button can be used to remove the last label added, but only if labels are not inline. Alternatively, the keyboard can be used to select label locations (enter to end label placement, delete or backspace act like the third mouse button, and any other key will select a label location). *rightside_up*: if *True* (default), label rotations will always be plus or minus 90 degrees from level. *use_clabeltext*: if *True* (default is False), ClabelText class (instead of matplotlib.Text) is used to create labels. ClabelText recalculates rotation angles of texts during the drawing time, therefore this can be used if aspect of the axes changes. .. plot:: mpl_examples/pylab_examples/contour_demo.py tfontsizetinlineitinline_spacingitfmts%1.3ftcolorstuse_clabeltexttmanualt rightside_upisSpecified levels s don't match available levels s-Illegal arguments to clabel, see help(clabel)s$Font size must be an integer number.tNtcmaptnorms9Select label locations manually using first mouse button.s.End manual selection with second mouse button.s1Remove last label by clicking third mouse button.s text.TextN(1tgettNonetlabelFmttFalset_use_clabeltextt labelManualtTrueRtlentlevelstrangetlistt enumeratetappendtstrt ValueErrort TypeErrortlabelLevelListtlabelIndiceListt font_managertFontPropertiestlabelFontPropstinttget_size_in_pointsttypetfloattset_sizetlabelFontSizeListt labelMappableR ttaketcvaluestlabelCValueListRtListedColormaptcmtScalarMappabletNoNormtlabelXYsRtlabelst labelTextstcltcl_xyt labelCValuest cl_cvaluestcbookt silent_listtlabelTextsList(R targstkwargsRRRt_colorsR)tindicestlevlabstitlevtmsgt font_sizeRtblocking_contour_labeler((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pytclabel4srU                 c C st|}|d|kr dStj|dddf}tj|dddf}tj|dddf}tj|dddf}|}||d|ks||d|krdSdSdS(s-if contours are too short, don't plot a labeli iNig333333?(R(R tamaxtamin( R t linecontourt labelwidthtlcsizetxmaxtxmintymaxtymintlw((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pyt print_labels (cC s|jgkr|g|jD]1}tj||dd||dd^q}x-|D]}|d|krqdSdSqWWndSdS(s6if there's a label already nearby, find a better placeiiig333333?N(RDR tsqrt(R RRRbtloctdisttd((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pyt too_closes>  c C st|d}tj|}xS|D]K}||||||} } |j| | |rdq&q&| | |fSq&W|d}||||||} } | | |fS(sB labels are ploted at a location with the smallest dispersion of the contour from a straight line unless there's another label nearby, in which case the second best place on the contour is picked up if there's no good place a label isplotted at the beginning of the contour ii(R6R targsortRh( R t distancestXXtYYtysizeRbthysizetadisttindRR((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pytget_label_coordss   cC stj|s$|j||}ntjj|\}}|dkrt|dsitj|_ n|j j ||\}}}np|rt|dst j d|_ n|j j|ddd|j\}}|j}nt||}|S(s$get the width of the label in pointstTeXt _TeX_managert_mathtext_parsertbitmaptdpiiHtprop(RKtis_string_liketget_textRRt is_math_textthasattrt texmanagert TexManagerRstget_text_width_height_descenttmathtexttMathTextParserRttparseR5t get_widthR((R RTRtfsizetismathRbt_timg((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pytget_label_widths !'cC stjtj|jjjdddd}tj|d|d}|j||j ||d|j d|jj j j }tj|jddddfd}|S(s This computes actual onscreen label width. This uses some black magic to determine onscreen extent of non-drawn label. This magic may not be very robust. itaxisiitktrendererN(R tmeantasarraytaxRtreshapeRRtset_label_propsRytget_window_extenttfiguretcanvasRtdifftcorners(R RTRRtxxtttbboxRb((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pytget_real_label_width#s 3,cC sA|j||j||j|j|j|jjdS(s0set the label properties - color, fontsize, textN(tset_textt set_colortset_fontpropertiesR5t set_clip_boxRR(R tlabelRtcolor((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pyR;s  cC s6tj|r|St|tr*||S||SdS(sget the text of the labelN(RKRxt isinstancetdict(R RTR((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pyRyBs cC s#t|}|dkr4ttj||}nd}|dkrO|}n t|}tj|dddf||f}tj|dddf||f}|dddfj|d}|dddfj|d} |dddfj|d} |dddfj|d} ||| | | || |} tj| | d| |dj} tjj gt |D]}t | || |^qd}|j |||||\}}}g|D]}t |^q}|j||f}|||fS(sfind a good place to plot a label (relatively flat part of the contour) and the angle of rotation for the text object iNiii(R(R6R tceiltresizeRRdtraveltaddtreduceR*tabsRqttupletindex(R R[R\tnsizetxsizeRmRkRltyfirsttylasttxfirsttxlasttstLRSRfRRRptltlctdind((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pyt locate_labelLs(     ((""""")@$ic C s|d krg}n|d}tj|}|rtj||d!||d f}t|rtj||d!||d f}nd}ntj|}|||}tj| |g} |rtj|ddg} ntj| } tj ||| | dt } tj | ddj } tj | dkrLd} n%tj| d| ddtj} |jr| d kr| d} n| d krd| } qng}t|r| | tj| |g} tj |tjt|| dt}tj|d rVt|d|dkrVtj ||| dg}ntj|d rt|d|dkrtj ||| dg}ntj|dtj|dg}|rtj tj|r|jtj|||d|dd!|fqqtj|dsZ|jtj||dd |fntj|ds|jtj|||dfqn| |fS( s This function calculates the appropriate label rotation given the linecontour coordinates in screen units, the index of the label location and the label width. It will also break contour and calculate inlining if *lc* is not empty (lc defaults to the empty list if None). *spacing* is the space around the label in pixels to leave empty. Do both of these tasks at once to avoid calling mlab.path_length multiple times, which is relatively costly. The method used here involves calculating the path length along the contour in pixel coordinates and then looking approximately label width / 2 away from central point to determine rotation and then to break contour if desired. g@iiitextrapRggf@iZiN(R"tmlabtis_closed_polygonR tr_R(t path_lengthR t zeros_liket less_simple_linear_interpolationR'RRtalltarctan2tpiRtarangeR$tisnanR6tfloorRR-(R tslcRpRbRtspacingthlwtclosedtpltxitdptlltddtrotationtnlctItxy1txy2((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pytcalc_label_rot_and_inlineqs\  " %   %     $..&6)(c C sO|jjjj||f\}}tj||d|dddd}|S(NRthorizontalalignmenttcentertverticalalignment(Rt transDatatinvertedttransform_pointRR(R RRRtdxtdyR((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pyt_get_label_texts ' c C s|jjj}|j||f\}}|jtj|gtj||gg}t||d|ddddd}|S(NRiRRR(RRRRR R R R( R RRRt transDataInvRRt drotationR((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pyt_get_label_clabeltexts cC s|jj|d|j}|j||j}|j||||jj||jj||j j||f|j j |dS(Ntalpha( R<tto_rgbaRRyR#RRFR-RIRDRt add_artist(R RRRRTtcvalueRt_text((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pyt _add_labelscC s2|j|||}|j|||||dS(s6 Addd contour label using Text class. N(RR(R RRRRTRR((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pyt add_labelscC s2|j|||}|j|||||dS(s< Addd contour label using ClabelText class. N(RR(R RRRRTRR((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pytadd_label_clabeltexts icC s0|jj||jj|}|jdS(s>Defaults to removing last label, but any index can be suppliedN(RItpopRFtremove(R RR((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pyt pop_labelscC s|jr|j}n |j}xt|j|j|j|jD]\}}}}|j|}|j } |j ||j |} g} |j } xAt | D]3\} }|j}| j|}tj|rtj||ddddff}n|}|j|| r|j|| \}}}|rC|}nd}|j||| ||\}}|||||||rx<|D]1}t|dkr| jtj|qqWqq| j|qW|r@| 2| j| q@q@WdS(Nii(R%RRtzipR2R1R;R?t collectionsRRR#t get_pathsR,tverticest transformRRR RRcRR"RR(R-tmpathtPathtextend(R RRRticonRTRRtconRRbt additionstpathstsegNumtlinepathRtslc0RRRRptlcargRtnewtn((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pyREsD       ,   #N(RRRRXRcRhRqRRRRyRR"RRRRRRRRE(((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pyR1s$      %i     t ContourSetcB seZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdedZRS(s Store a set of contour lines or filled regions. User-callable method: clabel Useful attributes: ax: the axes object in which the contours are drawn collections: a silent_list of LineCollections or PolyCollections levels: contour levels layers: same as levels for line contours; half-way between levels for filled contours. See _process_colors method. c O s||_|jdd!|_|jdt|_|jdd!|_|jdd!|_|jdd!|_|jdd!|_ |jdd!|_ |jdd!}|jd d!|_ |jd d!}|jd d |_ |jd t |_|jdd|_|jdd!|_t|t jsPt|jtjrt |_|d!krtt j}n|j d k rtdqn t|_|j d!k rn|j d!k rn|d!k rn|j d!k r|d!k rtdn|j dkrtjd|_ n|j|||j|j d!k rt|j}|jrk|d8}nt j|j d|}n|jrtjd|_ntjd|_g|_ g|_!i|d6}|d!k r||d RRttcolorsRRRt set_facecolorRRFRIt set_alphaR<RARBR#(R trgbaR@Rt collectionRtcv((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pyR#5s4  "cC s|jdkrC|jr*tj|_qCtj|d|_n|jj|j}|j}|jj |||j}t |_ |j r|S|||k||k@S(s Select contour levels to span the data. We need two more levels for filled contours than for line contours, because for the latter we need to specify the lower and upper boundary of each range. For example, a single contour boundary, say at z = 0, requires only one contour line, but two filled regions, and therefore three levels to provide boundaries for both regions. iN( RR"RR R t MaxNLocatortcreate_dummy_axisR1R2t set_boundsR'R3R(R tzRR1R2RT((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pyt_autolevDs        cC s|jrd}nd}t|_|jdkrt|dkrW|j|d}np|d}yFt|tkr|j||}nt j |j t j }Wnt d||fnX|jrt|dkrtdn||_ndS( sH Determine the contour levels and store in self.levels. tcontourftcontouriis*Last %s arg must give levels; see help(%s)is*Filled contours require at least 2 levels.N(RR$R3R)R"R(RJR8R6R Rtastypetfloat64R0R/(R RIRNtfnRTt level_arg((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pyt_contour_level_args_s$   cC st|j|_|jd krN|jjdt|jd|jdn|jd kr|jjt|jd|j dnt j |j|_t j |j|_ t j|j|_|jd krd|jd|jd|_ n|jd kr(d|jd|jd|_n|jrd |jd |jd|_|jdkrd |j |jd|jdtset_normRCRVR tscaledtset_climRTRUR$tclipt set_array(R ti0ti1((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pyRs"      cC s|j}t|j}|dkr>tjdfg|}ntj|s]|g|}nit|}t||krt t j |t|}||}nt||kr|| }ng|D]}|f^q}|S(Nslines.linewidth( RR(R)R"RRRKtiterableR+R6R R(R RtNlevRtnrepstw((sE/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/contour.pyRs     c C sE|j}t|j}|dkrdg|}|jrAtjd}|j|j d}x9t |jD]%\}}||krl|||s2