# Matplotlib set_xticks – Detailed tutorial

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

• Matplotlib set_xticks
• Matplotlib set_xticks invisible
• Matplotlib set_xticks rotation
• Matplotlib set_xticks fontsize
• Matplotlib set_xtciks labels
• Matplotlib set_xticks vertical
• Matplotlib set_xticks direction
• Matplotlib set_xticks length
• Matplotlib set_xticks color
• Matplotlib barplot set_xticks
• Matplotlib histogram set_xticks
• Matplotlib colorbar set_xticks
• Matplotlib set_xticks subplot
• Matplotlib set_xticks log scale
• Matplotlib set ticks right
• Matplotlib set ticks top
• Matplotlib set ticks every
• Matplotlib set_xticks range
• Matplotlib set minor ticks interval
• Matplotlib set xticks as text
• Matplotlib set theta ticks

## Matplotlib set_xticks

In this section, we will learn about the set_xticks() function in the axes module of matplotlib in Python. The set_xticks() function is used to set the x ticks location.

The syntax is given below:

matplotlib.axes.Axes.set_xticks(ticks, labels=None, *, minor=False, **kwargs) 

The following are the parameters:

Let’s see an example:

# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Create figure and subplots

fig, ax = plt.subplots()

# Define Data Coordinates

x = np.linspace(0, 20 , 100)
y = np.sin(x)

# Plot

plt.plot(x, y)

# Set ticks

ax.set_xticks(np.arange(0, len(x)+1, 5))

fig.suptitle('set_xticks Example', fontweight ="bold")

# Display

plt.show()

Explanation:

• Import important libraries such as numpy, matplotlib.pyplot.
• After this, we create a subplot by using the subplots () method.
• To define data coordinates, we use linespace() and sin() methods of numpy.
• To set the x ticks, use set_xticks().
• To add a title to the figure, use suptitle() method.
• To visualize the plot on the user’s screen, use the show() method.

## Matplotlib set_xtciks invisible

Here we’ll learn to hide the ticks from the x-axis. For this, we have to pass the empty list to the set_xticks() method.

# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Create figure and subplots

fig, ax = plt.subplots()

# Define Data Coordinates

x = np.linspace(0, 20 , 100)
y = np.tan(x)

# Plot

plt.plot(x, y)

# Set ticks invisible

ax.set_xticks([])

# Display

plt.show()

Here we use the set_xticks() method to set the x-axis ticks location. To make ticks invisible we pass an empty string to the method.

Output:

## Matplotlib set_xticks rotation

We’ll change the rotation of ticks at the x-axis. To change the rotation we pass the rotation parameter to the plt.xticks() method.

Let’s see an example:

# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Define Data Coordinates

x = np.linspace(0, 20 , 100)
y = np.cos(x)

# Plot

plt.plot(x, y)

# Set ticks Rotation

plt.xticks(rotation=45)

# Display

plt.show()
• Here we import matplotlib.pyplot and numpy library.
• Next, we define data coordinates using numpy.
• To plot the line chart, use the plot() method.
• To rotate the ticks at the x-axis, use the plt.xticks() method and pass the rotation argument to it.

## Matplotlib set_xticks fontsize

Here we’ll see an example to change the fontsize of ticks at the x-axis. to change the fontsize we pass the fontsize argument to the plt.xticks() function.

Example:

# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Define Data Coordinates

x = np.linspace(0, 20 , 100)
y = np.exp(x)

# Plot

plt.plot(x, y)

# Set ticks Fontsize

plt.xticks(fontsize=13)

# Display

plt.show()

Output:

## Matplotlib set_xtciks labels

Here we’ll learn to set the location of ticks and also to add labels at the x-axis in matplotlib.

Let’s see an example:

# Import Library

import matplotlib.pyplot as plt

# Define Data Coordinates

x = [0, 1, 2, 3, 4, 5]
y = [1, 2.5, 4, 3.3, 10, 8]

