为什么尝试对类别进行冒泡时会出现值错误?

问题描述 投票:1回答:1

我正在尝试遵循并学习如何使用图,并且在使用此代码时遇到了问题。我无法绘制出不同的颜色,它只能绘制出一种没有图例的颜色。但是直到代码的图形部分之前,我才在前几行中没有收到错误?我在做什么错?

这里是数据集的链接,它的名称为NewOrders.xlsx

https://drive.google.com/drive/folders/1eEL3dpCk_mfSsvEhME04-qJkWjrpi8Ib

trace0=[go.Scatter(
    x=orders['Sales'][orders['Order Priority']=='High'],
    y=orders['Profit'][orders['Order Priority']=='High'],
    name='High',
    mode='markers',
    marker=dict(size=100*orders['Discount'][orders['Order 
    Priority']=='High']))]

trace1=[go.Scatter(
    x=orders['Sales'][orders['Order Priority']=='Medium'],
    y=orders['Profit'][orders['Order Priority']=='Medium'],
    name='Medium',
    mode='markers',
    marker=dict(size=100*orders['Discount'][orders['Order ` ` 
    Priority']=='Medium']))]

trace2=[go.Scatter(
    x=orders['Sales'][orders['Order Priority']=='Low'],
    y=orders['Profit'][orders['Order Priority']=='Low'],
    name='Low',
    mode='markers',
    marker=dict(size=100*orders['Discount'][orders['Order 
    Priority']=='Low']))]

data=[trace0, trace1, trace2]

layout=go.Layout(
    title='Sales vs Profit',
    xaxis=dict(title='Sales'),
    yaxis=dict(title='Profit'),
    hovermode='closest')

figure=go.Figure(data=data, layout=layout) 

错误:ValueError追溯(最近一次通话)

in----> 1个图形= go.Figure(data = data,layout = layout)

python plotly graphing
1个回答
1
投票

在这种情况下,您可以使用plotly.express

plotly.express

import pandas as pd
import plotly.express as px

df = pd.read_excel("NewOrders.xlsx")
ddf = df[df["Order Priority"].isin(["High", "Medium", "Low"])]

# you can eventually create a column
# ddf["Discount_100"] = ddf["Discount] * 100

fig = px.scatter(ddf,
                 x="Sales",
                 y="Profit",
                 color="Order Priority",
                 size="Discount")

fig.update_layout(title='Sales vs Profit',
                  title_x=0.5,
                  hovermode="closest")
fig.show()

enter image description here

plotly.graph_objs

import pandas as pd
import plotly.graph_objs as go

df = pd.read_excel("NewOrders.xlsx")

traces = []
colors = ["red", "green", "blue"]
for i, priority in enumerate(["High", "Medium", "Low"]):
    ddf = df[df["Order Priority"]==priority]
    traces.append(
        go.Scatter(x=ddf["Sales"],
                   y=ddf["Profit"],
                   name=priority,
                   mode='markers',
                   marker=dict(size=100*ddf["Discount"],
                               color=colors[i])
                   ))
layout = dict(title='Sales vs Profit',
              title_x=0.5,
              hovermode="closest",
              xaxis=dict(title='Sales'),
              yaxis=dict(title='Profit'))

fig = go.Figure(data=traces, layout=layout)
fig.show()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.