didie
didie

Reputation: 11

add, change or delete draw and save chart plotly

In this example i try to add shape and save plot with configuration button

enter image description here in this image :

  1. I can draw (rectangle,line,...), I can change its shape, but i can't delete it?
  2. I can't save the plot

Code :

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout
from PyQt5.QtWebEngineWidgets import QWebEngineView
import plotly.graph_objects as go
import plotly.io as pio

I create a pyqt window

class PlotApp(QWidget):
def __init__(self):
    super().__init__()
    self.initUI()

def initUI(self):
    self.setWindowTitle('Dessin et suppression avec eraseshape')
    self.setGeometry(100, 100, 800, 600)

    # Layout principal
    self.layout = QVBoxLayout()

    # Vue du graphique Plotly
    self.plot_view = QWebEngineView()
    self.layout.addWidget(self.plot_view)

    # Définir le layout de la fenêtre
    self.setLayout(self.layout)

    # Créer et afficher le graphique
    self.create_plot()

def create_plot(self):
    # Créer un graphique vide
    fig = go.Figure()

    fig.add_trace(go.Scatter(x=[1, 2, 3, 4, 5], y=[1, 3, 2, 4, 5], mode='markers'))


    # Configuration des outils interactifs
    config = {
        'modeBarButtonsToAdd': [
            'drawline',
            'drawopenpath',
            'drawclosedpath',
            'drawcircle',
            'drawrect',
            'eraseshape'
        ],
        'editable': True,  # Permet d'éditer et de supprimer
        'displayModeBar': True  # Affiche la barre d'outils
    }

    # Mise à jour des axes et des dimensions du graphique
    fig.update_layout(
        title='Test de dessin et suppression avec eraseshape',
        xaxis=dict(range=[0, 10]),
        yaxis=dict(range=[0, 10]),
        height=600,
    )

    # Convertir le graphique Plotly en HTML
    html = pio.to_html(fig, full_html=False, include_plotlyjs='cdn', config=config)

    # Charger le graphique dans la vue QWebEngineView
    self.plot_view.setHtml(html)


if __name__ == '__main__':
# Lancer l'application PyQt
    app = QApplication(sys.argv)
    ex = PlotApp()
    ex.show()
    sys.exit(app.exec_())

I need to display a plot in pyqt5 windows, so I can't display a plot in another window

Upvotes: 0

Views: 33

Answers (0)

Related Questions