Python Matplotlib tick_params + 29 examples

In this Python Matplotlib tutorial, we will discuss Matplotlib tick_params in python. Here we will cover different examples related to the tick_params using matplotlib. And we will also cover the following topics:

  • Matplotlib tick_params
  • Matplotlib tick_params font size
  • Matplotlib tick_params color
  • Matplotlib tick_params rotation
  • Matplotlib tick_params label color
  • Matplotlib tick_params colors
  • Matplotlib tick_params direction
  • Matplotlib tick_params length
  • Matplotlib tick_params width
  • Matplotlib tick_params pad
  • Matplotlib tick_params zorder
  • Matplotlib tick_params right
  • Matplotlib tick_params left
  • Matplotlib tick_params top
  • Matplotlib tick_params bottom
  • Matplotlib tick_params label bottom
  • Matplotlib tick_params label top
  • Matplotlib tick_params label left
  • Matplotlib tick_params label right
  • Matplotlib tick_params grid color
  • Matplotlib tick_params grid alpha
  • Matplotlib tick_params grid linewidth
  • Matplotlib tick_params grid linestyle
  • Matplotlib tick_params which
  • Matplotlib set_tick_params
  • Matplotlib subplot tick_params
  • Matplotlib colorbar tick_params
  • Matplotlib tick_params show all
  • Matplotlib tick_params tick_params customize ticks style

Matplotlib tick_params

In this section, we learn about tick_params in matplotlib in Python. The tick_params() method is used to change the appearance of ticks, tick labels, and gridlines.

The syntax is given below:

matplotlib.pyplot.tick_params(axis='both',**kwargs)

The following are the parameters used above:

ParameterValueDescription
axis‘x’, ‘y’, ‘both’Specify to which axis to apply the parameters.
which‘major’, ‘minor’, ‘both’The groups of ticks to which the parameters are applied.
resetboolWhether to reset the ticks or not.
direction‘in’, ‘out’, ‘inout’Specify the direction to puts ticks.
lengthfloatSpecify the tick length.
widthfloatSpecify the tick width.
colorcolorSpecify the tick color.
padfloatSpecify the distance between tick and label.
labelsizefloat or strSpecify the font size of tick label.
labelcolorcolorSpecify the color of the tick label.
colorscolorSpecify color for ticks and tick labels.
zorderfloatSpecify tick and label zorder.
bottom, top, left, rightboolSpecify whether to draw the respective ticks or not.
labelbottom, labeltop, labelleft, labelrightboolSpecify whether to draw the respective tick labels or not.
labelrotationfloatSpecify rotation of tick label.
grid_colorcolorSpecify the color of the gridline.
grid_alphafloatSpecify the transparency of gridlines.
grid_linewidthfloatSpecify the width of gridlines in points.
grid_linestylestrAny valid 2DLine line style.

Example: Default plot

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.arange(0, 20, 0.2)
y = np.sin(x)

# Plotting

