Allow to use a non fixed axes formatter

Abierto
#1,081 3 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
52/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Tranquilo
Stack tecnológico
python

Línea de trabajo

Empieza leyendo el formatter fijo en plotnine/_mpl/ticker.py y cómo se aplica desde plotnine/facets/facet.py. Traza el flujo de dibujo de ggplot y la interacción entre plot.draw(), plot.show() y los ejes expuestos por la figure. El resultado debe permitir a los usuarios aplicar locators y formatters de Matplotlib, como AutoLocator y EngFormatter, para que los ticks se actualicen adecuadamente al hacer zoom, sin monkey-patching de ggplot.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Enhancement

Hello,

I am a Python user that came from using plain Matplotlib (not an R user switching to Python). I noticed today that if I take e.g this plot:

zoom-out

And zoom in:

zoom-in

The x & y ticks are fixed! I tried to figure out from reading the code whether I can change this, and I encountered this in plotnine's code:

https://github.com/has2k1/plotnine/blob/06653090d05f6cf9ce6fc9f554794e0b106ed274/plotnine/_mpl/ticker.py#L6-L16

Which is used here:

https://github.com/has2k1/plotnine/blob/06653090d05f6cf9ce6fc9f554794e0b106ed274/plotnine/facets/facet.py#L351-L352

I am not sure whether the graphics grammar allows modifying this behavior, but I won't mind doing it via Matplotlib's native functions. However, that turned out to be hard and frustrating, as plot.draw() returns a figure, but I can't modify the axes of it. Here's something I tried:

from matplotlib.ticker import (
    EngFormatter,
    AutoLocator,
)
for ax in plot.draw(show=False).axes:
    ax.xaxis.set_major_locator(AutoLocator())
    ax.yaxis.set_major_locator(AutoLocator())
    ax.xaxis.set_major_formatter(EngFormatter())
    ax.yaxis.set_major_formatter(EngFormatter())
plot.show() # Also tried `matplotlib.pyplot.show()` here too.

And it doesn't show anything. As a side note, this made me wonder: What is the benefit in returning the figure if it cannot be manipulated afterwards with plain-old Matplotlib? This is related of course to:

Anyway, I managed to workaround this with the following ugly monkey-patching of ggplot class:

from matplotlib.ticker import (
    EngFormatter,
    AutoLocator,
)
ggplot_original__draw_breaks_and_labels = ggplot._draw_breaks_and_labels
def ggplot__draw_breaks_and_labels_with_my_ax_manipulation(self):
    ggplot_original__draw_breaks_and_labels(self)
    for ax in self.axs:
        ax.xaxis.set_major_formatter(EngFormatter(unit="Hz"))
        ax.yaxis.set_major_formatter(EngFormatter(unit="Hz"))
        ax.xaxis.set_major_locator(AutoLocator())
        ax.yaxis.set_major_locator(AutoLocator())
ggplot._draw_breaks_and_labels = ggplot__draw_breaks_and_labels_with_my_ax_manipulation

Not sure why the ticks are located in fixed positions just due to the major_formatter set above, but for sure the AutoLocator is needed too.

Lenguaje dominante
Python
Estrellas
4.8k
Forks
260
Merge medio
2 d 12 h
PR fusionados (30 d)
4

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de has2k1/plotnine

Todos los issues de has2k1/plotnine

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.