Matplotlib.pyplot.semilogx() in Python - GeeksforGeeks

29 May, 2021
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. 
 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
#import required libraryimport matplotlib.pyplot as plt # defining the values# at X and Y axisx = [1, 2, 3,     4, 5, 6]y = [100, 200, 300,     400, 500, 600] # plotting the given graphplt.semilogx(x, y, marker = ".",             markersize = 15,             color = "green")# plot with gridplt.grid(True) # show the plotplt.show()
  
Output:
 
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
# importing required librariesimport matplotlib.pyplot as plt  # defining the values# at X and Y axisx = [-1, -2, 0]y = [5, -2, 0] # plotting the given graphplt.semilogx(x,y) # show the plotplt.show()
  
Output:
 
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
#import required libraryimport matplotlib.pyplot as plt # defining the values at X and Y axisx = [-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 graphplt.semilogx(x,y,'g^', color = "red") # plot with gridplt.grid(True) # set y axis labelplt.ylabel('---y---') # set x axis labelplt.xlabel('---x---') # show the plotplt.show()
  
Output:
 
Only positive values are plotted
 
Example 4: If the lines are used, the values are clipped.
 
Python3
#import required libraryimport matplotlib.pyplot as plt # defining the values# at X and Y axisx = [1, 2, -3,     -4, 5, 6]y = [100, 200, 300,     400, 500, 600] # plotting the graphplt.semilogx(x, y, marker = ".",             markersize = 15) # plot with gridplt.grid(True) # show the plotplt.show()
  
Output: 
 
The values corresponding to -3 and -4 are clipped
 
Example 5: The following subplots will make the differences more clear.
 
Python3
#import required libraryimport matplotlib.pyplot as plt # specifying the subplotfig, axes = plt.subplots(nrows = 4,                         ncols = 4,                         figsize = (10,10)) # Or equivalently, # "plt.tight_layout()"fig.tight_layout() # subplot 1plt.subplot(2, 2, 1)x2 = [0.1, 10, -30]y2 = [40, -10, 45] # plotting the given graphplt.semilogx(x2, y2,             color = "blue",             linewidth = 4)# set the titleplt.title("USING LINE") # set y axis labelplt.ylabel('-----------y-----------') # set x axis labelplt.xlabel('-----------x-----------') # plot with gridplt.grid(True)  # subplot 2plt.subplot(2, 2, 2)x2 = [0.1, 10, -30]y2 = [40, -10, 45] # plotting the given graphplt.semilogx(x2, y2,             'g^',             markersize = 20,             color = "black")# set the titleplt.title("USING SYMBOL") # set y axis labelplt.ylabel('-----------y-----------') # set x axis labelplt.xlabel('-----------x-----------') # plot with gridplt.grid(True) # subplot 3plt.subplot(2, 2, 3)x2 = [0.1, 10, -30]y2 = [40, -10 ,45] # plotting the given graphplt.semilogx(x2, y2,             nonposx = "clip",             color = "red",             linewidth = 4)# set the titleplt.title("CLIPPED") # set y axis labelplt.ylabel('-----------y-----------') # set x axis labelplt.xlabel('-----------x-----------') # plot with gridplt.grid(True) # subplot 4plt.subplot(2, 2, 4)x2 = [0.1, 10, -30]y2 = [40, -10, 45] # plotting the given graphplt.semilogx(x2, y2,             nonposx = "mask",             color = "green",             linewidth = 4) # set the titleplt.title("MASKED") # set y axis labelplt.ylabel('-----------y-----------') # set x axis labelplt.xlabel('-----------x-----------') # plot with gridplt.grid(True) # show the plotplt.show()
  
Output:
 
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
# import required libraryimport matplotlib.pyplot as plt fig, axes = plt.subplots(nrows = 1,                         ncols = 2,                         figsize = (15,9))# Or equivalently,  "plt.tight_layout()"fig.tight_layout()  # subplot 1x1 = [-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 graphplt.semilogx(x1, y1,             marker = ".",             markersize = 20,             nonposx = "clip",             color = "green" ) # set the y-axis labelplt.ylabel('---y---') # set the x-axis labelplt.xlabel('---x---') # set the titleplt.title('CLIP') # plot with gridplt.grid(True)  # subplot 2x2 = [-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 titleplt.title('MASK') # set the y-axis labelplt.ylabel('---y---') # set the x-axis labelplt.xlabel('---x---') # plot with gridplt.grid(True) # show the plotplt.show()
  
Output:
 
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
# importing the required librariesimport numpy as npimport matplotlib.pyplot as plt # function that will# output the valuesdef function(t):    return np.exp(-t)*np.sin(2*np.pi.t)/2 + np.tan(t) # define the x-axis valuest1 = np.arange(-0.01, 1.0, 0.08)t2 = np.arange(0.0, 5.0, 0.02)  # subplot 1plt.figure(figsize = (10,10))plt.subplot(211) # plot the graphplt.semilogx(t1, f(t1),             'bo', t2, f(t2),             'k', color = "blue",             basex = 3)# set the titleplt.title("BASE: 3") # subplot 2plt.subplot(212) # plot the graphplt.semilogx(t2, np.cos(2*np.pi*t2),             'r--', color = "brown",             linewidth = 2, basex = 4) # set the titleplt.title("BASE: 4") # show the plotplt.show()
  
Output:
 
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
# import required libraryimport matplotlib.pyplot as plt fig, axes = plt.subplots(nrows = 2,                         ncols = 2,                         figsize = (10,7)) # Or equivalently,  "plt.tight_layout()"fig.tight_layout() # subplot 1plt.subplot(2, 2, 1)x = [1, 11]y = [4, 6] # plot the graphplt.semilogx(x, y, marker = ".",             markersize = 20,             color = "green") # set the titleplt.title("Without subsx - line ") # plot with gridplt.grid(True)  # subplot 2plt.subplot(2, 2, 2)x = [1, 11]y = [4, 6] # plot the graphplt.semilogx(x, y, subsx = [2, 3, 9, 10],             marker = ".", markersize = 20,             color = "green") # set the titleplt.title("With subsx - line ")plt.grid(True)  # subplot 3plt.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 4plt.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()
  
Output:
 
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.
 
arorakashish0911
Code_Mech
Matplotlib Pyplot-class
Python-matplotlib
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python program to convert a list to string
Python String | replace()
Reading and Writing to text files in Python
sum() function in Python