  # Python | Matplotlib.pyplot pincers

NumPy | Python Methods and Functions

One of the greatest benefits of visualization is that it gives us visual access to massive amounts of data in easily digestible visuals. Matplotlib consists of multiple plots such as line, bar, scatter, histogram, etc.

`Ticks` — these are the values ​​used to display specific points on the coordinate axis. It can be a number or a string. Whenever we plot the graph, the axes are adjusted and take the checkboxes by default. Matplotlib`s default ticks are usually sufficient in normal situations, but by no means optimal for every plot. Here we will see how to customize these checkboxes according to our needs.

Options :

Parameter Value Use
axis x, y, both Tells which axis to operate
reset True, False If True, set all parameters to default
direction in, out, inout Puts the ticks inside or outside or both
length Float Sets tick`s length
width Float Sets tick`s width
rotation Float Rotates ticks wrt the axis
colors Color Changes tick color
pad Float Distance in points between tick and label

Example # 1: default graph

 ` # import required modules ` ` import ` ` matplotlib.pyplot as plt `   ` # x and y axis values ​​` ` x ` ` = [ 5 , 10 , 15 , 20 , 25 , 30 , 35 , 40 , 45 , 50 ] `` y  = [ 1 , 4 , 3 , 2 , 7 , 6 , 9 , 8 , 10 , 5 ]   plt.plot (x, y) plt .xlabel ( `x` ) plt.ylabel ( `y` )    plt.show () `

Output: Example No. 2: Play with ticks

Suppose we don`t want to display tick values ​​or want our ticks to be tilted or want some other setting. We can do it this way.

Output: Example # 3: Changing tick values.

In the first example, the x-axis and y-axis were split into values ​​10 and 2, respectively. Let`s do this 5 and 1.

 ` # importing libraries ` ` import ` ` random ` ` import ` ` matplotlib.pyplot as plt `   ` fig ` ` = ` ` plt.figure () `   ` # function to get random values ​​for the graph ` ` def ` ` get_graphs (): ` ` xs ` ` = ` ` [] ` ` ys ` ` = ` ` [ ] ` ` for ` ` i in range ( 10 ): `` xs.append (i ) ys.append (random.randrange ( 10 ))   return xs, ys   #define subplots ax1 = fig.add_subplot ( 221 ) ax2 = fig.add_subplot ( 222 ) ax3 = fig.add_subplot ( 223 ) ax4 = fig.add_subplot ( 224 )    # hide the marker on the axis x, y = get_graphs () ax1.plot (x, y ) ax1.tick_params (axis = `both` , which = ` both` , length = 0  )   # You can also change the marker length # by setting (length = any float)   # hide check marks and markers x, y = get_graphs () ax2.plot (x, y) ax2.axes.get_xaxis (). set_visible ( False ) ax2.axes.get_yaxis (). set_visible ( False )   # hide values ​​and marker display x, y = get_graph s () ax3.plot (x, y) ax3.yaxis. set_major_formatter (plt.NullFormatter ()) ax3.xaxis.set_major_formatter (plt.NullFormatter ())   # Tilting checkmarks (usually required when # ticks are heavily populated ) x, y = get_graphs ( ) ax4.plot (x, y) ax4.tick_params (axis = `x` , rotation = 45 ) ax4.tick_params (axis = ` y` , rotation = - 45 )   plt.show () `

 ` # importing libraries ` ` import ` ` matplotlib.pyplot as plt ` ` import ` ` numpy as np `   ` # x and y axis values ​​` ` x ` ` = ` ` [` ` 5 ` `, ` ` 10 ` `, ` ` 15 ` `, ` ` 20 ` `, ` ` 25 ` `, ` ` 30 ` `, ` ` 35 ` `, ` ` 40 ` , ` 45 ` `, ` ` 50 ` `] ` ` y ` ` = ` ` [` ` 1 ` `, ` ` 4 ` `, ` ` 3 ` `, ` ` 2 ` `, ` ` 7 ` `, ` ` 6 ` `, ` ` 9 ` `, ` ` 8 ` `, ` ` 10 ` `, ` ` 5 ` `] `   ` plt.plot (x, y, ` ` `b` ` `) ` ` plt.xlabel (< / code> `x` ) `` plt.ylabel ( `y` )    # 0 is the initial value, 51 is the final value. # (the last value is not taken) and 5 is the difference The number of values ​​between two consecutive ticks plt.xticks (np. arange ( 0 , 51 , 5 )) plt.yticks (np.arange ( 0 , 11 , 1 )) plt.show () `

Output: The main difference from the first example:

plt.xticks (np.arange (0, 51, 5))
plt.yticks (np .arange (0, 11, 1))

Changing the values ​​in np.arange will change the tick range.

Help: Matplotlib pincers .