Matplotlib: setting x-limits also forces tick marks?

I just upgraded to matplotlib 2.0, and it seems to me that I'm on crazy pills. I am trying to make a log line chart, with the y axis in linear scale and the x axis in log10 scale. Previously, the following code allowed me to pinpoint where I want my ticks and what I want their labels to be:

import matplotlib.pyplot as plt

plt.plot([0.0,5.0], [1.0, 1.0], '--', color='k', zorder=1, lw=2)

plt.xlim(0.4,2.0)
plt.ylim(0.0,2.0)

plt.xscale('log')

plt.tick_params(axis='x',which='minor',bottom='off',top='off')

xticks = [0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0]
ticklabels = ['0.4', '0.6', '0.8', '1.0', '1.2', '1.4', '1.6', '1.8', '2.0']
plt.xticks(xticks, ticklabels)

plt.show()

But in matplotlib 2.0, this now forces me to get a set of overlapping shortcut labels, where matplotlib apparently wants to automatically create ticks:

enter image description here

But if I comment on the line "plt.xlim (0.4.2.0)" and let it automatically determine the axis limits, there are no overlapping label labels, and I just get the ones I want:

enter image description here

But this does not work, because now I have useless limitations on the x axis.

Any ideas?

: , , , matplotlib. v. 1.5.3. .

+4
1

, , , . , NullFormatter:

plt.gca().xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

:

import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np

x = np.linspace(0,2.5)
y = np.sin(x*6)
plt.plot(x,y, '--', color='k', zorder=1, lw=2)

plt.xlim(0.4,2.0)

plt.xscale('log')

xticks = [0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0]
ticklabels = ['0.4', '0.6', '0.8', '1.0', '1.2', '1.4', '1.6', '1.8', '2.0']
plt.xticks(xticks, ticklabels)

plt.gca().xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

plt.show()

enter image description here

, , xticklabels , , FixedLocator a ScalarFormatter.
, .

import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np

x = np.linspace(0,2.5)
y = np.sin(x*6)
plt.plot(x,y, '--', color='k', zorder=1, lw=2)

plt.xlim(0.4,2.0)
plt.xscale('log')

xticks = [0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0]

xmajorLocator = matplotlib.ticker.FixedLocator(locs=xticks) 
xmajorFormatter = matplotlib.ticker.ScalarFormatter()
plt.gca().xaxis.set_major_locator( xmajorLocator )
plt.gca().xaxis.set_major_formatter( xmajorFormatter )
plt.gca().xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

plt.show()
+3

Source: https://habr.com/ru/post/1672505/


All Articles