pandas 修改熊猫箱线图输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40125528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
modify pandas boxplot output
提问by spore234
I made this plot in pandas, according to the documentation:
根据文档,我在Pandas中制作了这个图:
import pandas as pd
import numpy as np
import pyplot as plt
df = pd.DataFrame(np.random.rand(140, 4), columns=['A', 'B', 'C', 'D'])
df['models'] = pd.Series(np.repeat(['model1','model2', 'model3', 'model4', 'model5', 'model6', 'model7'], 20))
plt.figure()
bp = df.boxplot(by="models")
How can I modify this plot?
我该如何修改这个情节?
I want:
我想要:
- modify arrangement from (2,2) to (1,4)
- change the labels and titles, text and font size
- remove the '[models]' text
- 将排列从 (2,2) 修改为 (1,4)
- 更改标签和标题、文本和字体大小
- 删除“[模型]”文本
and how do I save this plot as pdf ?
以及如何将此图保存为 pdf ?
回答by Kennet Celeste
- For the arrangement use
layout
- For setting x label use
set_xlabel('')
- For figure title use
figure.subtitle()
- For changing the figure size use
figsize=(w,h)
(inches)
- 供安排使用
layout
- 用于设置 x 标签使用
set_xlabel('')
- 对于图标题使用
figure.subtitle()
- 用于更改图形大小使用
figsize=(w,h)
(英寸)
note: the line np.asarray(bp).reshape(-1)
is converting the layout of the subplots (2x2 for instance) to an array.
注意:该行将np.asarray(bp).reshape(-1)
子图的布局(例如 2x2)转换为数组。
code :
代码 :
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.rand(140, 4), columns=['A', 'B', 'C', 'D'])
df['models'] = pd.Series(np.repeat(['model1','model2', 'model3', 'model4', 'model5', 'model6', 'model7'], 20))
bp = df.boxplot(by="models",layout=(4,1),figsize=(6,8))
[ax_tmp.set_xlabel('') for ax_tmp in np.asarray(bp).reshape(-1)]
fig = np.asarray(bp).reshape(-1)[0].get_figure()
fig.suptitle('New title here')
plt.show()
result:
结果:
回答by Archie
A number of things you can do already using the boxplot function in pandas, see the documentation.
您可以使用 Pandas 中的 boxplot 函数执行许多操作,请参阅文档。
You can already modify the arrangement, and change the fontsize:
import pandas as pd import numpy as np import pyplot as plt df = pd.DataFrame(np.random.rand(140, 4), columns=['A', 'B', 'C', 'D']) df['models'] = pd.Series(np.repeat(['model1','model2', 'model3', 'model4', 'model5', 'model6', 'model7'], 20)) bp = df.boxplot(by="models", layout = (4,1), fontsize = 14)
Changing the columns the labels can be done by changing the columns labels of the dataframe itself:
df.columns(['E', 'F', 'G', 'H', 'models'])
For further customization I would use the functionality from matlotlib itself; you can take a look at the examples here.
您已经可以修改排列并更改字体大小:
import pandas as pd import numpy as np import pyplot as plt df = pd.DataFrame(np.random.rand(140, 4), columns=['A', 'B', 'C', 'D']) df['models'] = pd.Series(np.repeat(['model1','model2', 'model3', 'model4', 'model5', 'model6', 'model7'], 20)) bp = df.boxplot(by="models", layout = (4,1), fontsize = 14)
可以通过更改数据框本身的列标签来更改标签的列:
df.columns(['E', 'F', 'G', 'H', 'models'])
为了进一步定制,我将使用 matlotlib 本身的功能;你可以看看这里的例子。