从熊猫子图中删除单个图例

时间:2019-12-08 22:22:19

标签: legend subplot

我想从熊猫的各个子图中删除图例。我创建了条形图和#subplots。我想保留每个子图的标题并删除图例,因为它们显示的是#same verbiage。我尝试了几种技巧,甚至有些技巧让我在每个单独的#subplot上调用,但是我确信有一个简单的解决方案。下面的第四个结果图像是我需要帮助的图像。

到目前为止,这是我的代码:

import matplotlib.pyplot as plt
import pandas as pd
import scipy.stats as st
import numpy as np

mouse_metadata = "Mouse_metadata.csv"
study_results = "Study_results.csv"

mouse_metadata = pd.read_csv(mouse_metadata)
study_results = pd.read_csv(study_results)

study_data_combined = pd.merge(mouse_metadata,study_results, on= "Mouse ID")
pyma_sd = study_data_combined
pyma_sd.head()

Picture of Output 1

pyma_sd_grouped = pyma_sd.groupby(["Drug Regimen"])  

pyma_sd_grouped_mean = pyma_sd_grouped["Tumor Volume (mm3)"].mean()

pyma_sd_grouped_median = pyma_sd_grouped["Tumor Volume (mm3)"].median()

pyma_sd_grouped_variance = pyma_sd_grouped["Tumor Volume (mm3)"].var()

pyma_sd_grouped_std = pyma_sd_grouped["Tumor Volume (mm3)"].std()

pyma_sd_grouped_sem = pyma_sd_grouped["Tumor Volume (mm3)"].sem()

pyma_sd_grouped_stats = pd.DataFrame({ "Mean":pyma_sd_grouped_mean,
                                     "Median": pyma_sd_grouped_median,
                                     "Variance": pyma_sd_grouped_variance, 
                                     "Standard Error of Mean ": pyma_sd_grouped_sem})
print("                             ","Stats of Tumor Volume")
print(pyma_sd_grouped_stats)

Picture of Output 2

chart_pyma_sd_grouped_stats = pyma_sd_grouped_stats.plot(kind='bar', rot=50, figsize = (10, 6), 
width = .8)
plt.title("Stats on Drug Regimen")Output 2
plt.xlabel("Drug Regimen")
plt.ylabel("Stats per Drug Regimen")
plt.tight_layout()
plt.show()

Picture of output

axes = pyma_sd_grouped_stats.plot.bar(rot=50, subplots=True, figsize = (10, 6), width = .75,)
axes[1].legend(loc=1)
plt.subplots_adjust(hspace=0.5)
plt.show()

Picture of output 4 **

1 个答案:

答案 0 :(得分:0)

在致电legend=False时只需提供DataFrame.plot.bar

import matplotlib.pyplot as plt
import pandas as pd

speed = [0.1, 17.5, 40, 48, 52, 69, 88]
lifespan = [2, 8, 70, 1.5, 25, 12, 28]
index = ['snail', 'pig', 'elephant', 'rabbit', 'giraffe', 'coyote', 'horse']
df = pd.DataFrame({'speed': speed, 'lifespan': lifespan}, index=index)
axes = df.plot.bar(rot=0, subplots=True, legend=False)
plt.show()

enter image description here

将上面的图像与doc中生成的图像进行比较。