数据可视化是分析数据的重要部分,因为绘制图形有助于更好地了解和理解问题。 Matplotlib.pyplot是执行此操作的最常用的库之一。它有助于创建有吸引力的数据,并且超级易于使用。
Matplotlib.pyplot.semilogx()函数
此功能用于以x轴转换为对数格式的方式显示数据。当参数之一非常大并因此最初以紧凑方式存储时,此功能特别有用。它支持plot()和matplotlib.axes.Axes.set_xscale()的所有关键字参数。其他参数是basex,subsx和nonposx。
用法:Matplotlib.pyplot.semilogx(x, y, )
参数:一些重要的参数是:
x:X轴上的值。
y:Y轴上的值。
color:(可选)线条或符号的颜色。
linewidth:(可选)线的宽度。
label:(可选)指定图的标签
basex:(可选)x对数的底数。标量应大于1。
subsx:(可选)次要ticks的位置;没有默认值是自动订阅,这取决于情节中的十年数。
nonposx:(可选)可以将x中的非正值掩盖为无效值,或将其裁剪为非常小的正数。
marker:(可选)将点显示为上述符号。
markersize:(可选)更改所有标记的大小。
Return:X轴上的log-scaled图。
范例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()
输出:
一个简单的情节
示例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值,因此未绘制任何值
范例3:如果使用符号,则仅删除负值或零值,仅绘制正值。
Python3
#import required library
import matplotlib.pyplot as plt
# defining the values at X and Y axis
x = [-10, 30, 0, 20,