# Plot

plt.plot(x, y)

# Set ticks Labels

plt.xticks([0, 1, 2, 3, 4, 5],['Term-1','Term-2','Term-3','Term-4', 'Term-5', 'Term-6'])

# Display

plt.show()

In the above example, we use the plt.xticks() method to add labels and ticks at x-axis manually.

## Matplotlib set_xticks vertical

Here we’ll learn to rotate x-axis ticks vertically in Matplotlib.

Example:

# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Define Data Coordinates

x = np.linspace(0, 200 , 100)
y = np.sin(x)

# Plot

plt.plot(x, y)

# Set ticks Rotation Vertical

plt.xticks(rotation='vertical')

# Display

plt.show()

In the above example, we use the plt.xticks() function with rotation argument to rotate the x ticks vertically.

## Matplotlib set_xticks direction

Here we’ll see an example where we change the direction of x-axis ticks in Python matplotlib.

Example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data Coordinates

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

# Plot

plt.plot(x)

# Set ticks direction

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

# Display

plt.show()

Explanation:

• Here we define data coordinates using random.randint() method of numpy.
• To plot the line graph, use plot() method.
• To adjust the tick behaviour, we use tick_params() function with the direction argument. We specify it to in to get placement of ticks inwards.

## Matplotlib set_xticks length

Here we’ll learn to change the length of ticks at the x-axis in Matplotlib python. To change the length of ticks pass length argument to the tick_params() method.

Let’s see an example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data Coordinates

x = np.random.randint(500,size=(20))

# Plot

plt.plot(x,color='darkblue')

# Tick length

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

# Display

plt.show()

## Matplotlib set ticks width

Here we’ll learn to change the width of ticks at the x-axis in Matplotlib. To change the width pass width argument to the tick_params() method.

Let’s see an example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data Coordinates

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

# Plot

plt.plot(x,y,color='lightgreen')

# Tick Width

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

# Display

plt.show()

## Matplotlib set_xticks color

We’ll learn to change the color of ticks located at the x-axis in Python matplotlib.

Example:

# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Define Data Coordinates

x = np.linspace(0, 50 , 100)
y = np.tan(x)

# Plot

plt.plot(x, y)

# Set ticks color

plt.tick_params(axis='x', color='red', length=12, width=10)

# Display

plt.show()

In the above example, we use the tick_params() function with axis, color, length, and width argument and set their value to x, red, 12, and 10 respectively.

## Matplotlib barplot set_xticks

We” learn to set locations of x ticks in barplot. To set the location, we use the set_xticks method of the axes module in matplotlib.

Example:

# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Create figure and subplots

fig, ax = plt.subplots()

# Define Data Coordinates

x = [1, 2, 3, 4, 6]
y = [10, 20, 25, 15, 30]

# Plot

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

# Set ticks

ax.set_xticks([0, 2, 4, 6, 8])

# Display

plt.show()
• In the above example, we define the data coordinates x and y to draw a bar chart using the bar() method.
• To set locations of the x ticks, use the set_xticks() method.

The output after setting the ticks:

## Matplotlib histogram set_xticks

We’ll learn to set the x ticks according to our choice. To change the default x ticks, use set_xticks() function in Matplotlib.

The steps to set the x-ticks in the histogram plot are as fellow:

• Import important libraries, such as matplotlib.pyplot, and numpy.
• To define data coordinates x and y use numpy random.randn() function.
• To plot the histogram chart between x and y, use the plt.hist() function.
• To set the edge colors for each of the bars in the histogram, use the edgecolor argument in the hist() method.
• To set the x ticks, use the set_xtick() method and we use the range() method of numpy to set the location of ticks.
• To visualize the user’s plot, use the plt.show() method.

## Matplotlib colorbar set_xticks

