B
    ZH                 @   s.   d dl Zd dlmZ ddlmZ d
dd	ZdS )    N)range   )utilsFTbothc       C         sL
  ddl m} t|\}}t| } dd }||}||}||}||}t| }|dkrft|}|dkrxt|}|dkrt|}d}tt	|}|dk	rtt	|}|dkrd}d}n*|dkrd}|}n|dk}|dkr|nd}|| }|
d	 |d	 |dkr&tt	|}|  n|}|	dkrJtt	|}|  n|	}i }xzt|D ]n}|dk	r||| |kr|q\|	dk	r|| |	krq\|| || f} | |krg || < ||  | q\W | |j }!|!j|!j }"}#t|}$d
\}%}&|r||j|j}'n||j|j}'||# }(|rP|&|% ||(  t|$ })n|&|% t|$ })|dk	rp|ntt	|  |rddd t}*|*dkr|)dt|*d   }+nd}+tfddD },dddddddg}-|dkrtdd D }xt|*D ]v}.|. }/d||/ krD|-|.t|-  ||/ d< d||/ kr^d||/ d< d||/ kr|+dkrzdnd||/ d< qW |dkrtdd D }x\t|*D ]P}.|. }/d||/ krd||/ d< d ||/ kr|+dkrd!nd"||/ d < qW |r(|dkr"|&|)d!  n|&}0n|%|)d!  }0t	 }1g }2x|D ]ԉ |rd|rڈ |d krr|0|)d!  n|0}3|jd
|3|3f|0d#|(  |0d#|(  fd$|jdd% |jd|0d&|(   d'd'|jd(}4|4d) |0|(8 }0nt fd*d+|D }5|j|0|)d!  d, |0|5d |)  |)d!  d, fd-d.d$|jddd/ |j|0|5d |) d!  d0 d'd1|jd(}4|4d) d}6x|D ]}7 |7f|krqp|r|j|0dd2 n|j|0dd2 |dk	r|7 |}8t|8d!kr|8d |8d  }9}:n
|7d }9}:n
|7d }9}:|dk	r
||9}9|dk	r||:}:|r|6d! dkr|rx|jd
|0|)d!  |0|)d!  f|0|)d!  |0|)d!  fd3|jdd% n,|j|0|)d!  |0|)d!  fd4d5d3|jdd% |6d7 }6|! d6kr|r|jd7|" |0|9d8d'|jd9d: n|j|0d7|# |9d'd;|jd9d: |! d<krb|:dk	rb|r@|jdd=|"  |0|:d>d'|jd9d: n"|j|0dd=|#  |:d'd1|jd9d: |2|0 xt"| |7f D ]v\};}<d}=|
r|) d? |,||<  |+  }=| |< }>|dk	rzt#||< r|>||<  |>||<   }?}@n"|>||< d  |>||< d   }?}@|rH|j$|?|@g|0|= |0|= gd@fdA|'i|||<   n2|j$|0|= |0|= g|?|@gd@fdA|'i|||<   ||< }A|A|1kr|And}B|1%|A |r|j$|>g|0|= gfdB|'|BdC||A  n(|j$|0|= g|>gfdB|'|BdC||A  qW |	r|0|)8 }0n|0|)7 }0qpW qFW |	r|j&'d1 |j('dD |)g  |j*d> +dD |j*d8 +dD |j*d; +dD |j*d1 ,dEd7|# f |-dd |j(.|2 |j/dddF n|j('d> |j&'dD |0g  |j*d1 +dD |j*d8 +dD |j*d; +dD |j*d> ,dEd7|" f |1dd |j&.|2 |j/dddG |S )Ha  
    Produce a dotplot similar in style to those in Cleveland's
    "Visualizing Data" book.  These are also known as "forest plots".

    Parameters
    ----------
    points : array_like
        The quantitative values to be plotted as markers.
    intervals : array_like
        The intervals to be plotted around the points.  The elements
        of `intervals` are either scalars or sequences of length 2.  A
        scalar indicates the half width of a symmetric interval.  A
        sequence of length 2 contains the left and right half-widths
        (respectively) of a nonsymmetric interval.  If None, no
        intervals are drawn.
    lines : array_like
        A grouping variable indicating which points/intervals are
        drawn on a common line.  If None, each point/interval appears
        on its own line.
    sections : array_like
        A grouping variable indicating which lines are grouped into
        sections.  If None, everything is drawn in a single section.
    styles : array_like
        A grouping label defining the plotting style of the markers
        and intervals.
    marker_props : dict
        A dictionary mapping style codes (the values in `styles`) to
        dictionaries defining key/value pairs to be passed as keyword
        arguments to `plot` when plotting markers.  Useful keyword
        arguments are "color", "marker", and "ms" (marker size).
    line_props : dict
        A dictionary mapping style codes (the values in `styles`) to
        dictionaries defining key/value pairs to be passed as keyword
        arguments to `plot` when plotting interval lines.  Useful
        keyword arguments are "color", "linestyle", "solid_capstyle",
        and "linewidth".
    split_names : string
        If not None, this is used to split the values of `lines` into
        substrings that are drawn in the left and right margins,
        respectively.  If None, the values of `lines` are drawn in the
        left margin.
    section_order : array_like
        The section labels in the order in which they appear in the
        dotplot.
    line_order : array_like
        The line labels in the order in which they appear in the
        dotplot.
    stacked : boolean
        If True, when multiple points or intervals are drawn on the
        same line, they are offset from each other.
    styles_order : array_like
        If stacked=True, this is the order in which the point styles
        on a given line are drawn from top to bottom (if horizontal
        is True) or from left to right (if horiontal is False).  If
        None (default), the order is lexical.
    striped : boolean
        If True, every other line is enclosed in a shaded box.
    horizontal : boolean
        If True (default), the lines are drawn horizontally, otherwise
        they are drawn vertically.
    show_names : string
        Determines whether labels (names) are shown in the left and/or
        right margins (top/bottom margins if `horizontal` is True).
        If `both`, labels are drawn in both margins, if 'left', labels
        are drawn in the left or top margin.  If `right`, labels are
        drawn in the right or bottom margin.
    fmt_left_name : function
        The left/top margin names are passed through this function
        before drawing on the plot.
    fmt_right_name : function
        The right/bottom marginnames are passed through this function
        before drawing on the plot.
    show_section_titles : bool or None
        If None, section titles are drawn only if there is more than
        one section.  If False/True, section titles are never/always
        drawn, respectively.
    ax : matplotlib.axes
        The axes on which the dotplot is drawn.  If None, a new axes
        is created.

    Returns
    -------
    fig : Figure
        The figure given by `ax.figure` or a new instance.

    Notes
    -----
    `points`, `intervals`, `lines`, `sections`, `styles` must all have
    the same length whenever present.

    Examples
    --------
    This is a simple dotplot with one point per line:
    >>> dot_plot(points=point_values)

    This dotplot has labels on the lines (if elements in
    `label_values` are repeated, the corresponding points appear on
    the same line):
    >>> dot_plot(points=point_values, lines=label_values)

    References
    ----------
      * Cleveland, William S. (1993). "Visualizing Data". Hobart
        Press.
      * Jacoby, William G. (2006) "The Dot Plot: A Graphical Display
        for Labeled Quantitative Values." The Political Methodologist
        14(1): 6-14.
    r   Nc             S   s   | d krd S t | S )N)npasarray)x r	   <lib/python3.7/site-packages/statsmodels/graphics/dotplots.py<lambda>   s    zdot_plot.<locals>.<lambda>g      ?FTr   g{Gz?)r   r   g      @g        c             3   s   | ]}|  |fV  qd S )N)index).0r   )style_codesr	   r
   	<genexpr>   s    zdot_plot.<locals>.<genexpr>rgbykZpurpleZorangec             s   s   | ]}|i fV  qd S )Nr	   )r   r   r	   r	   r
   r     s    colorZmarkeroZms
      c             s   s   | ]}|i fV  qd S )Nr	   )r   r   r	   r	   r
   r     s    ZgreyZ	linewidth      gffffff?Zdarkgrey)r   	transformzordergffffff?center)horizontalalignmentverticalalignmentr   Zboldc                s   g | ]}|d   kr|qS )r   r	   )r   r   )k0r	   r
   
