Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用plotly字典在同一图形上绘制两条直线_Python_Dictionary_Plotly - Fatal编程技术网

Python 使用plotly字典在同一图形上绘制两条直线

Python 使用plotly字典在同一图形上绘制两条直线,python,dictionary,plotly,Python,Dictionary,Plotly,我的目标是在同一个绘图上显示一个折线图和一条平均线。这适用于Django项目,该项目显示数据库中的分数列表。这个数据库相当大(超过10万)。该应用程序显示每个页面的平均行数(页面仅包含20个结果)。我使用分散对象而不是图形对象进行此工作 plot_div = plot( [Scatter(x=x_data, y=y_data) , Scatter(x=x_data, y=[statistics.fmean(y_data_float)]*20) ],

我的目标是在同一个绘图上显示一个折线图和一条平均线。这适用于Django项目,该项目显示数据库中的分数列表。这个数据库相当大(超过10万)。该应用程序显示每个页面的平均行数(页面仅包含20个结果)。我使用分散对象而不是图形对象进行此工作

    plot_div = plot(
                    [Scatter(x=x_data, y=y_data) , Scatter(x=x_data, y=[statistics.fmean(y_data_float)]*20) ],
                    output_type='div', 
                    auto_open=False,
                    show_link=False,
                    config=dict(
                        displayModeBar=False,
                        )
                    
                    )
另外,我正在使用
从plotly.offline导入绘图

从plotly.graph\u objs导入散布
这在最新版本中不再推荐,但目前这是我发现的将绘图输出设置为div的最简单方法(而不是使用不适用于我的用例的
fig.show()


我对这个工作解决方案不满意的原因是,即使我成功地关闭了顶部模式栏,缩放功能仍然处于选中状态,我不希望用户能够缩放。我已经在一个移动应用程序上试用过了,缩放使绘图无法使用。但我仍然希望计算机上的用户能够使用悬停功能(这就是为什么我不想做静态绘图)。我找到了一个解决方案,可以使用一些地物布局选项,并在
绘图中使用一个地物而不是两个散布对象

fig = dict({
    "data": [{"type": "scatter",
              "x": x_data,
              "y": y_data}] ,
    "layout": {"title": {"text": "Game Scores"},
                "xaxis_fixedrange": True,
                "yaxis_fixedrange": True}
    })

plot_div = plot(
                fig,
                output_type='div', 
                auto_open=False,
                show_link=False,
                config=dict(
                    displayModeBar=False,
                )
                    
            )

现在,缩放功能已被禁用,但我在获取同一绘图上的平均线时遇到问题。我知道我不能像使用散点对象那样使用两个图形对象的数组(我只是想确保:()。下面是plotly在使用字典创建图形文档中的完整部分

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)
我尝试更新fig代码,使其上有两行代码

fig = dict({
    "data": [[{"type": "scatter","x": x_data,"y": y_data}] , [{"type": "scatter","x": x_data,"y": [5]*20}]] ,
    "layout": {"title": {"text": "Game Scores"},
                "xaxis_fixedrange": True,
                "yaxis_fixedrange": True}
    })
我不喜欢这样,这里是错误

Invalid element(s) received for the 'data' property of
        Invalid elements include: [[{'type': 'scatter', 'x': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 'y': ['212.124', '1.00', '14.2', '7.43', '21.21', '22.53', '4.24', '1.07', '1.01', '1.97', '1.41', '92.46', '1.09', '4.94', '1.0', '28.4', '16.05', '25.13', '1.2', '2.9']}], [{'type': 'scatter', 'x': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 'y': [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]}]]

    The 'data' property is a tuple of trace instances
    that may be specified as:
      - A list or tuple of trace instances
        (e.g. [Scatter(...), Bar(...)])
      - A single trace instance
        (e.g. Scatter(...), Bar(...), etc.)
      - A list or tuple of dicts of string/value properties where:
        - The 'type' property specifies the trace type
            One of: ['area', 'bar', 'barpolar', 'box',
                     'candlestick', 'carpet', 'choropleth',
                     'choroplethmapbox', 'cone', 'contour',
                     'contourcarpet', 'densitymapbox', 'funnel',
                     'funnelarea', 'heatmap', 'heatmapgl',
                     'histogram', 'histogram2d',
                     'histogram2dcontour', 'image', 'indicator',
                     'isosurface', 'mesh3d', 'ohlc', 'parcats',
                     'parcoords', 'pie', 'pointcloud', 'sankey',
                     'scatter', 'scatter3d', 'scattercarpet',
                     'scattergeo', 'scattergl', 'scattermapbox',
                     'scatterpolar', 'scatterpolargl',
                     'scatterternary', 'splom', 'streamtube',
                     'sunburst', 'surface', 'table', 'treemap',
                     'violin', 'volume', 'waterfall']

如果我正确理解了您想要完成的任务,那么看起来您可以将另一个跟踪实例(如
Scatter(…)
)传递到
data
属性,并且它将在禁用缩放功能的情况下显示这两个跟踪

from plotly.offline import plot
from plotly.graph_objs import Scatter

fig = dict({
    "data": [{"type": "scatter",
              "x": [1, 2, 3],
              "y": [1, 3, 2]},
              {"type": "scatter",
              "x": [1, 2, 3],
              "y": [5, 5, 5]}],
    "layout": {"title": {"text": "Game Scores"},
                "xaxis_fixedrange": True,
                "yaxis_fixedrange": True}
    })

# To display the figure defined by this dict, use the low-level plotly.io.show function
import plotly.io as pio

pio.show(fig)

这奏效了,现在我觉得有点傻。Plotly有点挑剔,所以可以理解——很高兴能帮上忙:)