Python - Matplotlib - 轴标签
Python - Matplotlib - Axis Labels
我正在尝试在我的 X 轴上绘制带有两位小数的四个数字标签 - 例如“1475.88”。如您所见,标签已被 Matplotlib 缩短为科学格式 1.478e3.
如何以定义的间距显示完整图形。
代码如下:
with plt.style.context('seaborn-whitegrid'):
# Plot the SVP data
plt.figure(figsize=(8, 8))
plt.plot(speed_x_clean, depth_y_clean)
plt.plot(smoothed_speed, smoothed_depth)
plt.scatter(float(speed_extrapolated), float(depth_extrapolated),marker='X', color='#ff007f')
# Add a legend, labels and titla
plt.gca().invert_yaxis()
plt.legend(('Raw SVP', 'Smoothed SVP'), loc='best')
plt.title('SVP - ROV '+ '['+ time_now + ']')
plt.xlabel('Sound Velocity [m/s]')
plt.ylabel('Depth [m]')
# plt.grid(color='grey', linestyle='--', linewidth=0.25, grid_animated=True)
ax = plt.axes()
plt.gca().xaxis.set_major_locator(plt.AutoLocator())
plt.xticks(rotation=0)
plt.show()
带有两位小数的标签
使用代码 FuncFormatter 可以实现任何用户定义的格式。
@ticker.FuncFormatter
def major_formatter(val, pos):
return "%.2f" % val
以定义的间距放置标签
使用 set_xticks 和 set_yticks 可以按定义的间距设置标签数量。
自包含示例
一个完全独立的示例可能如下所示(简单的正弦波):
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as ticker
@ticker.FuncFormatter
def major_formatter(val, pos):
return "%.2f" % val
def graph():
x = np.arange(0.0, 1501, 50)
y = np.sin(2 * np.pi * x / 1500)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.xaxis.set_major_formatter(major_formatter)
ax.yaxis.set_major_formatter(major_formatter)
x_ticks = np.arange(0, 1501, 500)
y_ticks = np.arange(-1.0, +1.01, 0.5)
ax.set_xticks(x_ticks)
ax.set_yticks(y_ticks)
ax.grid(which='both')
plt.show()
if __name__ == '__main__':
graph()
输出
这里是示例程序的输出:它有四个图形标签,x 轴上有两个小数位:
我正在尝试在我的 X 轴上绘制带有两位小数的四个数字标签 - 例如“1475.88”。如您所见,标签已被 Matplotlib 缩短为科学格式 1.478e3.
如何以定义的间距显示完整图形。
代码如下:
with plt.style.context('seaborn-whitegrid'):
# Plot the SVP data
plt.figure(figsize=(8, 8))
plt.plot(speed_x_clean, depth_y_clean)
plt.plot(smoothed_speed, smoothed_depth)
plt.scatter(float(speed_extrapolated), float(depth_extrapolated),marker='X', color='#ff007f')
# Add a legend, labels and titla
plt.gca().invert_yaxis()
plt.legend(('Raw SVP', 'Smoothed SVP'), loc='best')
plt.title('SVP - ROV '+ '['+ time_now + ']')
plt.xlabel('Sound Velocity [m/s]')
plt.ylabel('Depth [m]')
# plt.grid(color='grey', linestyle='--', linewidth=0.25, grid_animated=True)
ax = plt.axes()
plt.gca().xaxis.set_major_locator(plt.AutoLocator())
plt.xticks(rotation=0)
plt.show()
带有两位小数的标签
使用代码 FuncFormatter 可以实现任何用户定义的格式。
@ticker.FuncFormatter
def major_formatter(val, pos):
return "%.2f" % val
以定义的间距放置标签
使用 set_xticks 和 set_yticks 可以按定义的间距设置标签数量。
自包含示例
一个完全独立的示例可能如下所示(简单的正弦波):
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as ticker
@ticker.FuncFormatter
def major_formatter(val, pos):
return "%.2f" % val
def graph():
x = np.arange(0.0, 1501, 50)
y = np.sin(2 * np.pi * x / 1500)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.xaxis.set_major_formatter(major_formatter)
ax.yaxis.set_major_formatter(major_formatter)
x_ticks = np.arange(0, 1501, 500)
y_ticks = np.arange(-1.0, +1.01, 0.5)
ax.set_xticks(x_ticks)
ax.set_yticks(y_ticks)
ax.grid(which='both')
plt.show()
if __name__ == '__main__':
graph()
输出
这里是示例程序的输出:它有四个图形标签,x 轴上有两个小数位: