Python| Matplotlib.pyplot 滴答声
Matplotlib 是Python中用于数组二维图的惊人可视化库。 Matplotlib 是一个基于 NumPy 数组构建的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈配合使用。它是由 John Hunter 在 2003 年推出的。
可视化的最大好处之一是它允许我们以易于理解的视觉效果直观地访问大量数据。 Matplotlib 由几个图组成,如线、条、散点图、直方图等。
刻度是用于在坐标轴上显示特定点的值。它可以是数字或字符串。每当我们绘制图表时,轴都会调整并采用默认刻度。 Matplotlib 的默认刻度通常在常见情况下就足够了,但对于每个绘图来说绝不是最佳的。在这里,我们将看到如何根据需要自定义这些刻度。
参数:
Parameter Value Use axis x, y, both Tells which axis to operate reset True, False If True, set all parameters to default direction in, out, inout Puts the ticks inside or outside or both length Float Sets tick’s length width Float Sets tick’s width rotation Float Rotates ticks wrt the axis colors Color Changes tick color pad Float Distance in points between tick and label
示例 #1:默认绘图
Python3
# importing required modules
import matplotlib.pyplot as plt
# values of x and y axes
x = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
y = [1, 4, 3, 2, 7, 6, 9, 8, 10, 5]
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')
plt.show()
Python
# importing libraries
import random
import matplotlib.pyplot as plt
fig = plt.figure()
# function to get random values for graph
def get_graphs():
xs =[]
ys =[]
for i in range(10):
xs.append(i)
ys.append(random.randrange(10))
return xs, ys
# defining subplots
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
# hiding the marker on axis
x, y = get_graphs()
ax1.plot(x, y)
ax1.tick_params(axis ='both', which ='both', length = 0)
# One can also change marker length
# by setting (length = any float value)
# hiding the ticks and markers
x, y = get_graphs()
ax2.plot(x, y)
ax2.axes.get_xaxis().set_visible(False)
ax2.axes.get_yaxis().set_visible(False)
# hiding the values and displaying the marker
x, y = get_graphs()
ax3.plot(x, y)
ax3.yaxis.set_major_formatter(plt.NullFormatter())
ax3.xaxis.set_major_formatter(plt.NullFormatter())
# tilting the ticks (usually needed when
# the ticks are densely populated)
x, y = get_graphs()
ax4.plot(x, y)
ax4.tick_params(axis ='x', rotation = 45)
ax4.tick_params(axis ='y', rotation =-45)
plt.show()
Python3
# importing libraries
import matplotlib.pyplot as plt
import numpy as np
# values of x and y axes
x = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
y = [1, 4, 3, 2, 7, 6, 9, 8, 10, 5]
plt.plot(x, y, 'b')
plt.xlabel('x')
plt.ylabel('y')
# 0 is the initial value, 51 is the final value
# (last value is not taken) and 5 is the difference
# of values between two consecutive ticks
plt.xticks(np.arange(0, 51, 5))
plt.yticks(np.arange(0, 11, 1))
plt.show()
输出 :
示例 #2:玩蜱
假设我们不想显示刻度的值或希望我们的刻度倾斜或想要任何其他自定义。我们可以这样做。
Python
# importing libraries
import random
import matplotlib.pyplot as plt
fig = plt.figure()
# function to get random values for graph
def get_graphs():
xs =[]
ys =[]
for i in range(10):
xs.append(i)
ys.append(random.randrange(10))
return xs, ys
# defining subplots
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
# hiding the marker on axis
x, y = get_graphs()
ax1.plot(x, y)
ax1.tick_params(axis ='both', which ='both', length = 0)
# One can also change marker length
# by setting (length = any float value)
# hiding the ticks and markers
x, y = get_graphs()
ax2.plot(x, y)
ax2.axes.get_xaxis().set_visible(False)
ax2.axes.get_yaxis().set_visible(False)
# hiding the values and displaying the marker
x, y = get_graphs()
ax3.plot(x, y)
ax3.yaxis.set_major_formatter(plt.NullFormatter())
ax3.xaxis.set_major_formatter(plt.NullFormatter())
# tilting the ticks (usually needed when
# the ticks are densely populated)
x, y = get_graphs()
ax4.plot(x, y)
ax4.tick_params(axis ='x', rotation = 45)
ax4.tick_params(axis ='y', rotation =-45)
plt.show()
输出:
示例 #3:更改刻度的值。
在第一个示例中,x 轴和 y 轴分别除以 10 和 2 的值。让我们把它设为 5 和 1。
Python3
# importing libraries
import matplotlib.pyplot as plt
import numpy as np
# values of x and y axes
x = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
y = [1, 4, 3, 2, 7, 6, 9, 8, 10, 5]
plt.plot(x, y, 'b')
plt.xlabel('x')
plt.ylabel('y')
# 0 is the initial value, 51 is the final value
# (last value is not taken) and 5 is the difference
# of values between two consecutive ticks
plt.xticks(np.arange(0, 51, 5))
plt.yticks(np.arange(0, 11, 1))
plt.show()
输出:
与第一个示例的主要区别是:
plt.xticks(np.arange(0, 51, 5))
plt.yticks(np.arange(0, 11, 1))
更改 np.arange 中的值将更改刻度的范围。参考: Matplotlib 滴答作响。