<listcomp>@  s    zdot_plot.<locals>.<listcomp>g{Gz?)g)\(?g)\(?)g(\?g(\?)r   r   r   Zclip_ongRQ?bottom)r   Z	lightgrey)r   r   )r   r   )leftr   grightZ	monospace)r   r    r   Zfamilytop)r%   r   g?r$      -r   None)Zlsr   ZlabelZnoneZaxes)Zscaleytight)Zscalexr*   )2Zmatplotlib.transforms
transformsr   Zcreate_mpl_axr   r   lenZarangeZzerossetZset_xmarginZset_ymarginlistsortr   appendZget_window_extentZtransformedZdpi_scale_transinvertedwidthZheightZblended_transform_factoryZ	transDataZ	transAxesfloatdictZfill_betweentextZset_fontweightZaxhlineZaxvlinesplitlower	enumerateZisscalarZplotaddZxaxisZset_ticks_positionZyaxisZset_yticklabelsZspinesZ	set_colorZset_positionZset_ylimZ	set_ticksZautoscale_viewZset_xticklabelsZset_xlim)CZpointsZ	intervalslinesZsectionsZstylesZmarker_propsZ
line_propsZsplit_namesZsection_orderZ
line_orderZstackedZstyles_orderZstripedZ
horizontalZ
show_namesZfmt_left_nameZfmt_right_nameZshow_section_titlesZaxr+   ZfigZasarray_or_noneZnpointZsection_title_spaceZnsectZdraw_section_titlesZnsect_titleZsection_space_totalZlines0Zlines1Z	lines_mapiZkyZbboxZawidthZaheightZnrowsr#   r&   ZtransZtitle_space_axesZdposZnvalZstackdZstyle_codes_mapZcolorsjZscposZlabeledZticksZy0ZtxtmZjrowZk1usZ
left_labelZright_labelZjiZjpZyoZptZlcbZucbZslZsllr	   )r!   r   r
   dot_plot   s   t














 
 






















r@   )NNNNNNNNNFNFTr   NNNN)Znumpyr   Zstatsmodels.compatr    r   r@   r	   r	   r	   r
   <module>   s         