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

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

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

问题

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

  1. import math
  2. import pandas as pd
  3. pd.options.mode.chained_assignment = None # default='warn'
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6. from openpyxl import load_workbook
  7. def gen(fileIn):
  8. DF= pd.read_excel(fileIn)
  9. overall = DF['Awesome'].value_counts(normalize=True) # 获取“Awesome”列中每个唯一值的相对频率
  10. print(overall.plot(kind='bar', figsize=(10,5)))
  11. spec = DF['Not Awesome'].value_counts(normalize=True)
  12. print(spec.plot(kind='bar', color='red', figsize=(10,5)))
  13. 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:

  1. import math
  2. import pandas as pd
  3. pd.options.mode.chained_assignment = None # default='warn'
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6. from openpyxl import load_workbook
  7. def gen(fileIn):
  8. DF= pd.read_excel(fileIn)
  9. overall = DF['Awesome'].value_counts(normalize = True) # Get the relative frequency of each unique value in the column Awesome
  10. print(overall.plot(kind = 'bar', figsize = (10,5)))
  11. spec = DF['Not Awesome'].value_counts(normalize = True)
  12. print(spec.plot(kind = 'bar', color = 'red', figsize = (10,5)))
  13. 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

你需要创建两个子图:

  1. def gen(fileIn):
  2. DF = pd.read_excel(fileIn)
  3. fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))
  4. overall = DF['Awesome'].value_counts(normalize=True)
  5. print(overall.plot(kind='bar', ax=ax1))
  6. spec = DF['Not Awesome'].value_counts(normalize=True)
  7. print(spec.plot(kind='bar', color='red', ax=ax2))

输出:

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

但你也可以想要:

  1. def gen(fileIn):
  2. DF = pd.read_excel(fileIn)
  3. overall = DF['Awesome'].value_counts(normalize=True)
  4. spec = DF['Not Awesome'].value_counts(normalize=True)
  5. ax = (pd.concat([overall, spec], keys=['Awesome', 'Not Awesome'], axis=1)
  6. .plot(kind='bar', color=['blue', 'red'], figsize=(10,5)))

输出:

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

英文:

You have to create two subplots:

  1. def gen(fileIn):
  2. DF = pd.read_excel(fileIn)
  3. fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))
  4. overall = DF['Awesome'].value_counts(normalize=True)
  5. print(overall.plot(kind='bar', ax=ax1))
  6. spec = DF['Not Awesome'].value_counts(normalize=True)
  7. print(spec.plot(kind='bar', color='red', ax=ax2))

Output:

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

But you can also want:

  1. def gen(fileIn):
  2. DF = pd.read_excel(fileIn)
  3. overall = DF['Awesome'].value_counts(normalize=True)
  4. spec = DF['Not Awesome'].value_counts(normalize=True)
  5. ax = (pd.concat([overall, spec], keys=['Awesome', 'Not Awesome'], axis=1)
  6. .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:

确定