使用pandas matplotlib显示x轴中的文本描述而不是数字

时间:2018-06-08 17:20:49

标签: python pandas matplotlib data-visualization

我编写的代码将我的数据集显示为条形图。这是我的代码: 我以这种方式从.csv文件中读取了我的数据:

names = ["Clinic Number","Question Text","Answer Text","Answer Date","Class"]
data = pd.read_csv('ADLCI.csv', names = names)

然后

grouped = data.groupby(['Question Text','Answer Text']).size().reset_index(name='counts')

import matplotlib.pyplot as plt
plt.figure()

grouped.plot(kind='bar', title ="Functional Status Count", figsize=(15, 10), legend=True, fontsize=12)
plt.show()

这也是我想要显示为条形图的数据框的结果。

                         Question Text Answer Text  counts
0                          CI function          No     513
1                          CI function         Yes     373
2                             bathing?          No    2827
3                             bathing?         Yes     408
4                            dressing?          No    2824
5                            dressing?         Yes     423
6                              feeding          No    2851
7                              feeding         Yes     160
8                         housekeeping          No    2803
9                         housekeeping         Yes     717
10                      preparing food          No    2604
11                      preparing food         Yes     593
12  responsibility for own medications          No    2793
13  responsibility for own medications         Yes     625
14                            shopping          No      35
15                            shopping         Yes      49
16                           toileting          No    2843
17                           toileting         Yes     239
18                        transferring          No    2834
19                        transferring         Yes     904
20                using transportation          No    2816
21                using transportation         Yes     483

数字的第一列已自动添加,实际上我的数据集中没有。

以下是此代码创建的条形图。 enter image description here

正如您在条形图中看到的,所有条形都具有相同的颜色。 x轴也就是我说的数字。但我不想要这种形状。 我想要的东西看起来像this link

我要向我上传的图片解释我想要的更改。

而不是0和1 ...在x轴上,它应该描绘Question Text列。详细地说,x轴上的条形图将是:正如我们在数据框中看到的那样,CI function有两个yesNo有一个CI function。我希望No代替0和1,使用两种不同的颜色,一种指向1596 Yes的数量,另一种颜色指向1376 bathing?

下一个项目将是17965,一个栏位指向702,另一个栏位指向import matplotlib.pyplot as plt data.groupby(['Question Text','Answer Text']).sum().unstack().plot(kind='bar') plt.show()

有了这个,我应该有近十个酒吧,每个酒吧包含两个酒吧,就像我上面的链接一样。

我尝试了各种各样的方式,例如上面的链接,但是我没有表现出来或者出现错误。

谢谢:)

更新1 当我申请你的代码时:

  Traceback (most recent call last):
  File "C:/Users/M193053/PycharmProjects/ADL-distribution/test.py", line 52, in <module>
    data.groupby(['Question Text','Answer Text']).sum().unstack().plot(kind='bar')
  File "C:\Users\M193053\Documents\Anaconda3\envs\conda3\lib\site-packages\pandas\plotting\_core.py", line 2941, in __call__
    sort_columns=sort_columns, **kwds)
  File "C:\Users\M193053\Documents\Anaconda3\envs\conda3\lib\site-packages\pandas\plotting\_core.py", line 1977, in plot_frame
    **kwds)
  File "C:\Users\M193053\Documents\Anaconda3\envs\conda3\lib\site-packages\pandas\plotting\_core.py", line 1804, in _plot
    plot_obj.generate()
  File "C:\Users\M193053\Documents\Anaconda3\envs\conda3\lib\site-packages\pandas\plotting\_core.py", line 258, in generate
    self._compute_plot_data()
  File "C:\Users\M193053\Documents\Anaconda3\envs\conda3\lib\site-packages\pandas\plotting\_core.py", line 373, in _compute_plot_data
    'plot'.format(numeric_data.__class__.__name__))
TypeError: Empty 'DataFrame': no numeric data to plot

我收到了这个错误:

grouped = data.groupby(['Question Text','Answer Text']).size().reset_index(name='counts')

import matplotlib.pyplot as plt
grouped.groupby(['Question Text','Answer Text']).sum().unstack().plot(kind='bar')
plt.show()

但是当我使用这段代码时:

grouped = data.groupby(['Question Text','Answer Text']).size().reset_index(name='counts')

0                          CI function          No     513
1                          CI function         Yes     373
2                             bathing?          No    2827
3                             bathing?         Yes     408
4                            dressing?          No    2824
5                            dressing?         Yes     423
6                              feeding          No    2851
7                              feeding         Yes     160
8                         housekeeping          No    2803
9                         housekeeping         Yes     717
10                      preparing food          No    2604
11                      preparing food         Yes     593
12  responsibility for own medications          No    2793
13  responsibility for own medications         Yes     625
14                            shopping          No      35
15                            shopping         Yes      49
16                           toileting          No    2843
17                           toileting         Yes     239
18                        transferring          No    2834
19                        transferring         Yes     904
20                using transportation          No    2816
21                using transportation         Yes     483

我这样似乎没问题: enter image description here

但是应用两个groupby似乎不合逻辑。因为我还不确定我该怎么做。 他妈的花时间:))

更新两个

这是我的数据框,已经得到了这段代码:

grouped = data.groupby(['Question Text','Answer Text']).size().reset_index(name='counts')
print(grouped)
import matplotlib.pyplot as plt
final = grouped.groupby(['Question Text','Answer Text']).sum()
print(final)


Question Text                      Answer Text        
CI function                        No              513
                                   Yes             373
bathing?                           No             2827
                                   Yes             408
dressing?                          No             2824
                                   Yes             423
feeding                            No             2851
                                   Yes             160
housekeeping                       No             2803
                                   Yes             717
preparing food                     No             2604
                                   Yes             593
responsibility for own medications No             2793
                                   Yes             625
shopping                           No               35
                                   Yes              49
toileting                          No             2843
                                   Yes             239
transferring                       No             2834
                                   Yes             904
using transportation               No             2816
                                   Yes             483

这个数据框来自你的代码与我的组合:

1                             bathing?          No       3529933
2                            dressing?          No       3529933
3                              feeding          No       3529933
4                         housekeeping          No       3529933
5   responsibility for own medications          No       3529933
6                 using transportation          No       3529933
7                            toileting          No       3529933
8                         transferring          No       3529933
10                      preparing food          No       3529933
11                            bathing?         NaN       2864155
12                           dressing?         NaN       2864155
13                             feeding         NaN       2864155
14                        housekeeping         NaN       2864155
15  responsibility for own medications         NaN       2864155
16                           toileting         NaN       2864155
17                        transferring         NaN       2864155
19                      preparing food         NaN       2864155
20                using transportation         Yes       2864155
21                            bathing?         NaN       2921299
22                           dressing?         NaN       2921299

更新3

原始数据框有200000行,如下所示:

people/me

1 个答案:

答案 0 :(得分:1)

您可以这样做(df是您编写的数据框):

import matplotlib
import matplotlib.pyplot as plt
matplotlib.style.use('ggplot')
df.groupby(['Question Text','Answer Text']).sum().unstack().plot(kind='bar')
plt.show()

输出: enter image description here 您也可以这样旋转xlabel:

plt.xticks(rotation=45)

但我建议你缩短标签以使其更清晰