我一直在使用主机来绘制我的图形,我想从x轴取出十进制数字。我的下面的代码有点简化,它代表了我需要的东西。我希望x范围与RUNS矢量完全相同。非常感谢。
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import host_subplot
import numpy as np
import mpl_toolkits.axisartist as AA
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 1, height_ratios=[1, 1])
RUNS = [1,2,3,4,5]
NV = [26.3, 28.4, 28.5, 28.45, 28.5]
host = host_subplot(gs[0], axes_class = AA.Axes)
host.set_xlabel("Iteration")
host.set_ylabel("Stress (MPa)")
sv, = host.plot(RUNS,NV, marker = 'o', color = 'gray')
plt.grid(True)
plt.show()
答案 0 :(得分:7)
您也可以使用MaxNLocator
将主要刻度标签设置为整数,但它不是很简单。
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import host_subplot
import numpy as np
import mpl_toolkits.axisartist as AA
import matplotlib.gridspec as gridspec
from matplotlib.ticker import MaxNLocator ## Import MaxNLocator
gs = gridspec.GridSpec(2, 1, height_ratios=[1, 1])
RUNS = [1,2,3,4,5]
NV = [26.3, 28.4, 28.5, 28.45, 28.5]
host = host_subplot(gs[0], axes_class = AA.Axes)
host.set_xlabel("Iteration")
host.set_ylabel("Stress (MPa)")
x_ax = host.axes.get_xaxis() ## Get X axis
x_ax.set_major_locator(MaxNLocator(integer=True)) ## Set major locators to integer values
sv, = host.plot(RUNS,NV, marker = 'o', color = 'gray')
plt.grid(True)
plt.show()
答案 1 :(得分:5)
您可以在显示绘图之前明确设置刻度标签:
plt.xticks(RUNS)