📜  Python中的 Matplotlib.pyplot.semilogx()

📅  最后修改于: 2022-05-13 01:55:26.367000             🧑  作者: Mango

Python中的 Matplotlib.pyplot.semilogx()

数据可视化是分析数据的重要组成部分,因为绘制图表有助于更好地洞察和理解问题。 Matplotlib.pyplot是最常用的库之一。它有助于创建有吸引力的数据,并且非常易于使用。

Matplotlib.pyplot.semilogx()函数

此函数用于以 x 轴转换为日志格式的方式可视化数据。当其中一个参数非常大并因此最初以紧凑的方式存储时,此函数特别有用。它支持plot()matplotlib.axes.Axes.set_xscale()的所有关键字参数。附加参数是basexsubsxnonposx。

示例 1:简单的情节。

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


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


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


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


Python3
#import required library
import matplotlib.pyplot as plt
 
# specifying 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()


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


Python3
# importing the required libraries
import numpy as np
import matplotlib.pyplot as plt
 
# function that will
# output 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()


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



输出:

一个简单的情节

一个简单的情节

示例 2:在 X 和 Y 轴上使用负值和零值。

由于 X 轴涉及对数函数,因此很明显,负值或正值将被裁剪或屏蔽,如 nonposx 参数所指定。默认情况下,负值或零值被剪裁。

Python3

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


输出:

没有绘制任何值,因为都是负 x 值

没有绘制任何值,因为都是负 x 值

示例 3:如果使用符号,则简单地删除负值或零值,仅绘制正值。

Python3

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


输出:

仅绘制正值

仅绘制正值

示例 4:如果使用线条,则将剪裁值。

Python3

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


输出:

-3 和 -4 对应的值被裁剪

-3 和 -4 对应的值被裁剪

示例 5:以下子图将使差异更加清晰。

Python3

#import required library
import matplotlib.pyplot as plt
 
# specifying 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()


输出:

所有情节之间的差异。

所有情节之间的差异。

示例 6:使用 nonposx 参数。

屏蔽会删除无效值,而剪辑会将它们设置为非常低的可能值。

在下面的图中,裁剪遮罩之间的区别将更加清晰。

Python3

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


输出:

蒙版和剪辑之间的区别

蒙版和剪辑之间的区别

示例 7:更改基础。

底数可以根据方便设置,它应该大于1以满足对数性质。

Python3

# importing the required libraries
import numpy as np
import matplotlib.pyplot as plt
 
# function that will
# output 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()


输出:

改变基地

改变基地

示例 8:使用 subsx 参数。

指定 X 轴上的次要 xticks。默认情况下,它取决于图中的几十年数。

Python3

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


输出

SUBSX 参数

SUBSX 参数

概括:

  • X 轴以对数方式绘制,并且可以通过定义basex属性来指定底数。基数应大于 1
  • 如果绘制线条,则默认情况下会裁剪负值或零值。
  • mask属性删除负值/零值,而clip属性将它们设置为非常低的正值。
  • 如果使用符号,则默认情况下会屏蔽负数/零。
  • semilogx遵循plot()matplotlib.axes.Axes.set_xscale() 的所有参数。
  • subsx参数定义次要 xticks。