Skip to content
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

minorticks cannot be setted in colorbar #95

Closed
cmyangcn opened this issue Feb 22, 2025 · 5 comments
Closed

minorticks cannot be setted in colorbar #95

cmyangcn opened this issue Feb 22, 2025 · 5 comments

Comments

@cmyangcn
Copy link

The minorticks cannot be correctly setted in the colorbar.

m=ax.contourf(x1,t1,z,levels=np.linspace(0,500,200),extend='both')
ax.colorbar(m,ticks=np.arange(0,501,100),minorticks=20)

The minorticks in the plot are not 20, but depend on the "levels".

@cvanelteren
Copy link
Contributor

Hi there, thanks for reporting. Please provide a minimal reproducible example -- it is hard to tell what the intentions are based on the snippets.

I had a quick peek at the code to see what's going on. The levels will set the space of colors that you can extract from the contourf whereas the ticker on the colorbar will subdivide this space. For example

import ultraplot as plt
import numpy as np


x = np.random.rand(10, 10)
fig, ax = plt.subplots()
ticks = np.linspace(0, 10)
y = np.array([0, 2, 10])
m = ax.contourf(x, x, x, levels=y, extend="both")
cb = ax.colorbar(m)
fig.show()

will produce a plot in which the levels are indicated by y which will generate a colorbar with 3 levels.

If I would then set the ticks, it will attempt to fill the range set by levels, e.g.

x = np.random.rand(10, 10)
fig, ax = plt.subplots()
ticks = np.linspace(0, 10)
y = np.array([0, 2, 10])
m = ax.contourf(x, x, x, levels=y, extend="both")
cb = ax.colorbar(m, ticks = ticks)
fig.show()

This is behaving a it should I think, but I could be misinterpreting what you mean.

@cmyangcn
Copy link
Author

Thank you! Take the demo in the documentation as an example (https://ultraplot.readthedocs.io/en/latest/colorbars_legends.html#Figure-wide-colorbars-and-legends).

import ultraplot as uplt
import numpy as np

state = np.random.RandomState(51423)
fig, axs = uplt.subplots(ncols=3, nrows=3, refwidth=1.4)
for ax in axs:
    m = ax.pcolormesh(
        state.rand(20, 20), cmap="grays", levels=np.linspace(0, 1, 11), extend="both"
    )
fig.format(
    suptitle="Figure colorbars and legends demo",
    abc="a.",
    abcloc="l",
    xlabel="xlabel",
    ylabel="ylabel",
)
#fig.colorbar(m, label="column 1", ticks=0.5, loc="b", col=1)
#fig.colorbar(m, label="columns 2 and 3", ticks=0.2, loc="b", cols=(2, 3))
fig.colorbar(m, label="stacked colorbar", ticks=0.1, loc="b", minorticks=0.05)
#fig.colorbar(m, label="colorbar with length <1", ticks=0.1, loc="r", length=0.7)

The minorticks in the colorbar of the figure is not 0.05, but 0.1, as showing in the following image. When I use proplot, it's correct.
Image

@cvanelteren
Copy link
Contributor

Can you checkout the PR

pip install git+https://github.com/cvanelteren/ultraplot.git@locator-fix

@cmyangcn
Copy link
Author

The changes in the locator-fix branch resolve the issue effectively.

@cvanelteren
Copy link
Contributor

cvanelteren commented Feb 23, 2025

Fixed in main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants