Python中的普通matplotlib boxplot命令返回一个字典,其中包含box、median、shatters、fliers和caps的键。这使得造型非常容易。
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')
Pandas库有一个针对其分组(层次索引)数据帧的“优化”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')
pandas df.boxplot(by='X')函数生成的AxisSubplot对象是否可访问?
您还可以将^{} 指定为
dict
。这将直接在字典中返回boxplot属性,字典中的每个列都会在boxplot中绘制索引。要使用上面的示例(在IPython中):
现在,更改线宽是一个列表理解的问题:
恐怕你得硬编码。以
pandas
为例:http://pandas.pydata.org/pandas-docs/stable/visualization.html#box-plotting现在让我们来确定哪个是方框、中间值等:
情节是这样的:
每个酒吧由8个项目组成。e、 第五项是中位数。第七和第八项可能是传单,我们这里没有。
了解这些,修改酒吧的某些部分是很容易的。如果要将中值设置为2的
linewidth
:相关问题 更多 >
编程相关推荐