@@ -6725,16 +6725,19 @@ def matshow(self, Z, **kwargs):
6725
6725
integer = True ))
6726
6726
return im
6727
6727
6728
- def violinplot (self , dataset , positions = None , vert = True , widths = 0.5 , showmeans = False ,
6729
- showextrema = True , showmedians = False ):
6728
+ def violinplot (self , dataset , positions = None , vert = True , widths = 0.5 ,
6729
+ showmeans = False , showextrema = True , showmedians = False ,
6730
+ points = 100 ):
6730
6731
"""
6731
6732
Make a violin plot.
6732
6733
6733
6734
Call signature::
6734
6735
6735
- violinplot(dataset, positions=None)
6736
+ violinplot(dataset, positions=None, vert=True, widths=0.5,
6737
+ showmeans=False, showextrema=True, showmedians=False,
6738
+ points=100):
6736
6739
6737
- Make a violin plot for each column of *dataset* or each vector in
6740
+ Make a violin plot for each column of *dataset* or each vector in
6738
6741
sequence *dataset*. Each filled area extends to represent the
6739
6742
entire data range, with three lines at the mean, the minimum, and
6740
6743
the maximum.
@@ -6750,8 +6753,8 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5, showmeans=F
6750
6753
automatically set to match the positions.
6751
6754
6752
6755
vert : bool, default = True.
6753
- If true, creates vertical violin plot
6754
- Else , creates horizontal violin plot
6756
+ If true, creates a vertical violin plot.
6757
+ Otherwise , creates a horizontal violin plot.
6755
6758
6756
6759
widths : array-like, default = 0.5
6757
6760
Either a scalar or a vector that sets the maximal width of
@@ -6767,14 +6770,18 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5, showmeans=F
6767
6770
showmedians : bool, default = False
6768
6771
If true, will toggle rendering of the medians.
6769
6772
6773
+ points : scalar, default = 100
6774
+ Defines the number of points to evaluate each of the gaussian
6775
+ kernel density estimations at.
6776
+
6770
6777
Returns
6771
6778
-------
6772
6779
6773
6780
A dictionary mapping each component of the violinplot to a list of the
6774
6781
corresponding collection instances created. The dictionary has
6775
6782
the following keys:
6776
6783
6777
- - bodies: A list of the
6784
+ - bodies: A list of the
6778
6785
:class:`matplotlib.collections.PolyCollection` instances
6779
6786
containing the filled area of each violin.
6780
6787
- means: A :class:`matplotlib.collections.LineCollection` instance
@@ -6786,9 +6793,9 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5, showmeans=F
6786
6793
created to identify the top of each violin's distribution.
6787
6794
- bars: A :class:`matplotlib.collections.LineCollection` instance
6788
6795
created to identify the centers of each violin's distribution.
6789
- - medians: A :class:`matplotlib.collections.LineCollection` instance
6790
- created to identify the median values of each of the violin's
6791
- distribution.
6796
+ - medians: A :class:`matplotlib.collections.LineCollection`
6797
+ instance created to identify the median values of each of the
6798
+ violin's distribution.
6792
6799
6793
6800
"""
6794
6801
@@ -6799,15 +6806,20 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5, showmeans=F
6799
6806
medians = []
6800
6807
6801
6808
# Collections to be returned
6802
- bodies = []
6803
- cmeans = None
6804
- cmaxes = None
6805
- cmins = None
6806
- cbars = None
6807
- cmedians = None
6809
+ artists = {
6810
+ 'bodies' : [],
6811
+ 'cmeans' : None ,
6812
+ 'cmaxes' : None ,
6813
+ 'cmins' : None ,
6814
+ 'cbars' : None ,
6815
+ 'cmedians' : None
6816
+ }
6817
+
6818
+ datashape_message = ("List of violinplot statistics and `{0}` "
6819
+ "values must have the same length" )
6808
6820
6809
6821
# Validate positions
6810
- if positions == None :
6822
+ if positions is None :
6811
6823
positions = range (1 , len (dataset ) + 1 )
6812
6824
elif len (positions ) != len (dataset ):
6813
6825
raise ValueError (datashape_message .format ("positions" ))
@@ -6825,91 +6837,64 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5, showmeans=F
6825
6837
# Check hold status
6826
6838
if not self ._hold :
6827
6839
self .cla ()
6828
- holdStatus = self ._hold
6840
+ hold_status = self ._hold
6841
+
6842
+ # Check whether we are rendering vertically or horizontally
6843
+ if vert :
6844
+ fill = self .fill_betweenx
6845
+ rlines = self .hlines
6846
+ blines = self .vlines
6847
+ else :
6848
+ fill = self .fill_between
6849
+ rlines = self .vlines
6850
+ blines = self .hlines
6829
6851
6830
6852
# Render violins
6831
- for d , p , w in zip (dataset ,positions ,widths ):
6853
+ for data , pos , width in zip (dataset , positions , widths ):
6832
6854
# Calculate the kernel density
6833
- kde = mlab .ksdensity (d )
6834
- m = kde ['xmin' ]
6835
- M = kde ['xmax' ]
6855
+ kde = mlab .ksdensity (data )
6856
+ min_val = kde ['xmin' ]
6857
+ max_val = kde ['xmax' ]
6836
6858
mean = kde ['mean' ]
6837
6859
median = kde ['median' ]
6838
- v = kde ['result' ]
6839
- coords = np .arange (m , M ,( M - m ) / 100. )
6860
+ vals = kde ['result' ]
6861
+ coords = np .arange (min_val , max_val , ( max_val - min_val ) / points )
6840
6862
6841
6863
# Since each data point p is plotted from v-p to v+p,
6842
6864
# we need to scale it by an additional 0.5 factor so that we get
6843
6865
# correct width in the end.
6844
- v = 0.5 * w * v / v .max ()
6845
-
6846
- # create vertical violin plot
6847
- if vert :
6848
- bodies += [self .fill_betweenx (coords ,
6849
- - v + p ,
6850
- v + p ,
6851
- facecolor = 'y' ,
6852
- alpha = 0.3 )]
6853
- # create horizontal violin plot
6854
- else :
6855
- bodies += [self .fill_between (coords ,
6856
- - v + p ,
6857
- v + p ,
6858
- facecolor = 'y' ,
6859
- alpha = 0.3 )]
6866
+ vals = 0.5 * width * vals / vals .max ()
6867
+
6868
+ # create the violin bodies
6869
+ artists ['bodies' ] += [fill (coords ,
6870
+ - vals + pos ,
6871
+ vals + pos ,
6872
+ facecolor = 'y' ,
6873
+ alpha = 0.3 )]
6860
6874
6861
6875
means .append (mean )
6862
- mins .append (m )
6863
- maxes .append (M )
6876
+ mins .append (min_val )
6877
+ maxes .append (max_val )
6864
6878
medians .append (median )
6865
6879
6866
- # respective means, extrema median on vertical violin plot
6867
- if vert :
6868
- # Render means
6869
- if showmeans :
6870
- cmeans = self .hlines (means , pmins , pmaxes , colors = 'r' )
6871
-
6872
- # Render extrema
6873
- if showextrema :
6874
- cmaxes = self .hlines (maxes , pmins , pmaxes , colors = 'r' )
6875
- cmins = self .hlines (mins , pmins , pmaxes , colors = 'r' )
6876
- cbars = self .vlines (positions , mins , maxes , colors = 'r' )
6877
-
6878
- # Render medians
6879
- if showmedians :
6880
- cmedians = self .hlines (medians , pmins , pmaxes , colors = 'r' )
6881
-
6882
- # respective means, extrema median on horizontal violin plot
6883
- else :
6884
- # Render means
6885
- if showmeans :
6886
- cmeans = self .vlines (means , pmins , pmaxes , colors = 'r' )
6887
-
6888
- # Render extrema
6889
- if showextrema :
6890
- cmaxes = self .vlines (maxes , pmins , pmaxes , colors = 'r' )
6891
- cmins = self .vlines (mins , pmins , pmaxes , colors = 'r' )
6892
- cbars = self .hlines (positions , mins , maxes , colors = 'r' )
6893
-
6894
- # Render medians
6895
- if showmedians :
6896
- cmedians = self .vlines (medians , pmins , pmaxes , colors = 'r' )
6897
-
6898
-
6880
+ # Render means
6881
+ if showmeans :
6882
+ artists ['cmeans' ] = rlines (means , pmins , pmaxes , colors = 'r' )
6899
6883
6884
+ # Render extrema
6885
+ if showextrema :
6886
+ artists ['cmaxes' ] = rlines (maxes , pmins , pmaxes , colors = 'r' )
6887
+ artists ['cmins' ] = rlines (mins , pmins , pmaxes , colors = 'r' )
6888
+ artists ['cbars' ] = blines (positions , mins , maxes , colors = 'r' )
6900
6889
6890
+ # Render medians
6891
+ if showmedians :
6892
+ artists ['cmedians' ] = rlines (medians , pmins , pmaxes , colors = 'r' )
6901
6893
6902
6894
# Reset hold
6903
- self .hold (holdStatus )
6895
+ self .hold (hold_status )
6904
6896
6905
- return {
6906
- 'bodies' : bodies ,
6907
- 'means' : cmeans ,
6908
- 'mins' : cmins ,
6909
- 'maxes' : cmaxes ,
6910
- 'bars' : cbars ,
6911
- 'medians' : cmedians
6912
- }
6897
+ return artists
6913
6898
6914
6899
6915
6900
def tricontour (self , * args , ** kwargs ):
0 commit comments