0

When I use the following code below to make a plot, in which I assign customized values to the x axis and set xlim, it works out.

x = np.array([0,1,2,3])
y = np.array([0.650, 0.660, 0.675, 0.685])
customized_x = np.array([3, 4, 5, 6])
plt.xticks(x, customized_x)
plt.xlim(2, 3)
plt.plot(x, y)
plt.show()

However, because I want to make a few more plots together, when I use subplot instead, it pops out at the line ax1.set_xticks(x, customized_x):

fig = plt.figure(figsize=[6.0, 9.0])
ax1 = fig.add_subplot(111)

x = np.array([0,1,2,3])
y = np.array([0.650, 0.660, 0.675, 0.685])
customized_x = np.array([3, 4, 5, 6])
ax1.set_xticks(x, customized_x)
ax1.set_xlim(2, 3)
ax1.plot(x, y)
plt.show()

Error obtained:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 

Although I can avoid the issue by not using subplots, I am still wondering what's wrong with subplots?

EDIT

I find a solution to the similar question here. Sorry for the repetition.

Consideration
  • 182
  • 1
  • 1
  • 12

1 Answers1

0

You appear to use set_xticks mistakenly. From matplotlib documentation:

Axes.set_xticks(self, ticks, minor=False)ΒΆ

This is because Axes.set_xticks has different arguments, compared to plt.xticks. The code tries to evaluate customized_x as the minor parameter.

I think what you need is Axes.set_xticklabels: see the documentation here