reflex icon indicating copy to clipboard operation
reflex copied to clipboard

Graphing Support for Matplotlib

Open Alek99 opened this issue 2 years ago • 6 comments

Code for testing:

import reflex as rx
import altair as alt
import numpy as np
import pandas as pd
from bokeh.plotting import figure
import matplotlib.pyplot as plt

#Altair
# Compute x^2 + y^2 across a 2D grid
x, y = np.meshgrid(range(-5, 5), range(-5, 5))
z = x ** 2 + y ** 2

# Convert this grid to columnar data expected by Altair
chart_data = pd.DataFrame(
    np.random.randn(20, 3),
    columns=['a', 'b', 'c'])

chart = alt.Chart(chart_data).mark_circle().encode(
    x='a', y='b', size='c', color='c', tooltip=['a', 'b', 'c'])


#Bokeh
# prepare some data
x = [1, 2, 3, 4, 5]
y = [4, 5, 5, 7, 2]
p = figure(width=350, height=250)
circle = p.circle(x, y, fill_color="red", size=15)


# Matplotlib
np.random.seed(19680801)
fig, ax = plt.subplots()
for color in ['tab:blue', 'tab:orange', 'tab:green']:
    n = 750
    x, y = np.random.rand(2, n)
    scale = 200.0 * np.random.rand(n)
    ax.scatter(x, y, c=color, s=scale, label=color,
               alpha=0.3, edgecolors='none')
ax.legend()
ax.grid(True)

                      
class State(rx.State):
    pass


def index():
    return rx.vstack(
        rx.altair( 
            fig=chart 
        ),
        rx.bokeh(
            fig=p
        ),
        rx.pyplot(
            fig=fig
        ) 
    )
 
 
app = rx.App()
app.add_page(index)
app.compile()     

Alek99 avatar Sep 18 '23 21:09 Alek99

Resolving some dependency conflicts, in the pyproject.toml, but code is ready for review

Alek99 avatar Sep 18 '23 22:09 Alek99

Another thing to consider with this pr is I introduced another dependency to make the matplotlib graphs interactive mpld3. Not in the core dependencies but would be added with matplotlib in th entry except. in matplotlib.py

It is possible to support matplotlib without this but the charts would serialize to images which wouldn't be interactive to the end user. I'm open to going with this approach as well but I found the interactive charts to be a nice touch.

Alek99 avatar Sep 18 '23 23:09 Alek99

This may be a result of flaky ci something with tailwind test failing

Alek99 avatar Sep 24 '23 22:09 Alek99

Hi there,

Thank you for all your work and contributions to Reflex. I am very excited to have Matplotlib support in a future release.

One question, ¿Does events like 'button_press_event' or 'motion_notify_event' will be supported?

LuisOlivaresJ avatar Feb 14 '24 15:02 LuisOlivaresJ

Hi there,

Thank you for all your work and contributions to Reflex. I am very excited to have Matplotlib support in a future release.

One question, ¿Does events like 'button_press_event' or 'motion_notify_event' will be supported?

Hello! Are these matplot lib specific things? I am not too familiar with it

Alek99 avatar Feb 21 '24 01:02 Alek99

Hello,

Here you can see a list of available events. But I am not sure if they are available only when embedding a figure object in a interface toolkit like wxpython, tkinter, qt, gtk, and macosx.

LuisOlivaresJ avatar Feb 21 '24 02:02 LuisOlivaresJ