生成一个 pandas 数据框的直方图,其中列是各个箱子。

huangapple go评论56阅读模式
英文:

Make a histogram for a pandas dataframe where the columns are the individual bins

问题

我有一个数据框,看起来像这样:

蓝色 绿色 红色
1.0 27.0 30.0
24.0 3.0 22.0
3.0 3.0 2.0
5.0 0.0 5.0
2.0 5.0 6.0
10.0 20.0 7.0

我想制作一个直方图,其中每个列名都作为x轴上的条形的箱,而数字的总和作为条形的高度。我该如何做到这一点,因为我看到的大多数可视化库示例都没有使用列本身作为箱。任何可视化库都可以。

英文:

I have a dataframe that looks like the following:

Blue Green Red
1.0 27.0 30.0
24.0 3.0 22.0
3.0 3.0 2.0
5.0 0.0 5.0
2.0 5.0 6.0
10.0 20.0 7.0

I want to make a histogram with each of the column names in as the bins on the x-axis, and the summed values of the numbers as the bar heights. How can I do this, as most of the visualization library examples I've seen have not used the columns themselves as the bins. Any visualization library is fine.

答案1

得分: 2

根据您的描述,我认为您需要一个通用的条形图,可以像这样创建:

import matplotlib.pyplot as plt
import pandas as pd

# 示例数据框
df = pd.DataFrame({'Blue': [1, 2, 3], 'Green': [2, 3, 4], 'Red': [3, 4, 5]})

# 计算每列的总和
column_sums = df.sum()

# 绘制条形图
column_sums.plot(kind='bar', color=['blue', 'green', 'red'])
plt.show()

生成一个 pandas 数据框的直方图,其中列是各个箱子。

英文:

Based on your description, I think you need a generic bar chart which can be created like this

import matplotlib.pyplot as plt
import pandas as pd

# Example dataframe
df = pd.DataFrame({'Blue': [1, 2, 3], 'Green': [2, 3, 4], 'Red': [3, 4, 5]})

# Calculate the sum of each column
column_sums = df.sum()

# Plot the bar chart
column_sums.plot(kind='bar', color=['blue', 'green', 'red'])
plt.show()

生成一个 pandas 数据框的直方图,其中列是各个箱子。

huangapple
  • 本文由 发表于 2023年2月6日 09:05:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定