3

I've got a function that creates a log-log plot using Matplotlib.

import matplotlib.pyplot as plt

plt.loglog(xdata, ydata, label="function label...")
# ...more plots
plt.set_xlabel("Frequency (Hz)")
plt.show()

I'd like to have Matplotlib plot one y-axis tick step beyond the highest/lowest data points on the y-axis, so in the plot below I would like the y-axis limits to be set to (1e-9, 1e-6) as the next lowest/highest log steps with the default base of 10 (the arrows point towards these desired limits). While I can do this by computing the next lowest/highest step with the plotted data each time, and set the limits manually, I want to know if it's possible to set some option in e.g. LogLocator or elsewhere to do this automatically, ideally in a way that is independent of the axis scaling (log or linear), or the current LogLocator's base.

plotted function for which I want to expand the y-axis limits

Sean
  • 1,346
  • 13
  • 24
  • A locator takes the limits as input and calculates the location of the ticks. This cannot be inversed (i.e. it cannot take some locations and output limits). So you need to first set the limits, then the locator will tick them automatically. – ImportanceOfBeingErnest Jul 24 '19 at 14:41
  • Hmm, so I guess I need to write my own locator that does what I want? – Sean Jul 24 '19 at 15:09
  • No, you cannot write any locator for that matter. You need to set the limits. – ImportanceOfBeingErnest Jul 24 '19 at 15:10
  • If I don't set the limits, then what does Matplotlib pass to the locator? It must give it something, but it's not the `max(data)` and `min(data)` since as you can see above there are gaps below and above the plotted curves. – Sean Jul 24 '19 at 15:12
  • Locators take the limits of the axes (not of the data) as input. The limits of the axes are by default based on the min and max of the data, possibly adding 5% margin by default (steered by `plt.margins`). – ImportanceOfBeingErnest Jul 24 '19 at 15:15

0 Answers0