Hướng dẫn dùng go.scatter documentation python
The Show
Figures As Dictionaries¶At a low level, figures can be represented as dictionaries and displayed using functions from the In [1]: fig = dict({ "data": [{"type": "bar", "x": [1, 2, 3], "y": [1, 3, 2]}], "layout": {"title": {"text": "A Figure Specified By Python Dictionary"}} }) # To display the figure defined by this dict, use the low-level plotly.io.show function import plotly.io as pio pio.show(fig) Figures as Graph Objects¶The
Graph objects have several benefits compared to plain Python dictionaries.
Below you can find an example of one way that the figure in the example above could be specified using a graph object instead of a dictionary. In [2]: import plotly.graph_objects as go fig = go.Figure( data=[go.Bar(x=[1, 2, 3], y=[1, 3, 2])], layout=go.Layout( title=go.layout.Title(text="A Figure Specified By A Graph Object") ) ) fig.show() You can also create a graph object figure from a dictionary representation by passing the dictionary to the In [3]: import plotly.graph_objects as go dict_of_fig = dict({ "data": [{"type": "bar", "x": [1, 2, 3], "y": [1, 3, 2]}], "layout": {"title": {"text": "A Figure Specified By A Graph Object With A Dictionary"}} }) fig = go.Figure(dict_of_fig) fig.show() Converting Graph Objects To Dictionaries and JSON¶Graph objects can be turned into their Python dictionary representation using the
In [4]: import plotly.graph_objects as go fig = go.Figure( data=[go.Bar(x=[1, 2, 3], y=[1, 3, 2])], layout=go.Layout(height=600, width=800) ) fig.layout.template = None # to slim down the output print("Dictionary Representation of A Graph Object:\n\n" + str(fig.to_dict())) print("\n\n") print("JSON Representation of A Graph Object:\n\n" + str(fig.to_json())) print("\n\n") Dictionary Representation of A Graph Object: {'data': [{'x': [1, 2, 3], 'y': [1, 3, 2], 'type': 'bar'}], 'layout': {'height': 600, 'width': 800}} JSON Representation of A Graph Object: {"data":[{"x":[1,2,3],"y":[1,3,2],"type":"bar"}],"layout":{"height":600,"width":800}} Representing Figures in Dash¶Dash is the best way to build analytical apps in Python using Plotly figures. To run the app below, run Get started with the official Dash docs and learn how to effortlessly style & deploy apps like this with Dash Enterprise. Creating Figures¶This section summarizes several ways to create new graph object figures with the
Plotly Express¶Plotly Express (included as the In [6]: import plotly.express as px df = px.data.iris() fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", title="A Plotly Express Figure") # If you print the figure, you'll see that it's just a regular figure with data and layout # print(fig) fig.show() Graph Objects
|