0

I'm trying to simplify the look of a graph of mine. To this extent, I would like to set ticks only on definite points. The 'native' plot, out of a df.groupby.max().plot() operation looks like this:

initial plot

I don't like the fact that my data starts at 0.3 and ends at 0.6, but the graph is somewhat adding real estate there. To have the plot limited to the numbers I want, I do:

ax1.set_xlim(0.3,0.6)

Which however adds a series of intermediate points I wouldn't like to have:

limit adjusted plot

Now, for some reason, halfway points appear. Note that they do not belong to the measured data. I've then tried the recipes found - among other places - here

ax1.set_xticks = np.arange(0.3,0.6,0.1)

--> no change

ax1.xaxis.set_tick_params(which='minor',bottom=False)

--> no change

ax1.minorticks_off()

--> no change

I've run out of options and I'm not sure what I'm doing wrong here, any help appreciated.

DavidG
  • 24,279
  • 14
  • 89
  • 82
Michele Ancis
  • 1,265
  • 4
  • 16
  • 29
  • 1
    Rather than changing the axis limits, just remove the margins `ax1.margins(x=0)`? – DavidG Mar 22 '21 at 14:36
  • 2
    Btw, there are no minor ticks in those plots. If you adjust the axis limits, matplotlib will automatically adjust the ticks. Your attempt at setting the xticks yourself is slightly wrong, it should be `ax1.set_xticks(np.arange(0.3,0.6,0.1))`. Note that you should also change the xtick labels (using `ax1.set_xticklabels(...)`, if changing the xticks manually. – DavidG Mar 22 '21 at 14:45
  • Actually this is interesting: thanks for spotting the error with the form of `ax.set_xticks()` but the 'funny' part is that if I try your syntax, which is coherent with the manual, I get an error: 'numpy.ndarray' object is not callable I guess I'll now have to investigate why that is... – Michele Ancis Mar 22 '21 at 17:02

1 Answers1

1

OK,

thanks to @DavidG's hint I found the issue. It's maybe not subtle but worth mentioning. I can remove the whole thing if this turns out to be too trivial.

The issue was created by this wrong call to the ax.set_xticks() function:

 ax1.set_xticks = np.arange(0.3,0.6,0.1)

Although I cited the place where I took this approach, I actually managed to implement it wrongly. The right way would have been:

ax1.set_xticks(np.arange(0.3,0.6,0.1))

So, actually, I wasn't seeing any change in the plot because I wasn't calling the function correctly. But there's more. My code was actually assigning the (name?) ax1.set_xticks to an np.array, so that when trying to then implement the correct syntax, I kept getting an error:

ax1.set_xticks([0.3,0.4,0.5,0.6])
Traceback (most recent call last):

  File "<ipython-input-93-df3b8935eb28>", line 1, in <module>
    ax1.set_xticks([0.3,0.4,0.5,0.6])

TypeError: 'numpy.ndarray' object is not callable

Even with a simple list, I was getting the error. This is because I had assigned the name ax1.set_xticks to, indeed, an np.array object.

Once reset the variable space and properly called the function, everything ran smoothly.

Michele Ancis
  • 1,265
  • 4
  • 16
  • 29