Violinplot in Python using axes class of Matplotlib
Last Updated :
21 Apr, 2020
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library.
The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
#Sample Code
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
ax.plot([ 1 , 2 , 3 ])
ax.set_title( 'matplotlib.axes.Axes function' )
fig.canvas.draw()
plt.show()
|
Output:
Violinplot using Axes Class
The Axes.violinplot() function in axes module of matplotlib library is used to make a violin plot for each column of dataset or each vector in sequence dataset.
Syntax:
Axes.violinplot(self, dataset, positions=None, vert=True, widths=0.5, showmeans=False, showextrema=True, showmedians=False, points=100, bw_method=None, *, data=None)
Parameters: This method accept the following parameters that are described below:
- dataset: This parameter is a sequence of data.
- positions : This parameter is used to sets the positions of the violins.
- vert: This parameter is an optional parameter and contain boolean value. It makes the vertical violin plot if true.Otherwise horizontal.
- widths: This parameter is used to sets the width of each violin either with a scalar or a sequence.
- showmeans : This parameter contain boolean value. It is used to toggle rendering of the means.
- showextrema : This parameter contain boolean value. It is used to toggle rendering of the extrema.
- showmedians : This parameter contain boolean value. It is used to toggle rendering of the medians.
- points : This parameter is used to defines the number of points to evaluate each of the gaussian kernel density estimations at.
Returns: This returns the following:
- result :This returns the dictionary which maps each component of the violinplot to a list of the matplotlib.collections instances.
Below examples illustrate the matplotlib.axes.Axes.violinplot() function in matplotlib.axes:
Example-1:
import matplotlib.pyplot as plt
import numpy as np
np.random.seed( 10 * * 7 )
data = np.random.normal( 0 , 5 , 100 )
fig, ax1 = plt.subplots()
val = ax1.violinplot(data)
ax1.set_title( 'matplotlib.axes.Axes.violinplot() Example' )
plt.show()
|
Output:
Example-2:
import matplotlib.pyplot as plt
import numpy as np
np.random.seed( 10 * * 7 )
data = [ sorted (np.random.normal( 0 , std, 100 )) for std in range ( 1 , 5 )]
fig, ax1 = plt.subplots()
val = ax1.violinplot(data)
ax1.set_ylabel( 'Result' )
ax1.set_xlabel( 'Domain Name' )
for i in val[ 'bodies' ]:
i.set_facecolor( 'green' )
i.set_alpha( 1 )
ax1.set_title( 'matplotlib.axes.Axes.violinplot() Example' )
plt.show()
|
Output:
Share your thoughts in the comments
Please Login to comment...