如何使用 matplotlib 将标签放在两个图表中
How can I put labels in two charts using matplotlib
我正在尝试使用分组依据的结果绘制两个直方图。但是标签只出现在其中一个标签中。
如何将标签放在两个图表中?
以及如何为图表设置不同的标题(例如首先作为男性等级,其次作为女性等级)
import pandas as pd
import matplotlib.pyplot as plt
microdataEnem = pd.read_csv('C:\Users\Lucas\AppData\Local\Programs\Python\Python39\Scripts\Data Science\Data Analysis\Projects\ENEM\DADOS\MICRODADOS_ENEM_2019.csv', sep = ';', encoding = 'ISO-8859-1', nrows=10000)
sex_essaygrade = ['TP_SEXO', 'NU_NOTA_REDACAO']
filter_sex_essaygrade = microdataEnem.filter(items = sex_essaygrade)
filter_sex_essaygrade.dropna(subset = ['NU_NOTA_REDACAO'], inplace = True)
filter_sex_essaygrade.groupby('TP_SEXO').hist()
plt.xlabel('Grade')
plt.ylabel('Number of students')
plt.show()
您可以尝试使用以下格式来代替 filter_sex_essaygrade.groupby('TP_SEXO').hist()
:axs = filter_sex_essaygrade['NU_NOTA_REDACAO'].hist(by=filter_sex_essaygrade['TP_SEXO'])
。这将自动使用组名称为每个直方图命名。
您需要将变量 axs
设置为等于此直方图对象,以便您可以修改两个图的 x 和 y 标签。
我创建了一些与您类似的数据,得到以下结果:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
np.random.seed(42)
sex_essaygrade = ['TP_SEXO', 'NU_NOTA_REDACAO']
## create two distinct sets of grades
sample_grades = np.concatenate((np.random.randint(low=70,high=100,size=100), np.random.randint(low=80,high=100,size=100)))
filter_sex_essaygrade = pd.DataFrame({
'NU_NOTA_REDACAO': sample_grades,
'TP_SEXO': ['Men']*100 + ['Women']*100
})
axs = filter_sex_essaygrade['NU_NOTA_REDACAO'].hist(by=filter_sex_essaygrade['TP_SEXO'])
for ax in axs.flatten():
ax.set_xlabel("Grade")
ax.set_ylabel("Number of students")
plt.show()
我正在尝试使用分组依据的结果绘制两个直方图。但是标签只出现在其中一个标签中。 如何将标签放在两个图表中? 以及如何为图表设置不同的标题(例如首先作为男性等级,其次作为女性等级)
import pandas as pd
import matplotlib.pyplot as plt
microdataEnem = pd.read_csv('C:\Users\Lucas\AppData\Local\Programs\Python\Python39\Scripts\Data Science\Data Analysis\Projects\ENEM\DADOS\MICRODADOS_ENEM_2019.csv', sep = ';', encoding = 'ISO-8859-1', nrows=10000)
sex_essaygrade = ['TP_SEXO', 'NU_NOTA_REDACAO']
filter_sex_essaygrade = microdataEnem.filter(items = sex_essaygrade)
filter_sex_essaygrade.dropna(subset = ['NU_NOTA_REDACAO'], inplace = True)
filter_sex_essaygrade.groupby('TP_SEXO').hist()
plt.xlabel('Grade')
plt.ylabel('Number of students')
plt.show()
您可以尝试使用以下格式来代替 filter_sex_essaygrade.groupby('TP_SEXO').hist()
:axs = filter_sex_essaygrade['NU_NOTA_REDACAO'].hist(by=filter_sex_essaygrade['TP_SEXO'])
。这将自动使用组名称为每个直方图命名。
您需要将变量 axs
设置为等于此直方图对象,以便您可以修改两个图的 x 和 y 标签。
我创建了一些与您类似的数据,得到以下结果:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
np.random.seed(42)
sex_essaygrade = ['TP_SEXO', 'NU_NOTA_REDACAO']
## create two distinct sets of grades
sample_grades = np.concatenate((np.random.randint(low=70,high=100,size=100), np.random.randint(low=80,high=100,size=100)))
filter_sex_essaygrade = pd.DataFrame({
'NU_NOTA_REDACAO': sample_grades,
'TP_SEXO': ['Men']*100 + ['Women']*100
})
axs = filter_sex_essaygrade['NU_NOTA_REDACAO'].hist(by=filter_sex_essaygrade['TP_SEXO'])
for ax in axs.flatten():
ax.set_xlabel("Grade")
ax.set_ylabel("Number of students")
plt.show()