-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Issue #14233 - allow users to set a defualt value for ndivs for minor ticks on x and y axes #16762
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
Changes from all commits
8b3df04
72207e8
132a507
bc98435
2e0e6f4
5458309
dec2d8e
dad689e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -775,6 +775,21 @@ def validate_hatch(s): | |
validate_dashlist = _listify_validator(validate_floatlist) | ||
|
||
|
||
def validate_minor_tick_ndivs(n): | ||
""" | ||
Validate the given ndiv parameter for minor ticks. | ||
ndiv can be either the string 'auto' or a non-negative integer. | ||
""" | ||
if isinstance(n, int): | ||
if (0 <= n): | ||
return n | ||
else: | ||
raise RuntimeError(f'Value must be >=0; got {n}') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
elif (n == 'auto'): | ||
return n | ||
else: | ||
raise ValueError("Value must be a non-negative int or 'auto'") | ||
|
||
_prop_validators = { | ||
'color': _listify_validator(validate_color_for_prop_cycle, | ||
allow_stringlist=True), | ||
|
@@ -1287,6 +1302,8 @@ def _convert_validator_spec(key, conv): | |
"xtick.minor.bottom": validate_bool, # draw bottom minor xticks | ||
"xtick.major.top": validate_bool, # draw top major xticks | ||
"xtick.major.bottom": validate_bool, # draw bottom major xticks | ||
"xtick.minor.ndivs": ['auto', validate_minor_tick_ndivs], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this actually works the way you want, does it? |
||
# default number of minor ticks to display between each pair of major ticks | ||
"xtick.labelsize": validate_fontsize, # fontsize of xtick labels | ||
"xtick.direction": validate_string, # direction of xticks | ||
"xtick.alignment": ["center", "right", "left"], | ||
|
@@ -1310,6 +1327,8 @@ def _convert_validator_spec(key, conv): | |
"ytick.major.left": validate_bool, # draw left major yticks | ||
"ytick.major.right": validate_bool, # draw right major yticks | ||
"ytick.labelsize": validate_fontsize, # fontsize of ytick labels | ||
"ytick.minor.ndivs": ['auto', validate_minor_tick_ndivs], | ||
# default number of minor ticks to display between each pair of major ticks | ||
"ytick.direction": validate_string, # direction of yticks | ||
"ytick.alignment": [ | ||
"center", "top", "bottom", "baseline", "center_baseline"], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -191,6 +191,70 @@ def test_additional(self, lim, ref): | |
|
||
assert_almost_equal(ax.yaxis.get_ticklocs(minor=True), ref) | ||
|
||
def test_ndivs_rcParams_auto(self): | ||
# when no parameter is given rcParams should be used | ||
# auto | ||
mpl.rc_context(rc={'xtick.minor.ndivs': 'auto'}) | ||
mpl.rc_context(rc={'ytick.minor.ndivs': 'auto'}) | ||
Comment on lines
+197
to
+198
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be used as a context manager, or it has no effect. Or you can set |
||
fig, ax = plt.subplots() | ||
ax.set_xlim(0, 1.39) | ||
ax.set_ylim(0, 1.39) | ||
ax.xaxis.set_minor_locator(mticker.AutoMinorLocator()) | ||
ax.yaxis.set_minor_locator(mticker.AutoMinorLocator()) | ||
|
||
test_value = np.array([0.05, 0.1, 0.15, 0.25, 0.3, 0.35, 0.45, | ||
0.5, 0.55, 0.65, 0.7, 0.75, 0.85, 0.9, | ||
0.95, 1.05, 1.1, 1.15, 1.25, 1.3, 1.35]) | ||
assert_almost_equal(ax.xaxis.get_ticklocs(minor=True), test_value) | ||
assert_almost_equal(ax.yaxis.get_ticklocs(minor=True), test_value) | ||
|
||
def test_ndivs_rcParams_int(self): | ||
# when no parameter is given rcParams should be used | ||
# non-neg int | ||
mpl.rc_context(rc={'xtick.minor.ndivs': 2}) | ||
mpl.rc_context(rc={'ytick.minor.ndivs': 2}) | ||
Comment on lines
+214
to
+215
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
fig, ax = plt.subplots() | ||
ax.set_xlim(0, 4) | ||
ax.xaxis.set_major_locator(mticker.MultipleLocator(1)) | ||
ax.xaxis.set_minor_locator(mticker.AutoMinorLocator()) | ||
|
||
ax.set_ylim(0, 4) | ||
ax.yaxis.set_major_locator(mticker.MultipleLocator(1)) | ||
ax.yaxis.set_minor_locator(mticker.AutoMinorLocator()) | ||
|
||
test_value = np.array([0.5, 1.5, 2.5, 3.5]) | ||
assert_almost_equal(ax.xaxis.get_ticklocs(minor=True), test_value) | ||
assert_almost_equal(ax.yaxis.get_ticklocs(minor=True), test_value) | ||
|
||
def test_ndivs_auto(self): | ||
# when auto is given set ndivs to either 4 or 5 | ||
fig, ax = plt.subplots() | ||
ax.set_xlim(0, 1.39) | ||
ax.set_ylim(0, 1.39) | ||
ax.xaxis.set_minor_locator(mticker.AutoMinorLocator('auto')) | ||
ax.yaxis.set_minor_locator(mticker.AutoMinorLocator('auto')) | ||
|
||
test_value = np.array([0.05, 0.1, 0.15, 0.25, 0.3, 0.35, 0.45, | ||
0.5, 0.55, 0.65, 0.7, 0.75, 0.85, 0.9, | ||
0.95, 1.05, 1.1, 1.15, 1.25, 1.3, 1.35]) | ||
assert_almost_equal(ax.xaxis.get_ticklocs(minor=True), test_value) | ||
assert_almost_equal(ax.yaxis.get_ticklocs(minor=True), test_value) | ||
|
||
def test_ndivs_int(self): | ||
# when an integer is given, use that integer | ||
fig, ax = plt.subplots() | ||
ax.set_xlim(0, 4) | ||
ax.xaxis.set_major_locator(mticker.MultipleLocator(1)) | ||
ax.xaxis.set_minor_locator(mticker.AutoMinorLocator(2)) | ||
|
||
ax.set_ylim(0, 4) | ||
ax.yaxis.set_major_locator(mticker.MultipleLocator(1)) | ||
ax.yaxis.set_minor_locator(mticker.AutoMinorLocator(2)) | ||
|
||
test_value = np.array([0.5, 1.5, 2.5, 3.5]) | ||
assert_almost_equal(ax.xaxis.get_ticklocs(minor=True), test_value) | ||
assert_almost_equal(ax.yaxis.get_ticklocs(minor=True), test_value) | ||
|
||
|
||
class TestLogLocator: | ||
def test_basic(self): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2911,7 +2911,8 @@ def __init__(self, n=None): | |
major ticks; e.g., n=2 will place a single minor tick midway | ||
between major ticks. | ||
|
||
If *n* is omitted or None, it will be set to 5 or 4. | ||
If *n* is omitted or None, the value stored in rcParams will be used. | ||
If *n* is 'auto', it will be set to 5 or 4. | ||
""" | ||
self.ndivs = n | ||
|
||
|
@@ -2933,6 +2934,12 @@ def __call__(self): | |
return [] | ||
|
||
if self.ndivs is None: | ||
if self.axis.__name__ == 'xaxis': | ||
self.ndivs = mpl.rcParams['xtick.minor.ndivs'] | ||
else: | ||
self.ndivs = mpl.rcParams['ytick.minor.ndivs'] | ||
Comment on lines
+2937
to
+2940
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't work for 3D; it normally uses the x setting for z. |
||
|
||
if self.ndivs == 'auto': | ||
|
||
majorstep_no_exponent = 10 ** (np.log10(majorstep) % 1) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably defer to
validate_int_or_None
and then check that it's non-negative. And also should be private.