Here we’ll learn to set x ticks at the color bar by using the set_ticks() function in matplotlib.

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])
y = np.array([20,50,200,500,1000,60,75])
colors = np.array([0, 10, 20, 30, 40, 45, 50])

# Define scatter() function

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

# Colorbar

cbar = plt.colorbar()

# Set ticks

cbar.set_ticks(range(5, 30, 10))

# Display the graph

plt.show()
• In the above example, we import the matplotlib.pyplot and numpy library.
• Then we define the x, y and color data points in the form of an array.
• plt.scatter() method is used to draw markers for each data point and we pass the parameter ‘cmap’ to set the color map.
• plt.colorbar() method is used to show the color strip in the plot.
• Then we use the set_ticks() function to set the ticks at x-axis.
• Then we finally use the method plt.show() to display the plotted graph.

## Matplotlib set_xtciks subplot

In this section, we’ll see an example where we use the Matplotlib set_xticks() function in a subplot.

Example:

# Importing Libraries

import numpy as np
import matplotlib.pyplot as plt

# Create subplot

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

# 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]

# 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)

# Set x-ticks

ax[0, 0].set_xticks([])
ax[0, 1].set_xticks([0, 1, 2, 3])
ax[1, 0].set_xticks(range(-5, 11))

# Auto layout

fig.tight_layout()

# Display Graph

plt.show()
• In the above example, we use the set_xticks() function in subplot 1 to invisible the x ticks.
• In subplot 2, we use the set_xticks() function to set x ticks manually.
• And In subplot 3, we use the set_xticks() function to set x ticks by using the range() method of numpy.

## Matplotlib set_xticks log scale

Here we’ll create a plot with a log scale at the x-axis and also set the x ticks by using the set_xticks() function.

The following steps are used:

• To create a subplot, use plt.subplots() function.
• Define x and y data coordinates.
• To plot the lines, use plt.plot() method.
• To set log scale at x-axis, use set_xscale() method.
• To set the ticks at x-axis, use set_xticks() method.
• To display the plot, use plt.show() method.

Example:

# Importing Libraries

import numpy as np
import matplotlib.pyplot as plt

# Create subplot

fig, ax = plt.subplots()

# Define Data Coordinates

x = [50, 500, 5000, 50000]
y = [0, 1, 2, 3]

# Plot

plt.plot(x, y)

# Set log scale

ax.set_xscale('log')

# Set x ticks

ax.set_xticks([10, 100, 1000, 10000, 100000, 1000000,
10000000])

# Display

plt.show()

## Matplotlib set ticks right

Here we’ll learn to set ticks on the right side of the plot in matplotlib. To set the ticks, use the tick_params() method with the right argument and set its value to True.

The following is the syntax:

matplotlib.plot.tick_params(right=True)

Example:

# Importing Libraries

import numpy as np
import matplotlib.pyplot as plt

# Define Data Coordinates

x = np.arange(0, 100, 10)
y = np.sin(x)

# Plot

plt.plot(x, y)

# Right

plt.tick_params(right=True)

# Display

plt.show()

## Matplotlib set ticks top

Here we’ll learn to set ticks at top of the plot in Matplotlib. To set the ticks, use the tick_params() method with the top argument and set its value to True.

The following is the syntax:

matplotlib.plot.tick_params(top=True)

Example:

# Importing Libraries

import numpy as np
import matplotlib.pyplot as plt

# Define Data Coordinates

x = np.random.rand(100)

# Plot

plt.plot(x)

# Tick at top

plt.tick_params(top=True)

# Display

plt.show()

## Matplotlib set ticks every

Here we’ll learn to set ticks at every side of the plot in matplotlib.

Example:

# Importing Libraries

import numpy as np
import matplotlib.pyplot as plt

# Define Data Coordinates

x = np.random.rand(500)

# Plot

plt.plot(x)

# Set ticks everywhere

plt.tick_params(top=True, right=True, labelleft=False,
labelbottom=False)

# Display

