英文:
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()
英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论