简体   繁体   English

我如何使用 Plotly express 制作一维直方图而不为每一行的相同值创建新的 x 值?

[英]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:我是一个真正的菜鸟

  • there is something up with your data frame.您的数据框有问题。 Have simulated it and used your code to generate figure.已经模拟它并使用您的代码生成图形。 It generates a very different figure (see first image below)它产生了一个非常不同的数字(见下面的第一张图片)
    • I suspect there is something up with your dataframe, number columns are not numbers but strings.我怀疑您的 dataframe 有问题,数字列不是数字而是字符串。 check df.dtypes检查df.dtypes
    • this is implied by xaxis being unordered and the fact color does not generate a color bar, but uses sequential colors这暗示 xaxis 是无序的,事实颜色不会生成颜色条,但使用顺序 colors
  • have demonstrated approach to getting total profit by sales number.已经展示了通过销售数量获得总利润的方法。 Aggregate first with pandas then plot number as xaxis and profit as yaxis首先聚合pandas然后 plot数字作为 x 轴,利润作为 ya 轴
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.

相关问题 在一个 Plotly 直方图中,如何使每个 animation 框架在 dataframe 中成为一行? - In a Plotly histogram, how to make each animation frame a row in the dataframe? 如何使用 Python 在 Plotly 中制作水平直方图? - How do I make a horizontal histogram in Plotly express using Python? 我怎样才能用sklearn做一维高斯混合的直方图? - How can I do a histogram with 1D gaussian mixture with sklearn? Plotly:如何制作 3D 堆叠直方图? - Plotly: How to make a 3D stacked histogram? 如何在不使用 numpy 直方图的情况下制作直方图 - How can I make histogram without using numpy histogram 如何在 plotly.express 直方图中增加 x 刻度 - How to increase x ticks in plotly.express histogram 如何在不制作任何中间副本的情况下将多维h5py数据集复制到平面1D Python列表? - How can I copy a multidimensional h5py dataset to a flat 1D Python list without making any intermediate copies? 如何在给定每行一维索引的二维 numpy 数组中设置值? - How to set values in a 2d numpy array given 1D indices for each row? 如何在新行中制作每个数据 [data to csv]? - How can I make each data in new row [data to csv]? 如何根据 bin 的 x 值将颜色图应用于绘图直方图? - How do I apply a color map to a plotly histogram based on a bin's x-values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM