Matplotlib.pyplot.semilogx() in Python

Data Visualization Is an important part of analyzing the data as plotting graphs helps in providing better insight and understanding of the problem. Matplotlib.pyplot is one of the most commonly used libraries to do the same. It helps in creating attractive data and is super easy to use. 

Matplotlib.pyplot.semilogx() Function

 This function is used to visualize data in a manner that the x-axis is converted to log format. This function is particularly useful when one of the parameters is extremely large and thus stored in a compact manner initially. It supports all the keyword arguments of the plot() and matplotlib.axes.Axes.set_xscale(). The additional parameters are basex, subsx and nonposx.

Syntax: Matplotlib.pyplot.semilogx(x, y, ) 

Parameters: Some important parameters are:

  • x: Values on X-axis.
  • y: Values on Y-axis.
  • color: (optional) Color of the line or the symbol.
  • linewidth: (optional) Width of the line.
  • label: (optional) Specifies the label of the graph
  • basex: (optional) The base of the x logarithm. The scalar should be larger than 1.
  • subsx: (optional) The location of the minor xticks; None defaults to autosubs, which depend on the number of decades in the plot.
  • nonposx: (optional) Non-positive values in x can be masked as invalid, or clipped to a very small positive number.
  • marker: (optional) Displays the points as the mentioned symbol.
  • markersize: (optional) Changes the size of all the markers.

Return: A log-scaled plot on the x-axis.



Example 1: simple plot.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

#import required library
import matplotlib.pyplot as plt
  
# defining the values 
# at X and Y axis
x = [1, 2, 3
     4, 5, 6]
y = [100, 200, 300
     400, 500, 600]
  
# plotting the given graph 
plt.semilogx(x, y, marker = ".",
             markersize = 15
             color = "green")
# plot with grid
plt.grid(True)
  
# show the plot
plt.show()

chevron_right


Output:

A simple plot

A simple plot

Example 2: Using negative and zero values in X and Y axis.

Since the X-axis is involved in the logarithmic function, it is clear that the negative or the positive values would either be clipped or masked, as specified by the nonposx parameter. By default, the negative or zero values are clipped.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing required libraries
import matplotlib.pyplot as plt
  
  
# defining the values
# at X and Y axis
x = [-1, -2, 0]
y = [5, -2, 0]
  
# plotting the given graph 
plt.semilogx(x,y)
  
# show the plot
plt.show()

chevron_right


Output:

No value is plotted as all are negative x values

No value is plotted as all are negative x values 

Example 3: If symbols are used then the negative or zero values are simply removed and only the positive values are plotted.



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

#import required library
import matplotlib.pyplot as plt
  
# defining the values at X and Y axis
x = [-10, 30, 0, 20,
     -50, 25, 29, -3
     , 23, 25, 29, 31]
y = [-3, 30, -10, 0,
     -40, 3, 8, 0
     -24, 40, 43, 25]
  
# plotting the graph
plt.semilogx(x,y,'g^', color = "red")
  
# plot with grid
plt.grid(True)
  
# set y axis label
plt.ylabel('---y---')
  
# set x axis label
plt.xlabel('---x---')
  
# show the plot
plt.show()

chevron_right


Output:

Only positive values are plotted

Only positive values are plotted

Example 4: If the lines are used, the values are clipped.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

#import required library
import matplotlib.pyplot as plt
  
# defining the values 
# at X and Y axis
x = [1, 2, -3
     -4, 5, 6]
y = [100, 200, 300,
     400, 500, 600]
  
# plotting the graph
plt.semilogx(x, y, marker = "."
             markersize = 15)
  
# plot with grid
plt.grid(True)
  
# show the plot
plt.show()

chevron_right


Output: 

The values corresponding to -3 and -4 are clipped

The values corresponding to -3 and -4 are clipped

Example 5: The following subplots will make the differences more clear.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

#import required library
import matplotlib.pyplot as plt
  
# specifing the subplot
fig, axes = plt.subplots(nrows = 4
                         ncols = 4
                         figsize = (10,10))
  
# Or equivalently,  
# "plt.tight_layout()"
fig.tight_layout()
  
# subplot 1
plt.subplot(2, 2, 1)
x2 = [0.1, 10, -30]
y2 = [40, -10, 45]
  
# plotting the given graph 
plt.semilogx(x2, y2, 
             color = "blue",
             linewidth = 4)
# set the title
plt.title("USING LINE")
  
# set y axis label
plt.ylabel('-----------y-----------')
  
# set x axis label
plt.xlabel('-----------x-----------')
  
# plot with grid
plt.grid(True)
  
  
# subplot 2
plt.subplot(2, 2, 2)
x2 = [0.1, 10, -30]
y2 = [40, -10, 45]
  
# plotting the given graph
plt.semilogx(x2, y2, 
             'g^',
             markersize = 20,
             color = "black")
# set the title
plt.title("USING SYMBOL")
  
# set y axis label
plt.ylabel('-----------y-----------')
  
# set x axis label
plt.xlabel('-----------x-----------')
  
# plot with grid
plt.grid(True)
  
# subplot 3
plt.subplot(2, 2, 3)
x2 = [0.1, 10, -30]
y2 = [40, -10 ,45]
  
# plotting the given graph
plt.semilogx(x2, y2, 
             nonposx = "clip",
             color = "red",
             linewidth = 4)
# set the title
plt.title("CLIPPED")
  
# set y axis label
plt.ylabel('-----------y-----------')
  
# set x axis label
plt.xlabel('-----------x-----------')
  
# plot with grid
plt.grid(True)
  
# subplot 4
plt.subplot(2, 2, 4)
x2 = [0.1, 10, -30]
y2 = [40, -10, 45]
  