plt.plot(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# Visualize

plt.show()
  • In the above example, we import matplotlib.pyplot and numpy package and define the data coordinates for plotting.
  • After that, a line between x and y is drawn using the plt.plot() method.
  • The plt.xlabel() and plt.ylabel() method is used to set the x-axis and y-axis label respectively.
  • The figure is displayed using the plt.show() function.
matplotlib tick params

Example: With tick_params() function

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.arange(0, 20, 0.2)
y = np.sin(x)

# Plotting

plt.plot(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_param

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

# Display

plt.show()

To change the appearance of the x ticks, the axis and direction parameters are passed to the tick_params() method in the above example. We reversed the orientation of the x-axis ticks towards inside.

matplotlib tick params example
plt.tick_params()

Read Python plot multiple lines using Matplotlib

Matplotlib tick_params font size

In this section, we’ll learn how to change the font size of the tick labels in Matplotlib tick_params. The labelsize argument is used to change the font size of the labels.

The following is the syntax for changing the font size of the label:

matplotlib.pyplot.tick_params(axis= , labelszie= )

Let’s see an example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.arange(0, 50, 0.8)
y = np.tan(x)

# Plotting

plt.plot(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params fontsize

plt.tick_params(axis='both', labelsize=15)

# Visualize

plt.show()

In the example, the labelsize parameter is passed to the tick_params() method to change the label’s font size.

matplotlib tick params fontsize
plt.tick_params(labelsize=)

Let’s have a look at one more example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.arange(0, 50, 0.8)
y = np.tan(x)

# Plotting

plt.plot(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params fontsize

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

# Visualize

plt.show()

To change the font size of the label, the labelsize and axis parameters are passed to the tick_params() method in the example. Here we pass xx-large and y-axis as a value.

matplotlib tick params label fontsize
xx-large y-axis label

Read Matplotlib plot a line

Matplotlib tick_params color

We’ll learn how to modify the color of the Matplotlib ticks in this section. We use the color argument to customize its color.

The following is the syntax to modify the color of the ticks:

matplotlib.pyplot.tick_params(axis= , color= )

Example #1:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = [0, 1, 2, 3, 4]
y = [3, 6, 9, 12, 15]

# Plotting

plt.plot(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params color

plt.tick_params(axis='both', color='red')

# Visualize

plt.show()

To change the color of both the ticks, we pass the axis and color parameter to the tick_params() method in the example above. We’ve changed the color to red.

matplotlib tick params color
“Modified color of ticks”

Example #2:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.arange(0, 50, 0.8)
y = np.sin(x)

# Plotting

plt.plot(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params color

plt.tick_params(axis='x', color='b')

# Visualize

plt.show()
  • We import the matplotlib.pyplot and numpy package in the example above.
  • The next step is to define data and create graphs.
  • plt.xlabel() and plt.ylabel() methods are used to create an x-axis and y-axis label.
  • To change the color of the x-axis tick, use the plt.tick_params() method. We pass axis and color as parameters and set their value as x and blue respectively.
matplotlib tick params modify color
“Modify x-axis tick color to blue”

Read Matplotlib change background color

Matplotlib tick_params rotation

We learn how to rotate the label in matplotlib. We can position labels at any angle we choose.

The following is the syntax to rotate labels:

matplotlib.pyplot.tick_params(axis= , labelrotation= )

Let’s see an example:

To rotate labels, we use the tick_params() function. Pass axis and labelrotation as parameters and set their values to y and 90 degrees, respectively.

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.random.randint(450,size=(80))
y = np.random.randint(260, size=(80))

# Plotting

plt.scatter(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params rotation

plt.tick_params(axis='y', labelrotation=90)

# Visualize

plt.show()
matplotlib tick params rotation
“Label Rotation 90 Degrees”

Read Matplotlib scatter marker

Matplotlib tick_params label color

We’ll learn how to change the tick label color in Matplotlib in Python. To change the color we pass labelcolor as a parameter.

Example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.random.randint(low=0, high=30, size=15)
y = np.random.randint(low=0, high=30, size=15)

# Plotting

plt.scatter(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params labelcolor

plt.tick_params(axis='both', labelcolor='red')

# Visualize

plt.show()
  • In the example above, we use the random.randint() function to define data and the scatter() function to plot a graph between them.
  • The tick_params() method with labelcolor parameter is then used to change the color of the labels to red.
matplotlib tick params label color
plt.tick_params()

Let’s look at one more example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = [0, 1, 2, 3, 4, 5]
y = [0, 3, 1.5, 4, 2.8, 5]

# Plotting


plt.plot(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params labelcolor

plt.tick_params(axis='x', labelcolor='g')
plt.tick_params(axis='y', labelcolor='m')

# Visualize

plt.show()

Here we modify the color of the x-axis and y-axis by using the tick_paramas() function. We set the green color for the x-axis label and the magenta color for the y-axis label.

matplotlib tick params modify label color
Labelcolor

Read Matplotlib dashed line

Matplotlib tick_params colors

We’ll learn how to change the color of ticks and tick labels in matplotlib.

The syntax is as follow:

matplotlib.pyplot.tick_params(axis = , colors = )

Example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.arange(0, 100, 0.8)
y = np.cos(x)

# Plotting

plt.plot(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params colors

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

# Visualize

plt.show()
  • We import matplotlib.pyplot and the numpy library in the example above.
  • After that, we use the arange() and cos() functions to define data.
  • To plot a graph between x and y data coordinates, use the plt.plot() method.
  • The xlabel() and ylabel() functions are used to add labels to the axes.
  • The plt.tick_params() function is used to change the color of ticks and tick labels.
matplotlib tick params colors
plt.tick_params(colors=None)

Read Matplotlib plot_date

Matplotlib tick_params direction

We’ll learn how to change the direction of Matplotlib ticks. We have to pass direction as a parameter to change the direction.

The syntax is given below:

matplotlib.pyplot.tick_params(axis= , direction= )

Let’s see an example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = [0, 1, 2, 3, 4, 5]
y = [0, 3, 1.5, 4, 2.8, 5]

# Plotting

plt.plot(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params direction

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

# Visualize

plt.show()

In the above example, we use the plt.tick_params() method to change the direction of the ticks we pass the direction as a parameter and set its value to inout.

matplotlib tick params direction
InOut Direction

Read Matplotlib log log plot

Matplotlib tick_params length

We’ll look at how to change the length of the ticks in Matplotlib. To change the length, the length parameter is passed to the tick_params() method.

The following is the syntax for changing the length of the ticks:

matplotlib.pyplot.tick_params(axis , length )

The axis is st by axis, while the length of tick is specified by length.

Example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.arange(0, 100, 0.8)
y = np.exp(x)

# Plotting

plt.plot(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params length

plt.tick_params(axis='x', length=15)

# Visualize

plt.show()
  • We import matplotlib.pyplot and the numpy library in the example above.
  • Following that, we use the arange() and exp() functions to define data.
  • To plot a graph, use the plt.plot() method.
  • To add labels to the x-axis and y-axis, use the plt.xlabel() and plt.ylabel() methods respectively.
  • plt.tick_params() method with argument length is used to change the length of ticks.
matplotlib tick params length
plt.tick_params(length)

Here we change the length of x-axis ticks.

Example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = [0, 1, 2, 3, 4]
y = [1, 2, 3, 4, 5]

# Plotting

plt.plot(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params length

plt.tick_params(axis='both', length=25)

# Visualize

plt.show()

Here we are going to change the length of both axes ticks. We set its value to 25.

matplotlib tick params modify length

Read Matplotlib subplots_adjust

Matplotlib tick_params width

We’ll look at how to change width of the Matplotlib ticks. To change the width, the width parameter is passed to the tick_params() method.

The following is the syntax for changing the width of the ticks:

matplotlib.pyplot.tick_params(axis , width)

The axis is st by axis, while the width of the tick is specified by width.

Example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.arange(100)
y = np.sin(x)

# Plotting

plt.plot(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params width

plt.tick_params(axis='x', width=15)

# Visualize

plt.show()
  • In the above example, we firstly import numpy and matplotlib.pyplot library.
  • Next we define, data using arange(), sin() method.
  • Then we plot data using the plot() method.
  • plt.tick_params() method with width argument is used to change the width of the ticks at the x-axis.
matplotlib tick params width
plt.tick_params(axis = ‘x’ , width = 15)

Read Matplotlib best fit line

Matplotlib tick_params pad

We’ll look at how to add space between ticks and tick labels in Python matplotlib in this section. To add space, the pad parameter is passed to the tick_params() method.

The following is the syntax:

matplotlib.pyplot.tick_params(axis , pad)

Let’s see an example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x=np.linspace(0,10,15)
y= np.cos(2 * np.pi * x)

# Plotting

plt.plot(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params pad

plt.tick_params(axis='x', pad=26)

# Visualize

plt.show()

In the above example, we use the plt.tick_params() method to add space between ticks and tick labels for that we have to pass pad as a parameter.

matplotlib tick params pad
Padding

Read Matplotlib subplot tutorial

Matplotlib tick_params zorder

The default drawing order for axes in patches, lines, text. The order is determined by the zorder attribute. We can change the order by setting the zorder.

Example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data


x = np.random.random(20)
y = np.random.random(20)

# Plotting

plt.plot(x, y, 'C3', zorder=1, lw=3)
plt.scatter(x, y, s=120, zorder=2)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params

plt.tick_params(axis='both', direction="inout", length=8, width=4, color="m")

# Visualize

plt.show()

Here we pass the zorder parameter to the plot() and scatter() method. And we pass axis, direction, length, width, color parameters to the tick_params() method.

matplotlib tick params zorder
plt.tick_params()

Read Matplotlib plot bar chart

Matplotlib tick_params right

We’ll learn how to put a tick to the right of the chart in Matplotlib. The tick will be added on the left and bottom axes by default. We can turn on and off the ticks independently in matplotlib.

The following is the syntax for adding ticks to the right:

matplotlib.pyplot.tick_params(right=True)

Let’s see an example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x=np.linspace(0,10,15)
y= np.cos(2 * np.pi * x)

# Plotting

plt.plot(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params right

plt.tick_params(right=True)

# Visualize

plt.show()
  • In the above example, we firstly import numpy and matplotlib.pyplot library.
  • Next we define, data using linspace(), cos(), and pi() methods.
  • Then we plot data using the plot() method.
  • plt.tick_params() method with the right argument is used to add ticks at the right side of the plot axes.
matplotlib tick params right
Ticks at Right Side Of the Plot

Read What is matplotlib inline

Matplotlib tick_params left

By default, ticks will be shown on the left and bottom axes of the plot. We’ll learn how to turn off the ticks on the left axis. By default value of the left is True.

The following is the syntax:

matplotlib.pyplot.tick_params(left=False)

Example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = [0, 1, 2, 3, 4]
y = [1, 2, 3, 4, 5]

# Plot Bar

plt.bar(x, y, color='g')

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params left

plt.tick_params(left=False)

# Visualize

plt.show()
  • We import matplotlib.pyplot and the numpy library in the example above.
  • Following that, we define data and plot bar chart using plt.bar() method.
  • To add labels to the x-axis and y-axis, use the plt.xlabel() and plt.ylabel() methods respectively.
  • plt.tick_params() method with argument bottom is used to turn off the ticks from left axes. We assigned it a false value.
matplotlib tick params left
bottom=False

Read Matplotlib save as pdf

Matplotlib tick_params top

We’ll learn how to put a tick to the top of the chart in Python matplotlib. The tick will be added on the left and bottom axes by default. We can turn on and off the ticks independently in matplotlib.

The following is the syntax for adding ticks to the top:

matplotlib.pyplot.tick_params(top=True)

Let’s see an example:

# Import

import matplotlib.pyplot as plt
 
# Define Data

x = [1, 2, 3, 4, 5]
y = [5, 10, 15, 20, 25]
 
# Scatter Plot

plt.scatter(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params top

plt.tick_params(top=True, length=15, axis='x')

# Display

plt.show()
  • We import matplotlib.pyplot and the numpy library in the example above.
  • Following that, we define data and plot scatter chart using plt.scatter() method.
  • To add labels to the x-axis and y-axis, we use the plt.xlabel() and plt.ylabel() methods respectively.
  • plt.tick_params() method with argument top is used to show ticks at top of the axes. We assigned it a true value.
  • We also pass axis and length parameter and set their value x and 15 respectively.
matplotlib tick params top
“Ticks On The Top Of Axis”

Read Matplotlib title font size

Matplotlib tick_params bottom

By default, ticks will be shown on the left and bottom axes of the plot in Matplotlib. We’ll learn how to turn off the ticks from the bottom axis. By default value of the bottom is True.

The following is the syntax:

matplotlib.pyplot.tick_params(bottom=False)

Example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = [0, 1, 2, 3, 4, 5]
y = [0, 3, 1.5, 4, 2.8, 5]

# Plot

plt.plot(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params bottom

plt.tick_params(bottom=False)

# Visualize

plt.show()

In the above example, we use the plt.tick_params() method to turn off ticks from the bottom axes of the plot and we pass the bottom as a parameter and assign them false values.

matplotlib tick params bottom
bottom=False

Read Matplotlib default figure size

Matplotlib tick_params label bottom

By default, tick labels will be shown on the left and bottom axes of the plot in Python matplotlib. We’ll learn how to turn off the ticks labels from the bottom axis. By default value of the bottom is True.

The following is the syntax:

matplotlib.pyplot.tick_params(labelbottom=False)

Example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.arange(0, 50, 0.8)
y = np.tan(x)

# Plot

plt.plot(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params labelbottom

plt.tick_params(labelbottom=False)

# Visualize

plt.show()
  • In the above example, we firstly import numpy and matplotlib.pyplot library.
  • Next we define, data using arange(), tan() methods.
  • Then we plot data using the plot() method.
  • plt.tick_params() method with the labelbottom argument is used to turn off labels from the bottom axes.
matplotlib tick params label bottom
labelbottom=False

Read Matplotlib savefig blank image

Matplotlib tick_params labeltop

We’ll learn how to put a tick label at the top of the chart in Matplotlib in Python. The labels will be added at the left and bottom axes by default. We can turn on and off the ticks independently in matplotlib.

The following is the syntax:

matplotlib.pyplot.tick_params(labeltop=True)

Example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.arange(0, 50, 0.8)
y = np.tan(x)

# Plotting

plt.plot(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params label

plt.tick_params(labeltop=True)

# Visualize

plt.show()
  • We import matplotlib.pyplot and the numpy library in the example above.
  • Following that, we define data and plot chart using plt.plot() method.
  • To add labels to the x-axis and y-axis, we use the plt.xlabel() and plt.ylabel() methods respectively.
  • plt.tick_params() method with argument labeltop is used to show ticklabels at top of the axes. We assigned it a true value.
matplotlib tick params label top
labeltop=True

Read Matplotlib save as png

Matplotlib tick_params label left

By default, tick labels will be shown on the left and bottom axes of the Matplotlib plot. We’ll learn how to turn off the ticks labels from the left axis. By default, the value of the left is True.

The following is the syntax:

matplotlib.pyplot.tick_params(labelleft=False)

Example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.arange(0, 20, 0.2)
y = np.sin(x)

# Plotting

plt.plot(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params label left

plt.tick_params(labelleft=False)

# Visualize

plt.show()

Here we use the plt.tick_params() method with the labelleft argument to turn off labels from the left axes.

matplotlib tick params label left
labelleft=False

Read Matplotlib bar chart labels

Matplotlib tick_params label right

We’ll learn how to put a tick label at the right axes of the chart in Python. The labels will be added at the left and bottom axes by default. We can turn on and off the ticks independently in matplotlib.

The following is the syntax:

matplotlib.pyplot.tick_params(labelright=True)

Let’s see an example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = [0, 1, 2, 3, 4, 5]
y = [1.5, 3, 5.3, 6, 10, 2]

# Plotting

plt.plot(x, y, '-o')

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params label right

plt.tick_params(labelright=True)

# Visualize

plt.show()

Here we use the plt.tick_params() method with the argument labelright to turn on the labels from the right axes.

matplotlib tick params label right
plt.tick_params()

Read Add text to plot matplotlib in Python

Matplotlib tick_params grid color

Here we are going to learn how we can change the grid_color by using the tick_params() method in matplotlib. So, firstly you must have to know how to draw grid lines in a chart or plot. The grid() method is used to draw grid lines.

The following is the syntax for the drawing grid:

matplotlib.pyplot.grid()

Now our main objective is to change the color of the grid line. By default color of the grid, is white. To change the color we pass the grid_color argument to the tick_params method.

The following is the syntax for changing grid color:

matplotlib.pyplot.tick_params(grid_color=None)

Example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = [0, 1, 2, 3, 4, 5]
y = [1.5, 3, 5.3, 6, 10, 2]

# Plotting

plt.plot(x, y, '-o')

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# Turn on grid

plt.grid()

# tick_params grid color

plt.tick_params(grid_color='red')

# Visualize

plt.show()
  • We import matplotlib.pyplot and the numpy library in the example above.
  • Following that, we define data and plot chart using plt.plot() method.
  • To add labels to the x-axis and y-axis, we use the plt.xlabel() and plt.ylabel() methods respectively.
  • To add grid in the plot, we use the plt.grid() method.
  • plt.tick_params() method with argument grid_color is used to change the color of the grid.
matplotlib tick params grid color
plt.grid()

Read Matplotlib plot error bars

Matplotlib tick_params grid alpha

Here we are going to learn how to adjust the transparency of the Matplotlib grid. By using the grid_alpha attribute we can change the transparency of the plot in matplotlib.

By default, alpha is 1. It ranges between 0 to 1. We can adjust the transparency by increasing or decreasing the value of alpha.

The following is the syntax:

matplotlib.pyplot.tick_params(grid_alpha=0.5)

Example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = [0, 1, 2, 3, 4, 5]
y = [1.5, 3, 6.3, 6, 10, 2]

# Plotting

plt.plot(x, y, '-o')

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# plot grid

plt.grid()

# tick_params alpha

plt.tick_params(axis='y', grid_color='orange', grid_alpha=0.8)

# Visualize

plt.show()

Above we use the tick_params() method with the axis, grid_color, and grid_alpha parameters to change the opacity of the grid lines at the y-axis.

matplotlib tick params grid alpha
plt.tick_param(grid_alpha)

Read Matplotlib remove tick labels

Matplotlib tick_params grid linewidth

Here we are going to learn how to modify the linewidth of the Matplotlib grid. By using the grid_linewidth attribute we can change the linewidth of the plot.

The following is the syntax:

matplotlib.pyplot.tick_params(grid_linewidth=None)

Example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = [0, 1, 2, 3, 4, 5]
y = [1.5, 3, 6.3, 6, 10, 2]

# Plotting

plt.plot(x, y, '-o')

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# plot grid

plt.grid()

# tick_params linewidth

plt.tick_params(axis='y', grid_color='orange', grid_alpha=0.8, grid_linewidth=9)

# Visualize

plt.show()
  • In the above example, we pass the axis as y to the tick_params() method. We also pass grid_color, grid_alpha arguments and set their values to orange and 0.8 respectively.
  • Our main is to modify the grid linewidth, so for that, we pass the grid_linewidth to the tick_params() method and set their value to 9.
matplotlib tick params grid line width
plt.tick_paramss(grid_linewidth=None)

Check out, Matplotlib rotate tick labels

Matplotlib tick_params grid linestyle

We’ll look at how to modify the gridline style in Python matplotlib. To change the linestyle, the grid_linestyle parameter is passed to the tick_params() method.

The following is the syntax:

matplotlib.pyplot.tick_params(grid_linestyle=None)

Let’s see an example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.arange(0, 20, 0.2)
y = np.sin(x)

# Plotting

plt.plot(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# plot grid

plt.grid()

# tick_params linestyle

plt.tick_params(axis='both', grid_color='red',grid_linewidth=2, grid_linestyle=':')

# Visualize

plt.show()
  • In the above example, we firstly import numpy and matplotlib.pyplot library.
  • Next we define, data using arange(), sin() methods.
  • Then we plot data using the plot() method.
  • plt.grid() method is used to add gridlines in the plot.
  • plt.tick_params() method with the grid_linestyle argument is used to change grid linestyle.
matplotlib tick params grid linestyle
linestyle=:

Read Matplotlib x-axis label

Matplotlib tick_params which

Sometimes we have both major and minor ticks and tick labels in the Matplotlib plot. At that time, we use the which parameter so we can specify that at which ticks and tick labels we want to apply changes.

The following is the syntax:

matplotlib.pyplot.tick_params(axis=None, which=None, ....)

Let’s see an example:

# Import Library

import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator, FormatStrFormatter

# Define Data

x = [0, 1, 2, 3, 4, 5]
y = [1.5, 3, 6.3, 6, 10, 2]

# Create figure and subplot


fig, ax = plt.subplots()

# Plotting

plt.plot(x, y, '-o')

# Minor ticks


ax.xaxis.set_minor_locator(AutoMinorLocator())
ax.yaxis.set_minor_locator(AutoMinorLocator())
ax.yaxis.set_minor_formatter(FormatStrFormatter(4))

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params

plt.tick_params(axis ='both', which ='major', 
               labelsize = 15, pad = 6, 
               colors ='g')
  
plt.tick_params(axis ='both', which ='minor',
               labelsize = 6, colors ='k')


# Visualize


plt.show()
  • In the above example, we import pyplot and ticker libraries from matplotlib.
  • Next, we define data and plot a graph between them by using the plot() method.
  • By using the set_minor_locator() and set_minor_formatter() methods we set the location and format of minor ticks respectively.
  • plt.tick_params() method with which parameter is used to change the appearance of the ticks and tick labels.
matplotlib tick params which
tick_params(which=minor)

Read Matplotlib multiple bar chart

Matplotlib set_tick_params

To set appearance parameters for ticks, ticklabels, and gridlines in the axis module of matplotlib, we use the set_tick_params() function.

The following is the syntax:

matplotlib.axis.Axis.set_tick_params(self, axis=None, ...)

Let’s see an example:

# Import Library


import matplotlib.pyplot as plt
import numpy as np

# Create figure and subplot

fig,ax = plt.subplots()

# Define Data

x = [0, 1, 2, 3, 4]
y = [1, 2, 3, 4, 5]

# Plotting

plt.plot(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# set_tick_params 

ax.yaxis.set_tick_params(rotation=25, labelsize=12, labelcolor = "r")

# Visualize

plt.show()

Here we use the set_tick_params() method with the y-axis and we pass rotation, labelsize, and labelcolor parameters to change the appearance of ticklabels.

matplotlib set tick params
set_tick_params()

Read Matplotlib scatter plot legend

Matplotlib subplot tick_params

Here we will discuss how we can change the appearance of the specific subplot in Python Matplotlib. For this, we have to use the tick_params() method.

Let’s see an example to understand the concept:

# Importing Libraries

import numpy as np
import matplotlib.pyplot as plt

# Define Data

x1= [0.2, 0.4, 0.6, 0.8, 1]
y1= [0.3, 0.6, 0.8, 0.9, 1.5]

x2= [2, 6, 7, 9, 10]
y2= [3, 4, 6, 9, 12]

x3= [5, 8, 12]
y3= [3, 6, 9]

x4= [7, 8, 15]
y4= [6, 12, 18]

fig, ax = plt.subplots(2, 2)

# Plot graph

ax[0, 0].plot(x1, y1)
ax[0, 1].plot(x2, y2)
ax[1, 0].plot(x3, y3)
ax[1, 1].plot(x4, y4)

# change appearance of specific subplot

ax[0,1].tick_params(axis='x', labelsize=10, colors='red')
ax[0,1].tick_params(axis='y', length=15, width= 5, color= 'orange' )

# Display Graph


fig.tight_layout()
plt.show()
  • In the above example, we plot multiple graphs in a figure area and we want to change the appearance of the specific subplot.
  • Here we use the tick_params() method to change the appearance of tick and ticklabels and we pass different parameters such as axis, labelsize, color, length, width, and color.
matplotlib subplot tick params
ax.tick_params()

Read Matplotlib 3D scatter

Matplotlib colorbar tick_params

In this section, we’ll learn how to change the appearance of colorbar using the tick_params() method. For this, firstly you have to know how we add colorbar to the plot in Python.

The syntax for this is given below:

matplotlib.pyplot.scatter(x, y, c=None, cmap=None) 
matplotlib.pyplot.colorbar().tick_params()

Let’s see an example:

# Import Libraries


import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.array([99,86,88,111,103,87,94,78,77,85,86,23,15])
y = np.array([20,50,200,500,1000,60,90,10,300,600,800,65,12])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])

# Define scatter() function

plt.scatter(x, y, c=colors, cmap= 'Dark2')

# colorbar tick_params


plt.colorbar().ax.tick_params(axis='both', labelsize=10, colors='red' )

# Display the graph

plt.show()
  • In the above example, we import the matplotlib.pyplot and numpy library.
  • Then we define data and use the plt.scatter() method with a camp parameter to plot the graph and to set the color map respectively.
  • plt.colorbar() method is used to show the color strip in the plot.
  • plt.tick_params() method is used to change the appearance of ticks and ticklabels.
matplotlib tick params colorbar
plt.colorbar()

Read Stacked Bar Chart Matplotlib

Matplotlib tick_params show all

We’ll learn how to add ticks and ticklabels to all the sides of the plot in Python matplotlib.

Let’s see an example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.arange(100)
y = np.sin(x)

# Plotting

plt.plot(x, y)

# tick_params show all

plt.tick_params(right=True, top=True, labelright=True, labeltop=True)

# Visualize

plt.show()
  • In the above example, we import matplotlib.pyplot and numpy library.
  • After that, we define data using arange() and sin() methods of numpy. And we also plot a graph between data points using the plot() method of matplotlib.
  • By default, ticks and ticklabels are shown on the bottom and left axes of the plot. So, we use the tick_params() method with parameters right, top, labelright, and labeltop to show ticks and tickabels on all four axes of the plot.
matplotlib tick params show all
plt.tick_params()

Read Matplotlib two y axes

Matplotlib tick_params customize ticks style

Sometimes, we want to change the appearance of the plot by customizing the default ticks style. Luckily in matplotlib, we have different parameters to adjust the length, width, and color of ticks.

Example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data


x = np.arange(0, 50, 0.8)
y = np.tan(x)

# Plotting


plt.plot(x, y)

# Add label

plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')

# tick_params


plt.tick_params(axis='y', direction="in", length=16, width=5, color="g")
plt.tick_params(axis='x', direction="in", length=20, width=8, color="m")

# Visualize

plt.show()

Here we use the tick_params() method with parameters length and width measured in points and color any valid color.

You may also like:

So, in this Python tutorial, we have discussed the “Matplotlib tick_params” and we have also covered some examples related to it. These are the following topics that we have discussed in this tutorial.

  • Matplotlib tick_params
  • Matplotlib tick_params font size
  • Matplotlib tick_params color
  • Matplotlib tick_params rotation
  • Matplotlib tick_params label color
  • Matplotlib tick_params colors
  • Matplotlib tick_params direction
  • Matplotlib tick_params length
  • Matplotlib tick_params width
  • Matplotlib tick_params pad
  • Matplotlib tick_params zorder
  • Matplotlib tick_params right
  • Matplotlib tick_params left
  • Matplotlib tick_params top
  • Matplotlib tick_params bottom
  • Matplotlib tick_params label bottom
  • Matplotlib tick_params label top
  • Matplotlib tick_params label left
  • Matplotlib tick_params label right
  • Matplotlib tick_params grid color
  • Matplotlib tick_params grid alpha
  • Matplotlib tick_params grid linewidth
  • Matplotlib tick_params grid linestyle
  • Matplotlib tick_params which
  • Matplotlib set_tick_params
  • Matplotlib subplot tick_params