Allow to use a non fixed axes formatter

オープン
#1,081 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
52/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
静か
技術スタック
python

調査の方向性

まず plotnine/_mpl/ticker.py の固定 formatter と、それが plotnine/facets/facet.py からどのように適用されるかを読みます。ggplot の描画フローと、plot.draw()、plot.show()、および figure が公開する axes の相互作用を追跡します。これにより、ユーザーは AutoLocator や EngFormatter などの Matplotlib の locator と formatter を適用でき、monkey-patching で ggplot を変更せずに、ズーム時に ticks が適切に更新されるようにします。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Python
スター
4.8k
フォーク
260
平均マージ
2日 12時間
マージ済み PR(30日)
4

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

has2k1/plotnine のほかの issue

has2k1/plotnine の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。