Violinplot in Python using axes class of Matplotlib

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Implementation of matplotlib function
      
import matplotlib.pyplot as plt
import numpy as np
    
# make an agg figure
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
ax.set_title('matplotlib.axes.Axes function')
fig.canvas.draw()
plt.show()

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
  
# create test data
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()

chevron_right


Output:

Example-2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
  
# create test data
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()

chevron_right


Output:




My Personal Notes arrow_drop_up

Small things always make you to think big

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.