【问题标题】:Choosing graph types from dropdown从下拉列表中选择图表类型
【发布时间】:2021-05-20 19:27:59
【问题描述】:

假设我有某些数据。我的要求是,我应该能够选择我想要的图表类型。例如,如果我有一个由 1 和 0 组成的数组,我希望能够选择我是否为否。 1 和 0 的数量显示为条形图或饼图中的百分比。是否有一些具有此功能的库?如果没有,我有什么办法可以做到这一点?

【问题讨论】:

  • 您好@user_9,我认为这个问题非常广泛。你有偏好的语言吗?您是要在网站中显示这些数据,还是作为应用显示?
  • 您好@user_9,如果我的回答对您有帮助,请给我赏金

标签: plot graph data-visualization


【解决方案1】:

我想我有你正在寻找的答案。我的大学有同样的项目,我在 python 中使用了 plotly 库,它可以让你制作你想要的东西。

import plotly.graph_objects as go

import pandas as pd

# load dataset
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/volcano.csv")

# create figure
fig = go.Figure()

# Add surface trace
fig.add_trace(go.Surface(z=df.values.tolist(), colorscale="Viridis"))

# Update plot sizing
fig.update_layout(
    width=800,
    height=900,
    autosize=False,
    margin=dict(t=0, b=0, l=0, r=0),
    template="plotly_white",
)

# Update 3D scene options
fig.update_scenes(
    aspectratio=dict(x=1, y=1, z=0.7),
    aspectmode="manual"
)

# Add dropdown
fig.update_layout(
    updatemenus=[
        dict(
            buttons=list([
                dict(
                    args=["type", "surface"],
                    label="3D Surface",
                    method="restyle"
                ),
                dict(
                    args=["type", "heatmap"],
                    label="Heatmap",
                    method="restyle"
                )
            ]),
            direction="down",
            pad={"r": 10, "t": 10},
            showactive=True,
            x=0.1,
            xanchor="left",
            y=1.1,
            yanchor="top"
        ),
    ]
)

# Add annotation
fig.update_layout(
    annotations=[
        dict(text="Trace type:", showarrow=False,
        x=0, y=1.085, yref="paper", align="left")
    ]
)

fig.show()

这里是示例代码

它将按照此链接中所示的方式工作。

this is the output

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-19
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多