# plotting the given graph
plt.semilogx(x2, y2, 
             nonposx = "mask",
             color = "green"
             linewidth = 4)
  
# set the title
plt.title("MASKED")
  
# set y axis label
plt.ylabel('-----------y-----------')
  
# set x axis label
plt.xlabel('-----------x-----------')
  
# plot with grid
plt.grid(True)
  
# show the plot
plt.show()

chevron_right


Output:

Differences between all the plot.

Differences between all the plot.

Example 6: Using nonposx parameter.

Masking removes the invalid values while clipping sets them to a very low possible value.



The difference between clipping and masking will be more clear in the following plot.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import required library
import matplotlib.pyplot as plt
  
fig, axes = plt.subplots(nrows = 1,
                         ncols = 2
                         figsize = (15,9))
# Or equivalently,  "plt.tight_layout()"
fig.tight_layout() 
  
  
# subplot 1
x1 = [-1, 2, 0,
      -3, 5, 9,
      10, -3, -8,
      15, 12, 0.1,0.9]
  
y1 = [5, -2, 0
      10, 20, 30,
      25, 28, 16,
      25, 28, 3, 5]
  
plt.subplot(1,2,1)
  
# plotting the graph
plt.semilogx(x1, y1, 
             marker = ".",
             markersize = 20,
             nonposx = "clip",
             color = "green" )
  
# set the y-axis label
plt.ylabel('---y---')
  
# set the x-axis label
plt.xlabel('---x---')
  
# set the title
plt.title('CLIP')
  
# plot with grid
plt.grid(True)
  
  
# subplot 2
x2 = [-1, 2, 0,
      -3, 5, 9,
      10, -3, -8,
      15, 12, 0.1, 0.9]
  
y2 = [5, -2, 0,
      10, 20, 30,
      25, 28, 16,
      25, 28, 3, 5]
  
plt.subplot(1,2,2)
plt.semilogx(x2, y2, 
             nonposx = "mask"
             color ="green"
             linewidth = 4,
             marker = ".",
             markersize = 20)
  
# set the title
plt.title('MASK')
  
# set the y-axis label
plt.ylabel('---y---')
  
# set the x-axis label
plt.xlabel('---x---')
  
# plot with grid
plt.grid(True)
  
# show the plot
plt.show()

chevron_right


Output:

Difference between mask and clip

Difference between mask and clip

Example 7: Changing the base.

The base can be set as per the convenience and it should be greater than 1 to satisfy the logarithmic properties.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the required libraries
import numpy as np
import matplotlib.pyplot as plt
  
# function that will 
# ouput the values
def function(t):
    return np.exp(-t)*np.sin(2*np.pi.t)/2 + np.tan(t)
  
# define the x-axis values
t1 = np.arange(-0.01, 1.0, 0.08)
t2 = np.arange(0.0, 5.0, 0.02)
  
  
# subplot 1
plt.figure(figsize = (10,10))
plt.subplot(211)
  
# plot the graph
plt.semilogx(t1, f(t1),
             'bo', t2, f(t2),
             'k', color = "blue",
             basex = 3)
# set the title
plt.title("BASE: 3")
  
# subplot 2
plt.subplot(212)
  
# plot the graph
plt.semilogx(t2, np.cos(2*np.pi*t2),
             'r--', color = "brown",
             linewidth = 2, basex = 4)
  
# set the title
plt.title("BASE: 4")
  
# show the plot
plt.show()

chevron_right


Output:

Changing the base

Changing the base

Example 8: Using subsx parameter.

Specifies the minor xticks on the X-axis. By default, it depends on the number of decades in the plot.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import required library
import matplotlib.pyplot as plt
  
fig, axes = plt.subplots(nrows = 2
                         ncols = 2
                         figsize = (10,7))
  
# Or equivalently,  "plt.tight_layout()"
fig.tight_layout() 
  
# subplot 1
plt.subplot(2, 2, 1)
x = [1, 11]
y = [4, 6]
  
# plot the graph
plt.semilogx(x, y, marker = ".",
             markersize = 20,
             color = "green")
  
# set the title
plt.title("Without subsx - line ")
  
# plot with grid
plt.grid(True)
  
  
# subplot 2
plt.subplot(2, 2, 2)
x = [1, 11]
y = [4, 6]
  
# plot the graph
plt.semilogx(x, y, subsx = [2, 3, 9, 10],
             marker = ".", markersize = 20,
             color = "green")
  
# set the title
plt.title("With subsx - line ")
plt.grid(True)
  
  
# subplot 3
plt.subplot(2, 2, 3)
x = [1, 11]
y = [4, 6]
plt.semilogx(x, y, 'g^', marker = ".",
             markersize = 20
             color = "blue")
plt.title("Without subsx - symbol ")
plt.grid(True)
  
  
# subplot 4
plt.subplot(2, 2, 4)
x = [1, 11]
y = [4, 6]
plt.semilogx(x, y, 'g^', subsx=[2, 3, 9, 10],
             marker = ".", markersize = 20
             color = "blue")
plt.title("With subsx - symbol ")
plt.grid(True)
  
plt.show()

chevron_right


Output:

SUBSX parameter

SUBSX parameter

Summary:

  • The X-axis is plotted in the logarithmic manner and base can be specified by defining the basex property. The base should be greater than 1
  • If lines are plotted then the negative or zero values are clipped by default.
  • The mask property removes the negative/zero values while clip property sets them to a very low positive value.
  • If the symbols are used then the negative/zero are masked by default.
  • semilogx follows all the arguments of plot() and matplotlib.axes.Axes.set_xscale().
  • subsx parameter defines the minor xticks.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.