plotly / plotly/plotly.py

R ggplotly to Python plotly conversion issues (with beginnings of a possible fix)

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

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

community feature P2
主要言語
Python
スター
18.8k
フォーク
2.8k
平均マージ
16時間 26分
マージ済み PR(30日)
21

説明

Hello Plotly team! I want to make graphs in R’s ggplotly and then run existing Plotly Python code to reformat them. However, I’m getting errors when importing a ggplotly JSON created in R into Plotly in Python.

To fix these errors, I have to resort to either running code to fix them in the JSON (2a below) or setting pio.read_json()’s skip_invalid parameter to true (2b below). , I believe the JSON is valid because 1) The JSON renders correctly in Plotly.js 2) The JSON comes from simple graphs made in ggplotly.

R code:

library(ggplot2)
library(plotly)

# Data
a = data.frame(x = (0:4), y = c(2, 4, 1, 6, 8), group="A")
b = data.frame(x = (0:4), y = c(3, 2, 5, 4, 7), group="B")
data <- rbind(a, b)

# Graph 
fig <- ggplot(data, aes(x, y, color = group)) + geom_line()

#Convert to ggplotly, save as JSON, and export
fig_ggplotly <- ggplotly(fig)
fig_ggplotly_json<-plotly_json(fig_ggplotly, FALSE)
write(fig_ggplotly_json,'fig_ggplotly.json')

2a: Python code - Errors when using json.load( )

import json
import sys

with open(“fig_ggplotly.json", "r") as f:
    fig_ggplotly_json = json.load(f)
print(fig_ggplotly_json)

go.Figure(fig_ggplotly_json) # throws errors; solved with ggplotly_to_plotly.py 

 # Example of invalid parameter:  
fig_ggplotly_json['layout']['font']['family'] # shows value as ‘’. Does not throw error.

2b: Python code - Using pio.read_json() with skip_invalid=True

import json
import sys
import plotly.io as pio

with open("fig_ggplotly.json", "r") as f:
    fig_ggplotly_pio = pio.read_json(f, skip_invalid=True)

# Example of invalid parameter that gets completely eliminated using skip_invalid, causing later issues:   
fig_ggplotly_pio.to_dict()['layout']['font']['family'] #throws KeyError

Skipping elements of the JSON seems to be inviting a loss of fidelity. Indeed, the reformatting code I apply has issues handling some elements due to the skipped invalid parameters. I’ve identified a number of specific elements that are not compatible with Plotly.py and would appreciate it if Plotly.py were to handle this valid input correctly. The ggplotly_to_plotly conversion code, which highlights many of the problem areas for basic graphs, can be found here.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

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

調査の方向性

まず、提供された R ggplotly エクスポートを再現し、Python の pio.read_json() と go.Figure() を使ってインポートします。特定された非互換性について、リンクされている ggplotly_to_plotly.py gist を確認してください。skip_invalid=True を指定せずに互換性のある ggplotly JSON をインポートでき、layout.font.family などのフィールドが保持されれば完了です。

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

評価

技術スタック
python, r
領域
data-visualization
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
35/100

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

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