Python

# Matplotlib tick_params

Matplotlib is a Python visualizing package for two-dimensional array graphs. It is a visual analytics package based on NumPy Data frames and intended to operate on the SciPy platform. To adjust the layout of ticks, tick titles, and colors, we utilize matplotlib.pyplot.tick params(). We will go through Matplotlib tick_­­­params in this article.

## Use tick_params() Method and Specify the Direction of Ticks:

Here, we will discuss how to utilize the tick­_params() method by passing the “direction” parameter to specify the direction of ticks.

import matplotlib.pyplot as plt

import numpy as np

a = np.arange(10, 30, 0.4)

b = np.sin(a)

plt.plot(a, b)

plt.xlabel('X')

plt.ylabel('Y')

plt.tick_params(axis='both', direction='in')

plt.show() Before starting the code, we import two libraries. The matplotlib.pyplot library is responsible for the graphical functions and plotting functions. On the other hand, the Numpy library handles different numeric values. Next, we take a variable with the name “a”, which represents the data set of the x-axis. The variable “b” represents the data sets of the y-axis. We assign an array by using the built-in function of the NumPy library. Here, we pass some numeric values as the parameters of this function.

Further, we utilize the sin() function of the Numpy library, and we pass the “a” variable that is our x-axis to this function. Then, we stored the value of this function into variable “b”. This is how we create data sets of the y-axis. Now, we call a method plot() that fetches the Matplotlib library. And we provide data sets of x-axis and y-axis to this method.

The method aims to plot a graph with the help of data points from both axes. After drawing the graph, we set the label of the x-axis and y-axis by the use of the plt.label() function, respectively. In addition to this, we employ the tick_params() function responsible for changing the appearance of ticks and tick labels. Here, we apply this function on both axes and set the direction of the tick. In the end, we call the show() method used to display the graph. ## Adjust the Width of the Ticks:

Now, let’s see how to adjust the width of the ticks in the graph. For this purpose, we provided the “width” argument to the tick_params() functionality. In addition, we specify the labelsize of axes here:

import matplotlib.pyplot as plt

import numpy as np

a = np.arange(150)

b = np.sin(a)

plt.plot(a, b)

plt.xlabel('X')

plt.ylabel('Y')

plt.tick_params(axis='y', width=20, labelsize='xx-large')

plt.show() First of all, we include matplotlib.pyplot and Numpy libraries that are used to plot the graphs and accomplish various numeric values. Then, we initialize two variables with the names “a” and “b”, representing the data sets of both the x-axis and y-axis. Here, the arrange() method is utilized to create an array in which we pass numeric values.

On the other hand, we apply the sin() function of the NumPy library to the second variable. We provided the x-axis as its parameters. Further, we plot the values on the graph by using the plot() function. We also specify the labels of both axes by applying the plt.label() method. Additionally, we are going to call the tick_params() method. This function is applied on the y-axis. Hence, we set the “20” width of the ticks on the y-axis. And the label size of the y-axis is adjusted here. Then, we employ the plt.show() function to represent the graph. ## Change the Angle of Rotation for Matplotlib Ticks:

In this step, we will modify the rotation of the labels by passing the “labelrotation” parameter to the plt.ticks_params() method. We may adjust the labels of the axes at whatever angle that we select.

import matplotlib.pyplot as plt

import numpy as np

a = np.random.randint(500,size=(100))

b = np.random.randint(340, size=(100))

plt.scatter(a, b)

plt.xlabel('X')

plt.ylabel('Y')

plt.tick_params(axis='both', labelrotation=270)

plt.show() At the beginning of the code, we integrate libraries that are being used to perform required operations. Then, we take variables with the names “a” and “b”. We assign different values in the parameters of the random() function. These values are values of data sets of the x-axis and y-axis, as shown in the output.

Further, we call the plt.scatter() function, and it is responsible for drawing dots randomly on the x-y axes. We provided data sets of the x-axis and y-axis as the parameters of this function. Now, we employ the plt.label() function to label x-y plain for both axes. In addition, we utilize the tick_params() method, and we apply this function on both axes to manage the outlook of the ticks of the graph.

Here, we set the rotation of the labels of both axes. After all this, we have to show the graph. ## Specify the Label Color:

We can adjust the color of the axes’ label in Matplotlib. To accomplish this, we have provided the “labelcolor” parameter to the plt.ticks_params() method.

import matplotlib.pyplot as plt

import numpy as np

a = [5, 10, 15, 20, 25, 30]

b = [5, 8, 12.5, 21, 24, 31]

plt.plot(a, b)

plt.xlabel('X')

plt.ylabel('Y')

plt.tick_params(axis='x', labelcolor='r')

plt.tick_params(axis='y', labelcolor='y')

plt.show() Here, we introduce matplotlib.pyplot and Numpy libraries that allow us to create graphs and execute some numeric functions. Then, we initialize two arrays with the names “a” and “b”, respectively.

Meanwhile, we pass some values that intercept each other on the x-y axes. By using the plot() function, we draw a line on x-y axes, as shown in the graph. In the next line, we define the labels to the x-axis and y-axis by the use of the plt.label() function. In addition, we utilize the tick_params() method that changes the color of labels of the x-axis to red and labels of the y-axis to green. We call the plt.show() function to display the graph. ## Conclusion:

We talked about how to utilize the tick_params() method in Matplotlib. We can adjust the direction and width of the ticks by using this function. Further, we see how to set the label color and angle of the ticks’ rotation with the help of this function. We hope you found this article helpful. Check the other Linux Hint Articles for more tips and tutorials. 