【问题标题】:Is there a simple way to plot a multiple bar graph using iplot?有没有一种简单的方法可以使用 iplot 绘制多个条形图?
【发布时间】:2020-08-27 19:21:45
【问题描述】:

我有一个看起来像这样的数据集,我需要使用位置作为颜色,单词作为 x 轴。

country   good   amazing    best
    Aus     12        45      12
   Fiji     25         5      23
    USA     45         5      12
     UK     88       258      18

理想情况下,它应该是这样的:

我尝试了以下代码:

positive.iplot(kind = 'bar', title = 'Frequency of Positive Words per Country', y = 'Location', x = ['good', 'amazing', 'best'])

【问题讨论】:

标签: pandas matplotlib ggbiplot


【解决方案1】:

要生成您想要的分组条形图,每个国家/地区及其值都应该有自己的列,因此您需要在某个时候转置您的 df。

同样在您的示例图片中,条形的高度似乎与您的 df 中的值不匹配,但我假设这只是一张显示您要创建的条形图类型的图片。

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'country': ["Aus","Fiji","USA","UK"], 
    'good': [12,25,45,88], 
    'amazing': [45,5,5,258],
    'best':[12,23,12,18]})

df_values = df[['good','amazing','best']]
df_values.index = df['country']

# transpose the dataframe so that each country and its values have its own column
ax = df_values.T.plot.bar(rot=0)
plt.show()

【讨论】:

    猜你喜欢
    • 2021-04-03
    • 2010-09-10
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多