0

When trying to run the following code I get this warning under it:

<ipython-input-47-f4ba06f67a93>:13: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance. ax = plt.axes()

from pylab import * 
import numpy as np
from math import *

x = np.linspace(0, 10, 1000)

E = np.piecewise(x, [x < 1, ((x >= 1) & (x < 2)), x >= 2], [0 , lambda x : (1/x)-(1/x**2), lambda x : 1/(x**2)])

#Graph of |E| a epsilon / k vs. x=r/a for unitless quantities

figure()
plot(x, E)
xlabel('r / a') 
ylabel('|E|a epsilon / k') 
title('Behavior of |E| vs r')

ax = plt.axes()

ax.set_xticks([1, 2])
  
ax.set_xticklabels(['a', 'b'])

#plt.tick_params(left = False, right = False , labelleft = False ,
#labelbottom = False, bottom = False)

grid()

show()

Could anyone explain me why? and what should I do to fix it?

  • 2
    You probably need `ax = plt.gca()` ("get current ax") instead of `ax = plt.axes()`. – JohanC Sep 16 '21 at 16:18
  • `ax = plt.axes()` creates axes. When you do this AFTER plotting something, it creates the second axes, on top of the axes used to `plot(x, E)`. You don't need this, you only need to get the handle to the axes you have already got (using ax = plt.gca()), then you add your ticks and labels, and that will do the job :) – Yulia V Sep 16 '21 at 18:29

0 Answers0