简体   繁体   English

如何在一行中显示所有 plotly 图形?

[英]How can I display all plotly graphs in a single row?

This is my python/flask file.这是我的 python/flask 文件。 I have created two graphs one bar chart and one pie chart.我创建了两张图表,一张条形图和一张饼图。 I just wanted to display my graphs in a single row.我只想在一行中显示我的图表。 How can I achieve that?我怎样才能做到这一点? Please it would be great to receive some help and I have provided as much details as I could.请获得一些帮助会很棒,我已经提供了尽可能多的细节。 Thanks in advance!!提前致谢!!

# x is the topic list and y is the list of total tweets based on every topic
trace1 = go.Bar(x=x, y=y)
        layout = go.Layout(title="Overall Tweet Count based on the topics", xaxis=dict(title="Topics",),
                           yaxis=dict(title="Tweet Count",), )
data = [trace1]
fig = go.Figure(data=data, layout=layout)
fig_json = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
x=['positive','negative','neutral']
y=[50,70,80]

piel=go.Pie(labels=x,
               values=y,
               hoverinfo='label+value+percent', textinfo='value'
               )
data=[piel]
fig_pie = go.Figure(data=data, layout=layout)
fig_json_pie = json.dumps(fig_pie, cls=plotly.utils.PlotlyJSONEncoder)

return render_template("charts.html",plot=fig_json,plot_pie=fig_json_pie)

This is charts.html这是图表。html

<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>My First Dashboard</title>
    <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>

</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-xs-3">
            <label> Choose the plot type....</label>
            <select class="form-control" id ='first_cat'>
                <option value="Bar">Bar</option>
                <option value="Scatter">Scatter</option>
            </select>
        </div>
        <div class="col-md-6">
            <div class="chart" id="bargraph">
                <script>
                    var graphs = {{plot | safe}};
                    Plotly.plot('bargraph',graphs,{});
                </script>
            </div>
        </div>

        <div class="col-md-6">
            <div class="chart" id="piechart">
                <script>
                    var graphs = {{plot_pie | safe}};
                    Plotly.plot('piechart',graphs,{});
                </script>

            </div>
        </div>
    </div>
</div>
<script src="{{ url_for('static', filename='js/jquery-1.11.1.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/plot.js') }}"></script>
</body>
</html>

Result:-结果:-

在此处输入图像描述

Here you can use make_subplots being carefully to correctly specify subplots-types .在这里,您可以仔细使用make_subplots来正确指定 subplots -types As Example例如

import plotly.graph_objs as go
from plotly.subplots import make_subplots
fig = make_subplots(rows=1, cols=2, specs=[[{"type": "xy"}, {"type":"domain"}]])
fig.add_trace(go.Bar(x=["A", "B", "C"],
                    y=[40,10,30],
                    showlegend=False),
              row=1, col=1)
fig.add_trace(go.Pie(labels=["one", "two"],
                     values=[45,55],
                     domain=dict(x=[0.5, 1.0]),
                     showlegend=False,
                     hoverinfo='label+value+percent', textinfo='value'),
             row=1, col=2)

fig.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在单个浏览器窗口中获取在for循环显示中创建的所有Plotly图? - How can I get all Plotly plots created inside a for loop display in single browser window? 我似乎无法让 plotly 显示多个图表 - I can't seem to get plotly to display multiple graphs 如何使用Tkinter显示两个图形? - How can I display two graphs with Tkinter? 如何使用 plotly express 和 python 将多个图形添加到单个选项卡 - How to add several graphs to a single tab using plotly express with python 如何在同一HTML页面中离线绘制所有Plotly图? - How to make offline plot of all Plotly graphs in the same HTML page? 我可以在 PyQt4 应用程序中嵌入绘图(离线)吗? - Can I embed plotly graphs (offline) in my PyQt4 application? 为什么在 GitHub 上看不到使用 plotly 库创建的图表? - Why can't I see graphs created with the plotly library on GitHub? 如何在 plotly 中向散点图添加一条线? - How can I add a single line to a scatter plot in plotly? 如何在 python 中编写一个函数来在一行中列出每个客户的所有项目? - How can I write a function in python to list in a single row all the items for every customer? 如何在图表中可视化单个值 - How do I visualize single values in plotly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM