英文:
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("文件路径")
这是输出的结果:
正如您所看到的,红色来自“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")
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))
输出:
但你也可以想要:
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)))
输出:
英文:
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:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论