U
    KvfE                     @   s"   d dl ZddlmZ d	ddZdS )
    N   )utilsFTbothc           C         s"
  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 }t|D ]n}|dk	rz|| |krzqZ|	dk	r|| |	krqZ|| || f} | |krg || < ||  | qZ| |j }!|!j|!j }"}#t|}$d
\}%}&|r||j|j}'n||j|j}'||# }(|rL|&|% ||(  t|$ })n|&|% t|$ })|dk	rl|ntt	|  |rddd t}*|*dkr|)dt|*d   }+nd}+fddD },dddddddg}-|dkrdd D }t|*D ]v}.|. }/d||/ kr6|-|.t|-  ||/ d< d||/ krPd||/ d< d||/ kr|+dkrldnd||/ d< q|dkrdd D }t|*D ]P}.|. }/d||/ krd||/ d< d ||/ kr|+dkrd!nd"||/ d < q|r|dkr
|&|)d!  n|&}0n|%|)d!  }0t	 }1g }2|D ]ȉ |rH|r |d krV|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|D ]}7 |7f|krh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 t!| |7f D ]v\};}<d}=|
r|) d? |,||<  |+  }=| |< }>|dk	rVt"||< r|>||<  |>||<   }?}@n"|>||< d  |>||< d   }?}@|r$|j#|?|@g|0|= |0|= gd@fdA|'i|||<   n2|j#|0|= |0|= g|?|@gd@fdA|'i|||<   ||< }A|A|1krl|And}B|1$|A |r|j#|>g|0|= gfdB|'|BdC||A  n(|j#|0|= g|>gfdB|'|BdC||A  q\|r|0|)8 }0n|0|)7 }0qPq*|	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 |/g  |j)d1 *dD |j)d8 *dD |j)d; *dD |j)d> +dEd7|" f |0dd |j%-|2 |j.dddG |S )Ha-  
    Dot plotting (also known as forest and blobbogram).

    Produce a dotplot similar in style to those in Cleveland's
    "Visualizing Data" book ([1]_).  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 : str
        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 : bool
        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 horizontal is False).  If
        None (default), the order is lexical.
    striped : bool
        If True, every other line is enclosed in a shaded box.
    horizontal : bool
        If True (default), the lines are drawn horizontally, otherwise
        they are drawn vertically.
    show_names : str
        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 : callable
        The left/top margin names are passed through this function
        before drawing on the plot.
    fmt_right_name : callable
        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.

    References
    ----------
    .. [1] Cleveland, William S. (1993). "Visualizing Data". Hobart Press.
    .. [2] Jacoby, William G. (2006) "The Dot Plot: A Graphical Display
       for Labeled Quantitative Values." The Political Methodologist
       14(1): 6-14.

    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)
    r   Nc                 S   s   | d krd S t | S )N)npasarray)x r   A/tmp/pip-unpacked-wheel-2v6byqio/statsmodels/graphics/dotplots.py<lambda>       zdot_plot.<locals>.<lambda>g      ?FTr   g{Gz?)r   r   g      @g        c                    s   i | ]}|  |qS r   )index.0r   )style_codesr   r	   
<dictcomp>   s      zdot_plot.<locals>.<dictcomp>rgbykZpurpleZorangec                 S   s   i | ]
}|i qS r   r   r   r   r   r	   r     s      colormarkeroms
      c                 S   s   i | ]
}|i qS r   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?))\(?r&   )(\?r'   )r   r   r    Zclip_ongRQ?bottom)r   Z	lightgrey)r   r   )r   r   )leftr   grightZ	monospace)r"   r#   r   familytop)r*   r   g?r)      -r   None)Zlsr   labelnoneZaxes)Zscaleytight)Zscalexr2   )1Zmatplotlib.transforms
transformsr   Zcreate_mpl_axr   r   lenZarangezerossetZset_xmarginZset_ymarginlistsortrangeappendZget_window_extentZtransformedZdpi_scale_transinvertedwidthheightZblended_transform_factoryZ	transDataZ	transAxesfloatZ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lines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axr3   ZfigZasarray_or_noneZnpointZsection_title_spaceZnsectZdraw_section_titlesZnsect_titleZsection_space_totalZlines0Zlines1Z	lines_mapikyZbboxZawidthZaheightZnrowsr(   r,   ZtransZtitle_space_axesZdposnvalZstackdZstyle_codes_mapcolorsjZscposZlabeledZticksZy0txtmZjrowZk1usZ
left_labelZright_labelZjiZjpZyoptZlcbZucbslZsllr   )r$   r   r	   dot_plot   s   w



















  






 







  



rQ   )NNNNNNNNNFNFTr   NNNN)Znumpyr    r   rQ   r   r   r   r	   <module>   s$                            