plr
plr

Reputation: 244

Customize xticks in matplotlib plot

I want to plot a figure which has the xticks starting from, for example, 50 to 100.

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:

Plot

Instead of a figure with the xticks starting from 50 up to 140, i.e., the list [50, 60, 70, ..., 130, 140] for the labels in the xticks.

Using: Python 3, Matplotlib 3.0.2, MacOS 10.13.

Upvotes: 1

Views: 1758

Answers (2)

Venkatachalam
Venkatachalam

Reputation: 16966

you have to specify the x-values as well when you are plotting the graph. Then, based on the level of detail you need, set the 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

Upvotes: 5

Sheldore
Sheldore

Reputation: 39072

You need to define a spacing of 10 while creating your ticks as

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]

Upvotes: 2

Related Questions