简体   繁体   English

在Matplotlib图中自定义Xticks

[英]Customize xticks in matplotlib plot

I want to plot a figure which has the xticks starting from, for example, 50 to 100 . 我想绘制一个具有从例如50100xticks的图形。

What I have tried so far: 到目前为止我尝试过的是:

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()

And what I get is this figure: 我得到的是这个数字:

情节

Instead of a figure with the xticks starting from 50 up to 140 , ie, the list [50, 60, 70, ..., 130, 140] for the labels in the xticks. 而不是带有xticks50140的图形,即xticks中标签的列表[50、60、70,...,130、140]。

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

you have to specify the x-values as well when you are plotting the graph. 绘制图形时,还必须指定x值。 Then, based on the level of detail you need, set the x_ticks. 然后,根据所需的详细程度设置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))

在此处输入图片说明

You need to define a spacing of 10 while creating your ticks as 在创建刻度时,您需要将间距定义为10

np.arange(50, 140, 10)

The default spacing in np.arange is 1 which means np.arange(50, 140) which result in an array [50, 51, 52, ... 139] np.arange的默认间距为1,这意味着np.arange(50, 140)会导致数组[50, 51, 52, ... 139]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM