What is the barbs plot in Matplotlib?
Overview
In Matplotlib, barbs are arrowheads with a dark projection at their head. They're used to show magnitude and direction while a barbs plot is used to plot barbs in the cartesian plane.
Matplotlib has a class named Axes, which helps in the coordination system and supports callbacks throughout the functions. In axes.barbs(), the axes class is used to generate a 2-D field of barbs.
Syntax
barbs([X, Y], U, V, [C], **kw)
Parameters
[X, Y]: These are the x and y coordinates of barbs locations.U: This is the barb direction along with the x component.V: This is the barb direction along with the y component.[C]: This is used to add colors to barbs.**kw: These are some additional argument values.
Return value
It returns an instance of barbs type.
Example
# import pyplot from matplotlib & numpy libraryimport matplotlib.pyplot as pltimport numpy as np# evenly spaced numbers between 10 and 30x = np.linspace(10, 30, 15)# It will create a figure & a set of subplotsfig, axis = plt.subplots()# creating barbs plotaxis.barbs(x**5, x**3, x * 3, x * 2, x * 4)# set title for above created graphaxis.set_title('Barbs Graph Example', fontsize = 16, fontweight ='bold')# save the current figureplt.savefig('output/graph.png')
Explanation
- Line 5: We use
np.linspace(10, 30, 15)to create an Ndarray where10shows the starting value,30shows the ending value, and15shows the number of samples. Therefore, it creates evenly spread numeric values for the plot. - Line 7: The
plt.subplots()method returns a figure and axis type instance of a frame to show the graph. We use it to describe the layout of the plot. - Line 9: We invoke the
axis.barbs()method to generate a barbs plot on random values. - Line 11: We rename the title of the above-created plot to
'Barbs Graph Example'withfontsize = 16andfontweight ='bold'. - Line 13: We save the figure in the output folder as a PNG file.