Saltar al contingut

Plotly

Plotly is an interactive, open-source, and browser-based graphing library for Python

plotly.py is built on top of plotly.js and is a high-level, declarative charting library.

plotly.js ships with over 30 chart types, including scientific charts, 3D graphs, statistical charts, SVG maps, financial charts, and more.

Crea un entorn de treball

Terminal window
uv init plot
cd plot
uv add kaleido plotly

Obre el projecte amb PyCharm

Modifica el fitxer main.py:

Terminal window
import plotly.express as px
fig = px.bar(x=["a", "b", "c"], y=[1, 3, 2])
fig.show()

En executar el fitxer, s’obre el panell Plots:

The plotly Python package exists to create, manipulate and render graphical figures (i.e. charts, plots, maps and diagrams) represented by data structures also referred to as figures.

The rendering process uses the Plotly.js JavaScript library under the hood although Python developers using this module very rarely need to interact with the Javascript library directly, if ever.

Figures can be represented in Python either as dicts or as instances of the plotly.graph_objects.Figure class, and are serialized as text in JavaScript Object Notation (JSON) before being passed to Plotly.js.

Viewing the underlying data structure for any plotly.graph_objects.Figure object can be done via print(fig). print()ing the figure will result in the often-verbose layout.template key being represented as ellipses ’…’ for brevity.

import plotly.express as px
fig = px.bar(x=["a", "b", "c"], y=[1, 3, 2], title="sample figure")
print(fig)
Figure({
'data': [{'hovertemplate': 'x=%{x}<br>y=%{y}<extra></extra>',
'legendgroup': '',
'marker': {'color': '#636efa', 'pattern': {'shape': ''}},
'name': '',
'orientation': 'v',
'showlegend': False,
'textposition': 'auto',
'type': 'bar',
'x': array(['a', 'b', 'c'], dtype=object),
'xaxis': 'x',
'y': {'bdata': 'AQMC', 'dtype': 'i1'},
'yaxis': 'y'}],
'layout': {'barmode': 'relative',
'legend': {'tracegroupgap': 0},
'template': '...',
'title': {'text': 'sample figure'},
'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'title': {'text': 'x'}},
'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title': {'text': 'y'}}}
})

Figures also support fig.to_dict() and fig.to_json() methods.

Figure Data Structure


El contingut d'aquest lloc web té llicència CC BY-NC-ND 4.0.

©2022-2025 xtec.dev