plt.show()
• In the above example, we use the tick_params() method set the ticks at every side of the plot.
• To turn on the ticks at right and top of the plot, use the tick_params() method with top and right argument and set its bool value to True.
• To hide the labels from bottom and left side of the plot, use the tick_params() method with labelbottom, and labeltop argument and set its bool value to False.

## Matplotlib set_xticks range

Here we set the location of x ticks by using the range() method in matplotlib.

Example:

# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Create subplot

fig, ax = plt.subplots()

# Define Data Coordinates

y = [0, 1, 2, 3, 5]
x = [0, 5, 8, 9, 15]

# Ticks using arange method

x_ticks = np.arange(0, 25, 5)
ax.set_xticks(x_ticks)

# Plot

plt.plot(x, y)

# Display

plt.show()

Explanation:

• In the above example, to create a ticks at the x-axis, we use arange() method of numpy.
• In arange() method we define starting value of the ticks, non-inclusive ending value and a step which specify space between the ticks.
• To set the location of x ticks, use set_xticks() method of axes module.

## Matplotlib set minor ticks interval

Here we’ll see an example, where we use the matplotlib set_xticks() method to set minor ticks bypassing minor argument to the method and setting its bool value to True.

Example:

# Import Libraries

import numpy as np
import matplotlib.pyplot as plt

# Create subplot

fig, ax = plt.subplots()

# Set minor ticks

minor_ticks = np.arange(0, 50, 2)
ax.set_xticks(minor_ticks, minor=True)

# Display

plt.show()

Here we’ll learn to add space between ticks and tickslabel at the x-axis in matplotlib. For this, we use the tick_parmas() method with pad argument.

Let’s see an example:

# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Define Data Coordinates

x = np.linspace(0, 50 , 100)
y = np.cos(x)

# Plot

plt.plot(x, y)

# Display

plt.show()

## Matplotlib set xticks as text

Here we’ll learn to set x-ticks as text. For this, use xticks() function with ticks locations and ticks labels in matplotlib.

Example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data Coordinates

x = [0, 1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10, 12]

# Plot

plt.plot(x, y)

# Set ticks as text

plt.xticks([0, 1, 2, 3, 4, 5],['Value-1','Value-2','Value-3','Value-4', 'Value-5', 'Value-6'])

# Display

plt.show()
• Firstly, we import the matplotlib.pyplot, and numpy library.
• Next, we define data coordinates and plotline chart using the plot() method.
• To set x ticks as text, use plt.xticks() method.

## Matplotlib set theta ticks

Here we’ll see an example where we set x ticks as theta values in Python matplotlib. We use the xticks() method and pass ticks location and tick labels as theta.

Example:

# Import Library

import matplotlib.pyplot as plt

# Define Data Coordinates

x = [0, 1, 2, 3, 4, 5]
y = [5.8, 3, 2.6, 6, 9, 5.6]

# Plot

plt.plot(x, y)

# Set theta ticks

plt.xticks([1, 2, 3, 4],[r'2$\theta$', r'4$\theta$', r'6$\theta$', r'8$\theta$'])

# Display

plt.show()

You may like the following python matplotlib tutorials:

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

• Matplotlib set_xticks
• Matplotlib set_xticks invisible
• Matplotlib set_xticks rotation
• Matplotlib set_xticks fontsize
• Matplotlib set_xtciks labels
• Matplotlib set_xticks vertical
• Matplotlib set_xticks direction
• Matplotlib set_xticks length
• Matplotlib set_xticks color
• Matplotlib barplot set_xticks
• Matplotlib histogram set_xticks
• Matplotlib colorbar set_xticks
• Matplotlib set_xticks subplot
• Matplotlib set_xticks log scale
• Matplotlib set ticks right
• Matplotlib set ticks top
• Matplotlib set ticks every
• Matplotlib set_xticks range
• Matplotlib set minor ticks interval