本文介绍了Plotly:如何检查基本图形结构(版本 4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于旧版本的 plotly,例如在 Jupyterlab 中,您可以简单地运行 figure 来检查图形的基础,如下所示:

For older versions of plotly, for example in Jupyterlab, you could simply run figure to inspect the basics of your figure like this:

输出:

{'data': [{'marker': {'color': 'red', 'size': '10', 'symbol': 104},
   'mode': 'markers+lines',
   'name': '1st Trace',
   'text': ['one', 'two', 'three'],
   'type': 'scatter',
   'x': [1, 2, 3],
   'y': [4, 5, 6]}],
 'layout': {'title': 'First Plot',
  'xaxis': {'title': 'x1'},
  'yaxis': {'title': 'x2'}}}

V4 之前版本的代码:

import plotly.plotly as py
import plotly.graph_objs as go

trace1 = go.Scatter(x=[1,2,3], y=[4,5,6], marker={'color': 'red', 'symbol': 104, 'size': "10"},
                    mode="markers+lines",  text=["one","two","three"], name='1st Trace')

data=go.Data([trace1])
layout=go.Layout(title="First Plot", xaxis={'title':'x1'}, yaxis={'title':'x2'})
figure=go.Figure(data=data,layout=layout)
#py.iplot(figure, filename='pyguide_1')
figure

如果你现在用类似的设置做同样的事情,同样的方法不会产生图形基础,而是绘制图形本身:

If you do the same thing now with a similar setup, the same approach will not produce the figure basics, but rather plot the figure itself:

代码:

import pandas as pd
import plotly.graph_objects as go

trace1 = go.Scatter(x=[1,2,3], y=[4,5,6], marker={'color': 'red', 'symbol': 104},
                    mode="markers+lines",  text=["one","two","three"], name='1st Trace')  

figure = go.Figure(data=trace1)  

figure

输出:

在很多方面,这类似于您在 R 中使用 ggplot 构建和绘制图形的方式.由于 plotly 可用于 RPython,我认为这毕竟是有道理的.但我真的很想知道如何访问基本设置.

In many ways this is similar to how you for example would build and plot a figure with ggplot in R. And since plotly is available for both R and Python I thinks this makes sense after all. But I'd really like to know how to access that basic setup.

我的尝试:

我认为这种变化是由于 figure 现在是一个 plotly.graph_objs._figure.Figure 并且曾经是一个字典(?).所以 figure['data']figure['layout'] 仍然是具有必要和有趣内容的字典:

I think this change is due to the fact that figure is now a plotly.graph_objs._figure.Figure and used to be a dictionary(?). So figure['data'] and figure['layout'] are still dicts with necessary and interesting content:

来自figure['data']

(Scatter({
     'marker': {'color': 'red', 'symbol': 104},
     'mode': 'markers+lines',
     'name': '1st Trace',
     'text': [one, two, three],
     'x': [1, 2, 3],
     'y': [4, 5, 6]
 }),)

来自figure['layout']

Layout({
    'template': '...'
})

当然,诸如 help(figure)dir(figure) 之类的选项很有帮助,但会产生非常不同的输出.

And of course options such as help(figure) and dir(figure) are helpful, but produces a very different output.

推荐答案

我刚刚发现忘记"figure.show() 的方括号会给我我正在寻找的东西.因此,使用类似于问题中的代码和 plotly V4 的设置,只需运行 figure.show 即可:

I just found out that 'forgetting' the brackets for figure.show() will give me exactly what I'm looking for. So with a setup similar to the code in the question and with plotly V4, simply running figure.show will give you this:

输出:

<bound method BaseFigure.show of Figure({
    'data': [{'marker': {'color': 'red', 'symbol': 104},
              'mode': 'markers+lines',
              'name': '1st Trace',
              'text': [one, two, three],
              'type': 'scatter',
              'x': [1, 2, 3],
              'y': [4, 5, 6]}],
    'layout': {'template': '...'}
})>

代码:

import pandas as pd
import plotly.graph_objects as go

trace1 = go.Scatter(x=[1,2,3], y=[4,5,6], marker={'color': 'red', 'symbol': 104},
                    mode="markers+lines",  text=["one","two","three"], name='1st Trace')  

figure = go.Figure(data=trace1)  

figure.show

这篇关于Plotly:如何检查基本图形结构(版本 4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 16:28