Python matplotlib/seaborn 绘图条形图子类别

标签 python matplotlib seaborn

我有一本字典,名为 data 。我想绘制一个条形图,以便每个 A , B , C ,和D值为 posneg .

我不想要posneg条相互堆叠。我希望他们肩并肩。

此外,我希望类别按其总频率的降序排列 ( pos + neg )

data = {'A': {'pos': 289794, 'neg': 515063},
        'B': {'pos': 174790, 'neg': 292551},
        'C': {'pos': 375574, 'neg': 586616},
        'D': {'pos': 14932, 'neg': 8661}}

最佳答案

%matplotlib inline 
import matplotlib.pyplot as plt
import pandas as pd

data = {'A': {'pos': 289794, 'neg': 515063},
        'B': {'pos': 174790, 'neg': 292551},
        'C': {'pos': 375574, 'neg': 586616},
        'D': {'pos': 14932, 'neg': 8661}}
df = pd.DataFrame(data)
df = df.T
df ['sum'] = df.sum(axis=1)
df.sort_values('sum', ascending=False)[['neg','pos']].plot.bar() 

enter image description here

关于Python matplotlib/seaborn 绘图条形图子类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57746744/

相关文章:

python - 为直方图 Pandas 设置轴标签

python - 使用seaborn从列表列中分组箱线图

python - 100% 堆积条形图 - python (seaborn/matplotlib)

python - 将 SciPy 编译为 Android - 是否已完成,关于如何将 FORTRAN 代码编译为 Android Arm 的任何帮助

python - 替换 Numpy 图像中的像素值

python - 为什么条件判断结果为 2

python - 带有 Google App Engine 本地开发服务器的 Matplotlib

python - Seaborn 的 relplot : Is there a way to assign a specific color to all lines and another color to another single line when using the hue argument?

Python 调用类变量

python - 使用 Beautifulsoup 在页面中查找特定字符串