簡體   English   中英

如何只用整數設置x軸刻度?

[英]How can I set x-axis tick marks with only whole numbers?

我一直在使用主機繪制我的圖形,我想從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()

您可以使用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()

您可以在顯示繪圖之前明確設置刻度標簽:

plt.xticks(RUNS)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM