3

I'm trying to render a Plotly graph in PyCharm's SciView-Plots panel (using PyCharm Professional 2020.3).

A simplified version of what I'm running is:

import plotly.express as px
import plotly.io as pio

pio.renderers.default = 'png'

fig = px.scatter(data)
fig.show()

This code runs, but does not show a plot. Instead I see a printout in the console that looks like:

{'image/png': 'iVBORw0KGgoAAAANSUh<cropped for brevity...>'}

I was working on following this article, but the solutions there don't seem to work: how can I see plotly graphs in pycharm?

David Parks
  • 30,789
  • 47
  • 185
  • 328
  • plt.figure() fig = px.scatter(data) plt.show() Give this a try? I sometimes had issues with PyCharm when using fig.show(), not plt.show() – flow_me_over Jan 28 '21 at 15:05

1 Answers1

3

Sciview will support matplotlib.pyplot.show, therefore you can export to png, then display the png using matplotlib.pyplot.show

import io
import numpy as np
import plotly.express as px
import plotly.io as pio
from matplotlib import pyplot as plt
import matplotlib.image as mpimg

pio.renderers.default = 'png'


def plot(fig):
    img_bytes = fig.to_image(format="png")
    fp = io.BytesIO(img_bytes)
    with fp:
        i = mpimg.imread(fp, format='png')
    plt.axis('off')
    plt.imshow(i, interpolation='nearest')
    plt.show()


x = np.arange(0, 5, 0.1)
y = np.sin(x)
fig = px.scatter(x, y)
plot(fig)

works with Pycharm professional 2019.3

Jean Bouvattier
  • 303
  • 3
  • 19