如何在pandas中防止条形图相互叠加?

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

How to prevent bar plots from superimposing on each other in pandas?

问题

我正在使用pandas处理一些数据,并想基于这些数据生成两个单独的条形图。然而,我的两个条形图却重叠在一起(它们在同一图表中)。以下是我的代码:

import math
import pandas as pd
pd.options.mode.chained_assignment = None  # default='warn'
import numpy as np
import matplotlib.pyplot as plt
from openpyxl import load_workbook

def gen(fileIn):
    DF= pd.read_excel(fileIn)
    
    overall = DF['Awesome'].value_counts(normalize=True) # 获取“Awesome”列中每个唯一值的相对频率
    print(overall.plot(kind='bar', figsize=(10,5)))
    
    spec = DF['Not Awesome'].value_counts(normalize=True) 
    print(spec.plot(kind='bar', color='red', figsize=(10,5)))
                
gen("文件路径")

这是输出的结果:

如何在pandas中防止条形图相互叠加?

正如您所看到的,红色来自“Not Awesome”列的值重叠在“Awesome”列的相对频率值上。我只希望这两个条形图是分开的。我查看了https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.html 上的绘图函数的文档,但似乎没有可以用来关闭默认叠加的参数。

英文:

I am working with some data in pandas and I want to generate 2 separate bar plots based on the data. However, my two bar plots instead get superimposed on each-other (they are in the same graph). Here is my code:

import math
import pandas as pd
pd.options.mode.chained_assignment = None  # default='warn'
import numpy as np
import matplotlib.pyplot as plt
from openpyxl import load_workbook

def gen(fileIn):
    DF= pd.read_excel(fileIn)
    
    overall = DF['Awesome'].value_counts(normalize = True) # Get the relative frequency of each unique value in the column Awesome
    print(overall.plot(kind = 'bar', figsize = (10,5)))
    
    spec = DF['Not Awesome'].value_counts(normalize = True) 
    print(spec.plot(kind = 'bar', color = 'red', figsize = (10,5)))
                
gen("my file path")

This is the output:
如何在pandas中防止条形图相互叠加?

As you can see, the red color from the 'Not Awesome' column get supplanted on the 'Awesome' column's relative frequency values. I just want the two bar plots to be separate. I looked at the documentation in https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.html for the plot function, however, there don't seem to be any parameters I can use to turn off the superimposition which seems to be a default.

答案1

得分: 1

你需要创建两个子图:

def gen(fileIn):
    DF = pd.read_excel(fileIn)
    
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))

    overall = DF['Awesome'].value_counts(normalize=True)
    print(overall.plot(kind='bar', ax=ax1))
    
    spec = DF['Not Awesome'].value_counts(normalize=True) 
    print(spec.plot(kind='bar', color='red', ax=ax2))

输出:

如何在pandas中防止条形图相互叠加?

但你也可以想要:

def gen(fileIn):
    DF = pd.read_excel(fileIn)

    overall = DF['Awesome'].value_counts(normalize=True)
    spec = DF['Not Awesome'].value_counts(normalize=True) 

    ax = (pd.concat([overall, spec], keys=['Awesome', 'Not Awesome'], axis=1)
            .plot(kind='bar', color=['blue', 'red'], figsize=(10,5)))

输出:

如何在pandas中防止条形图相互叠加?

英文:

You have to create two subplots:

def gen(fileIn):
    DF = pd.read_excel(fileIn)
    
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))

    overall = DF['Awesome'].value_counts(normalize=True)
    print(overall.plot(kind='bar', ax=ax1))
    
    spec = DF['Not Awesome'].value_counts(normalize=True) 
    print(spec.plot(kind='bar', color='red', ax=ax2))

Output:

如何在pandas中防止条形图相互叠加?

But you can also want:

def gen(fileIn):
    DF = pd.read_excel(fileIn)

    overall = DF['Awesome'].value_counts(normalize=True)
    spec = DF['Not Awesome'].value_counts(normalize=True) 

    ax = (pd.concat([overall, spec], keys=['Awesome', 'Not Awesome'], axis=1)
            .plot(kind='bar', color=['blue', 'red'], figsize=(10,5)))

Output:

如何在pandas中防止条形图相互叠加?

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

发表评论

匿名网友

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

确定