Pandas Plotly Express:使用px.bar()打印未堆叠的数据帧时出错

Pandas Plotly Express:使用px.bar()打印未堆叠的数据帧时出错,pandas,plotly,plotly-dash,plotly-python,Pandas,Plotly,Plotly Dash,Plotly Python,我的目标是从Pandas数据框中的两个过滤列创建比率,然后使用Plotly Express使用px.bar()创建条形图。我可以使用Pandas中的baseplot()函数来执行此操作,但不能使用Plotly Express中的px.bar()函数 我遇到的一个问题是,有些列包含重复的值。这导致我不得不做一些体操 以下是我的数据: test_df = pd.DataFrame({'Manufacturer':['Ford', 'Ford', 'Mercedes', 'BMW', 'Ford',

我的目标是从
Pandas
数据框中的两个过滤列创建比率,然后使用
Plotly Express
使用
px.bar()
创建条形图。我可以使用Pandas中的base
plot()
函数来执行此操作,但不能使用
Plotly Express中的
px.bar()
函数

我遇到的一个问题是,有些列包含重复的值。这导致我不得不做一些体操

以下是我的数据:

test_df = pd.DataFrame({'Manufacturer':['Ford', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW'],
                          'Metric':['Orders', 'Orders', 'Orders', 'Orders', 'Orders', 'Orders', 'Orders', 'Sales', 'Sales', 'Sales', 'Sales', 'Sales', 'Sales', 'Warranty', 'Warranty', 'Warranty', 'Warranty', 'Warranty', 'Warranty'],
                          'Sector':['Germany', 'Germany', 'Germany', 'Germany', 'USA', 'USA', 'USA', 'Germany', 'Germany', 'Germany', 'USA', 'USA', 'USA', 'Germany', 'Germany', 'Germany', 'USA', 'USA', 'USA'],
                          'Value':[45000, 70000, 90000, 65000, 40000, 65000, 63000, 2700, 4400, 3400, 3000, 4700, 5700, 1500, 2000, 2500, 1300, 2000, 2450],
                          'City': ['Frankfurt', 'Bremen', 'Berlin', 'Hamburg', 'New York', 'Chicago', 'Los Angeles', 'Dresden', 'Munich', 'Cologne', 'Miami', 'Atlanta', 'Phoenix', 'Nuremberg', 'Dusseldorf', 'Leipzig', 'Houston', 'San Diego', 'San Francisco']
                       })
由于某些重复值,我创建了一个临时表:

temp_table = test_df.reset_index().pivot_table(values = 'Value', index = ['Manufacturer', 'Metric', 'Sector'], aggfunc='sum')
然后,重置索引:

df_new = temp_table.reset_index()
那么

然后,取消堆叠并打印:

temp_frame = s1.div(s2).unstack()
temp_frame.plot(kind='bar')
这非常有效,并使用标准的Pandas
plot()
函数生成以下条形图:

现在,我尝试使用
Plotly Express
中的
px.bar()
函数进行绘图:

px.bar(temp_frame, x='Sector', y='Value', color='Exchange',
                            barmode='group',
                            text='Value',
                            title='Order to Sales Ratio)
此代码导致以下错误消息:

ValueError: Value of 'x' is not the name of a column in 'data_frame'. Expected one of ['Germany', 'USA'] but received: Sector
此错误似乎与中报告的问题有关。但是,我认为我的数据帧配置方式不足以实现由@Laurens Koppenol提出并经@nicolaskruchten验证的“丑陋修复”解决方案

有人能帮我解决这个错误吗,这样我就可以使用
Plotly Express
创建上面的条形图了?


提前谢谢

Plotly版本4.8允许我们这样做

根据文件:

“…Plotly Express现在可以在对应函数的x和y参数方面表现得更像默认后端。有鉴于此,我们正在利用pandas v0.25中引入的新pandas.options.plotting.backend选项,并为pandas plotting提供官方plotly后端。这意味着您可以像往常一样导入熊猫,将绘图后端设置为“plotly”,当您调用df.plot()时,将调用plotly Express,并返回plotly.graph\u objects.Figure对象,以便进行自定义、渲染或传递到仪表板核心组件的图形组件中。”

有关守则如下:

import pandas as pd
pd.options.plotting.backend='plotly'

test_df = pd.DataFrame({'Manufacturer':['Ford', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW'],
                          'Metric':['Orders', 'Orders', 'Orders', 'Orders', 'Orders', 'Orders', 'Orders', 'Sales', 'Sales', 'Sales', 'Sales', 'Sales', 'Sales', 'Warranty', 'Warranty', 'Warranty', 'Warranty', 'Warranty', 'Warranty'],
                          'Sector':['Germany', 'Germany', 'Germany', 'Germany', 'USA', 'USA', 'USA', 'Germany', 'Germany', 'Germany', 'USA', 'USA', 'USA', 'Germany', 'Germany', 'Germany', 'USA', 'USA', 'USA'],
                          'Value':[45000, 70000, 90000, 65000, 40000, 65000, 63000, 2700, 4400, 3400, 3000, 4700, 5700, 1500, 2000, 2500, 1300, 2000, 2450],
                          'City': ['Frankfurt', 'Bremen', 'Berlin', 'Hamburg', 'New York', 'Chicago', 'Los Angeles', 'Dresden', 'Munich', 'Cologne', 'Miami', 'Atlanta', 'Phoenix', 'Nuremberg', 'Dusseldorf', 'Leipzig', 'Houston', 'San Diego', 'San Francisco']
                       })


temp_table = test_df.reset_index().pivot_table(values = 'Value', index = ['Manufacturer', 'Metric', 'Sector'], aggfunc='sum')


df_new = temp_table.reset_index()


s1 = df_new.set_index(['Manufacturer','Sector']).query("Metric=='Orders'").Value
s2 = df_new.set_index(['Manufacturer','Sector']).query("Metric=='Sales'").Value

temp_frame = s1.div(s2).unstack()


fig = temp_frame.plot(kind='bar')
fig.update_layout(barmode='group')
fig.show()

Plotly版本4.8允许我们这样做

根据文件:

“…Plotly Express现在可以在对应函数的x和y参数方面表现得更像默认后端。有鉴于此,我们正在利用pandas v0.25中引入的新pandas.options.plotting.backend选项,并为pandas plotting提供官方plotly后端。这意味着您可以像往常一样导入熊猫,将绘图后端设置为“plotly”,当您调用df.plot()时,将调用plotly Express,并返回plotly.graph\u objects.Figure对象,以便进行自定义、渲染或传递到仪表板核心组件的图形组件中。”

有关守则如下:

import pandas as pd
pd.options.plotting.backend='plotly'

test_df = pd.DataFrame({'Manufacturer':['Ford', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW'],
                          'Metric':['Orders', 'Orders', 'Orders', 'Orders', 'Orders', 'Orders', 'Orders', 'Sales', 'Sales', 'Sales', 'Sales', 'Sales', 'Sales', 'Warranty', 'Warranty', 'Warranty', 'Warranty', 'Warranty', 'Warranty'],
                          'Sector':['Germany', 'Germany', 'Germany', 'Germany', 'USA', 'USA', 'USA', 'Germany', 'Germany', 'Germany', 'USA', 'USA', 'USA', 'Germany', 'Germany', 'Germany', 'USA', 'USA', 'USA'],
                          'Value':[45000, 70000, 90000, 65000, 40000, 65000, 63000, 2700, 4400, 3400, 3000, 4700, 5700, 1500, 2000, 2500, 1300, 2000, 2450],
                          'City': ['Frankfurt', 'Bremen', 'Berlin', 'Hamburg', 'New York', 'Chicago', 'Los Angeles', 'Dresden', 'Munich', 'Cologne', 'Miami', 'Atlanta', 'Phoenix', 'Nuremberg', 'Dusseldorf', 'Leipzig', 'Houston', 'San Diego', 'San Francisco']
                       })


temp_table = test_df.reset_index().pivot_table(values = 'Value', index = ['Manufacturer', 'Metric', 'Sector'], aggfunc='sum')


df_new = temp_table.reset_index()


s1 = df_new.set_index(['Manufacturer','Sector']).query("Metric=='Orders'").Value
s2 = df_new.set_index(['Manufacturer','Sector']).query("Metric=='Sales'").Value

temp_frame = s1.div(s2).unstack()


fig = temp_frame.plot(kind='bar')
fig.update_layout(barmode='group')
fig.show()

您的DataFrame中似乎没有Exchenge列,该列应该是“制造商”列,而不是“交换”。在替换
px.bar(temp\u frame,x='Sector',y='Value',color='Manufacturer',barmode='group',text='Value',title='Order-to-Sales Ratio'时,您会收到相同的错误消息)
dataframe中似乎没有Exchenge列,它应该是“制造商”列,而不是“交换”。在替换
px.bar(temp\u frame,x='Sector',y='Value',color='Manufacturer',barmode='group',text='Value',title='Order-to-Sales Ratio')时,您会收到相同的错误消息。