Wait, i just solved it!
     It is not the issue with matplotlib but with the construction of 
SelectiveFormatter. 
This formatter construction is incomplete. I have created two new 
customized ScalarFormatters (default formatters used by matplotlib), the 
second one would be a good new addition to sage along with the first one. 
We can have both. I have worked out same situation but in native python 
with matplotlib. Only thing is to adapt it to sage. It won't be hard to do 
that, just need slight adjustments. Please look into the attached two 
example python files.

Inspired from:
https://stackoverflow.com/questions/11244514/modify-tick-label-text    <-- 
Discusses similar issue (see second answer from last)
https://stackoverflow.com/questions/63475934/skipping-certain-values-in-python-with-matplotlib
https://github.com/matplotlib/matplotlib/blob/4f5cacfde9024076d9ab0cb6ad1c9a7cf352d5f9/lib/matplotlib/ticker.py


On Saturday, July 30, 2022 at 12:05:24 AM UTC+5:30 kcrisman wrote:

> Time being solution would be to drop use of  SelectiveFormatter to get 
>> rid of 0 tick label if the axes cross. Let the origin be visible for some 
>> time.
>>
>
> My guess is that this would not be seen as a great fix, but let's put 
> images and other followup on the ticket.
>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sage-devel/3ccaa889-7548-4756-8024-2baa99dce5b6n%40googlegroups.com.
#!/usr/bin/python

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

from matplotlib import rcParams
rcParams['axes.formatter.use_mathtext'] = True

class CustomScalarFormatter(matplotlib.ticker.ScalarFormatter):
    def __init__(self, useOffset=None, useMathText=None, useLocale=None, skip_values=[]):
        super().__init__(useOffset=None, useMathText=None, useLocale=None)
        self.skip_values = skip_values

    def __call__(self, x, pos=None):
        """
        Return the format for tick value *x* at position *pos*.
        """
        if len(self.locs) == 0:
            return ''
        #elif x == 0:
        #    return ''
        elif x in self.skip_values:
            return ''
        else:
            xp = (x - self.offset) / (10. ** self.orderOfMagnitude)
            if abs(xp) < 1e-8:
                xp = 0
            return self._format_maybe_minus_and_locale(self.format, xp)

z = np.linspace(-5000, 5000, 100)

fig, ax = plt.subplots()
ax.plot(z,z**2)


xmajorformatter = CustomScalarFormatter(skip_values=[2000,0])
ymajorformatter = CustomScalarFormatter(skip_values=[1E7,0])
ax.xaxis.set_major_formatter(xmajorformatter)
ax.yaxis.set_major_formatter(ymajorformatter)

plt.show()
#!/usr/bin/python

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

from matplotlib import rcParams
rcParams['axes.formatter.use_mathtext'] = True

class CustomScalarFormatter(matplotlib.ticker.ScalarFormatter):
    def __init__(self, useOffset=None, useMathText=None, useLocale=None, replace_values=[]):
        super().__init__(useOffset=None, useMathText=None, useLocale=None)
        self.replace_values = replace_values

    def __call__(self, x, pos=None):
        """
        Return the format for tick value *x* at position *pos*.
        """
        if len(self.locs) == 0:
            return ''
        #elif x == 0:
        #    return ''
        elif x in self.replace_values[0]:
            idx = self.replace_values[0].index(x)
            return str(self.replace_values[1][idx])
        else:
            xp = (x - self.offset) / (10. ** self.orderOfMagnitude)
            if abs(xp) < 1e-8:
                xp = 0
            return self._format_maybe_minus_and_locale(self.format, xp)



z = np.linspace(0, 5000, 100)
fig, ax = plt.subplots()
ax.plot(z,z**2)


xmajorformatter = CustomScalarFormatter(replace_values=([2000,0],['$x_0$','']))
ymajorformatter = CustomScalarFormatter(replace_values=([1E7,0],['$y_0$','']))
ax.xaxis.set_major_formatter(xmajorformatter)
ax.yaxis.set_major_formatter(ymajorformatter)

plt.show()

Reply via email to