Python中的正常matplotlib boxplot命令返回一個包含框,中位數,鬍鬚,傳單和大寫字母鍵的字典。這使得造型非常簡單。熊貓羣組的箱型造型
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Create a dataframe and subset it for a boxplot
df1 = pd.DataFrame(rand(10), columns=['Col1'])
df1['X'] = pd.Series(['A','B','A','B','A','B','A','B','A','B'])
boxes= [df1[df1['X'] == 'A'].Col1, df1[df1['X'] == 'B'].Col1]
# Call the standard matplotlib boxplot function,
# which returns a dictionary including the parts of the graph
mbp = plt.boxplot(boxes)
print(type(mbp))
# This dictionary output makes styling the boxplot easy
plt.setp(mbp['boxes'], color='blue')
plt.setp(mbp['medians'], color='red')
plt.setp(mbp['whiskers'], color='blue')
plt.setp(mbp['fliers'], color='blue')
熊貓圖書館有一個「優化」boxplot函數爲其分組(索引)的數據框。但是,它不是爲每個組返回多個字典,而是返回一個matplotlib.axes.AxesSubplot對象。這使得造型非常困難。
# Pandas has a built-in boxplot function that returns
# a matplotlib.axes.AxesSubplot object
pbp = df1.boxplot(by='X')
print(type(pbp))
# Similar attempts at styling obviously return TypeErrors
plt.setp(pbp['boxes'], color='blue')
plt.setp(pbp['medians'], color='red')
plt.setp(pbp['whiskers'], color='blue')
plt.setp(pbp['fliers'], color='blue')
這個AxisSubplot對象是由pandas df.boxplot(by ='X')函數產生的嗎?
你能告訴我們一些示例代碼(用假數據?) – tacaswell
我已經編輯了這個問題以包含示例數據和代碼,以及更清楚地展示我的問題。 –