[英]How can i use Plotly express to make a 1D histogram without making new x values for each row of the same value?
This is how my plot actually is: current output这就是我的 plot 实际上是这样的:当前 output
This is the Data Frame: DataFrame这是数据框: DataFrame
This is my current code:这是我当前的代码:
import pandas as pd
import plotly.express as px
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
df = pd.read_csv('sales.csv')
df.columns = df.columns.str.replace(' ', '')
fig= px.bar(df, x='number', y='number', color='profit')
fig.show()
As you can see, the sales numbers are many times the same, so i want to plot a histogram being each x value the total profit of all sales with the same key number, so i can compare the profit of each sales key number.如您所见,销售数字多次相同,所以我想要 plot 一个直方图,每个 x 值是具有相同键号的所有销售的总利润,因此我可以比较每个销售键号的利润。
How can i do that using Pandas and plotly express?我如何使用 Pandas 和 plotly 快递做到这一点?
ps: I'm a real noobie with all this ps:我是一个真正的菜鸟
df.dtypes
检查df.dtypes
import numpy as np
import pandas as pd
import plotly.express as px
# simulate data frame
df = pd.DataFrame({"number": np.repeat(np.arange(3334, 3389), 6)}).pipe(
lambda d: d.assign(profit=np.random.uniform(-10, 200, len(d)))
)
# question approach
fig = px.bar(df, x="number", y="number", color="profit")
fig.show()
# requested approach
fig = px.bar(
df.groupby("number", as_index=False).agg({"profit": "sum"}), x="number", y="profit"
).update_layout(xaxis={"type": "category"})
fig.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.