admin管理员组文章数量:1356585
I have a data frame with products and their variances that I want to plot as a histogram:
import pandas as pd
data = {'product_name': ['link', 'zelda', 'impa', 'rauru', 'saria', 'darunia'], 'variance': [0.95, -0.85, 0.3, 0.2, 0.03, 0.02]}
df = pd.DataFrame(data)
I can use plotly to create a histogram:
px.histogram(df, 'variance')
But I want to create five bins, with custom start and ends (like in the graph below) rather than what plotly defaults to. This seems weirdly difficult to do using plotly express - is there a way to customize the bin sizes at all?
Thanks
I have a data frame with products and their variances that I want to plot as a histogram:
import pandas as pd
data = {'product_name': ['link', 'zelda', 'impa', 'rauru', 'saria', 'darunia'], 'variance': [0.95, -0.85, 0.3, 0.2, 0.03, 0.02]}
df = pd.DataFrame(data)
I can use plotly to create a histogram:
px.histogram(df, 'variance')
But I want to create five bins, with custom start and ends (like in the graph below) rather than what plotly defaults to. This seems weirdly difficult to do using plotly express - is there a way to customize the bin sizes at all?
Thanks
Share Improve this question asked Mar 27 at 21:34 Tyler MooreTyler Moore 2034 silver badges10 bronze badges1 Answer
Reset to default 2A way to deal with this is to use pandas' cut
method to bin the variance
columns into user-defined categories:
bins = [-1, -0.25, -.05, 0.05, 0.25, 1]
names = ['-25%+', '-5 to -25%', '-5 to 5%', '5 to 25%', '25%+']
df['MyCategories'] = pd.cut(df['variance'], bins, labels=names)
You may then create your histogram as follows:
px.histogram(df, 'MyCategories', category_orders=dict(variance = names))
...which returns:
本文标签: pythonPlotly express histogram custom bin sizeStack Overflow
版权声明:本文标题:python - Plotly express histogram custom bin size - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744066862a2585161.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论