-
-
Notifications
You must be signed in to change notification settings - Fork 528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Missing scientific labels #34233
Comments
This comment has been minimized.
This comment has been minimized.
comment:2
|
comment:3
The fix is as follows: STEP 1: Add following function to sage.plot.plot.py def CustomScalarFormatter(replace_values=([],[])):
"""
This matplotlib formatter selectively replaces the given tick labels.
Takes a tuple or list of two lists as argument. First list labels will be replaced by second list labels.
Note that the first label entries are cumpulsorily int or float values. No strings.
No such restrictions for the second list. Because they will be anyway converted to strings in the end.
EXAMPLES:
::
sage: from sage.plot.plot import CustomScalarFormatter
sage: import matplotlib.pyplot as plt
sage: import numpy as np
sage: z = np.linspace(0, 5000, 100)
sage: fig, ax = plt.subplots()
sage: xmajorformatter = CustomScalarFormatter(replace_values=([2000,0],['$x_0$','']))
sage: ymajorformatter = CustomScalarFormatter(replace_values=([1E7,0],['$y_0$','']))
sage: ax.xaxis.set_major_formatter(xmajorformatter)
sage: ax.yaxis.set_major_formatter(ymajorformatter)
sage: ax.plot(z,z**2)
sage: plt.show()
::
sage: from sage.plot.plot import CustomScalarFormatter
sage: from matplotlib import font_manager
sage: plot(x^2, (x,100,5000), tick_formatter = [ CustomScalarFormatter(replace_values=[[2000,0],['$x_0$','']]), CustomScalarFormatter(replace_values=[[1E7,0],['$y_0$','']]) ])
"""
from matplotlib.ticker import ScalarFormatter
class _CustomScalarFormatter(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)
return _CustomScalarFormatter(replace_values=replace_values) I have verified the examples in docstring but check once the formatting. STEP 2: Replace following lines in sage.plot.graphics.py from sage.plot.plot import SelectiveFormatter
subplot.yaxis.set_major_formatter(SelectiveFormatter(
subplot.yaxis.get_major_formatter(), skip_values=[0]))
subplot.xaxis.set_major_formatter(SelectiveFormatter(
subplot.xaxis.get_major_formatter(), skip_values=[0])) by from sage.plot.plot import CustomScalarFormatter
subplot.yaxis.set_major_formatter(CustomScalarFormatter(replace_values=([0],[''])))
subplot.xaxis.set_major_formatter(CustomScalarFormatter(replace_values=([0],['']))) STEP 3: (Optional) To have nice display of scientific label as x107 instead of 1e7 (say for example) by default add following line in sage.plot.graphics.py rcParams['axes.formatter.use_mathtext'] = True just after from matplotlib import rcParams in function def matplotlib(self, filename=None, Very sorry, now I am far from setting up and working with sage git due to my upcoming exams and also since i am new to git and i have to explore and learn some more about git. Please somebody take up this task of getting these into sage. |
comment:5
FWIW, a new report for this bug: |
I thought it's a problem of plotting near a singular point, but no, with
That's on cocalc - not sure why I couldn't do a screenshot with Firefox, or save an image, so this is a photo of the screen. |
I can confirm that #34233 (comment) provides a fix. |
Fix is provided in a comment by @niranjankm (Niranjana K.M.)
Fix is provided in a comment by @niranjankm (Niranjana K.M.)
also, adjust one other example to show customized large-scale y-axis
#37502 does fix this as such, though see the related problems it causes in its current version (likely quite fixable). |
Fix is provided in a comment by @niranjankm (Niranjana K.M.)
also, adjust one other example to show customized large-scale y-axis
Fix is provided in a comment by @niranjankm (Niranjana K.M.)
also, adjust one other example to show customized large-scale y-axis
Fix is provided in a comment by @niranjankm (Niranjana K.M.)
also, adjust one other example to show customized large-scale y-axis
Fix is provided in a comment by @niranjankm (Niranjana K.M.)
also, adjust one other example to show customized large-scale y-axis
See this sage-devel discussion for the problem. Compare
gh-niranjankm has identified the problem as the use of
SelectiveFormatter
, in particularskip_values=[0]
Related:
CC: @niranjankm
Component: graphics
Issue created by migration from https://trac.sagemath.org/ticket/34233
The text was updated successfully, but these errors were encountered: