在Matplotlib图中自定义Xticks

时间:2018-12-13 17:35:44

标签: python matplotlib

我想绘制一个图形,该图形的xticks50100

到目前为止,我已经尝试过:

data_to_plot = data[-90:]  # 90 datapoints

# Create figure
fig = plt.figure()
ax = fig.add_subplot(111)

# Plot the value
ax.plot(data_to_plot/S)

# Set the ticks
x_ticks = np.arange(50, 140)
ax.set_xticks(x_ticks)

# Limits
plt.ylim(0, 1)

# Naming
plt.title("Cooperation")
plt.xlabel("Round")
plt.ylabel("Value")
plt.show()

我得到的是这个数字:

Plot

代替从50140 xticks 的图形,即列表[50,60,70,...,130,140 ]来标记xticks中的标签。

使用: Python 3,Matplotlib 3.0.2,MacOS 10.13。

2 个答案:

答案 0 :(得分:3)

在绘制图形时,还必须指定x值。 然后,根据所需的详细程度设置x_ticks。

# Plot the value
x_ticks = np.arange(50, 140)
ax.plot(x_ticks,data_to_plot)

# Set the ticks
ax.set_xticks(np.arange(50, 140,10))

enter image description here

答案 1 :(得分:2)

在创建刻度时,您需要将间距定义为10

np.arange(50, 140, 10)

np.arange中的默认间距为1,这意味着np.arange(50, 140)会产生数组[50, 51, 52, ... 139]