pandas 箱图中共享轴的不同ylim

编程入门 行业动态 更新时间:2024-10-27 15:23:25
本文介绍了 pandas 箱图中共享轴的不同ylim的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个分组的熊猫箱图,排列在(2,2)网格中:

I have a grouped pandas boxplot, arrange in a (2,2) grid:

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=(2,2),figsize=(6,8)) plt.show()

我现在只想更改第二行的ylim.

I now want to change the ylim of the second row only.

我的想法是添加:

[ax_tmp.set_ylim(-10,10) for ax_tmp in np.asarray(bp).reshape(-1)[2:4]]

[ax_tmp.set_ylim(-10,10) for ax_tmp in np.asarray(bp)[1,:]]

但是它们都改变了所有子图的ylim. 这可能是由于共享.但是我不知道要摆脱它.

but they both change the ylim of all subplots. This may be because of the sharedy. But I have no idea to get rid of it.

我的问题与此相关: pandas boxplot,groupby每个子图中的ylim都不同,但我认为不是重复的.另外,该解决方案在此处不容易应用.

my problem is somewhat related to this one: pandas boxplot, groupby different ylim in each subplot but not a duplicate in my opinion. Also the solution is not easily applicable here.

更新:理想情况下,各行应共享一个共同的y,而不是各绘制一个自己的

UPDATE: Ideally, the rows should share a common y, not each plot its own

推荐答案

解决方案是将fig,axes传递给使用sharey=False自定义的熊猫的boxplot:

The solution is to pass a fig,axes to pandas's boxplot that are customised with sharey=False:

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)) fig, ax_new = plt.subplots(2,2, sharey=False) bp = df.boxplot(by="models",ax=ax_new,layout=(2,2),figsize=(6,8)) [ax_tmp.set_xlabel('') for ax_tmp in ax_new.reshape(-1)] [ax_tmp.set_ylim(-2, 2) for ax_tmp in ax_new[1]] fig.suptitle('New title here') plt.show()

结果:

如果要逐行共享.这段代码适合您:

If you want to sharey row-wise. This code works for you :

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)) layout = [2,2] fig = plt.figure() all_axes = [] counter = 1 for i in range(layout[0]): tmp_row_axes = [] for j in range(layout[1]): if j!=0 : exec "tmp_row_axes.append(fig.add_subplot(%d%d%d, sharey=tmp_row_axes[0]))"%(layout[0],layout[1],counter) else: exec "tmp_row_axes.append(fig.add_subplot(%d%d%d))" % (layout[0], layout[1], counter) counter+=1 all_axes.append(tmp_row_axes) all_axes = np.array(all_axes) bp = df.boxplot(by="models",ax=np.array(all_axes),layout=(2,2),figsize=(6,8)) [ax_tmp.set_xlabel('') for ax_tmp in all_axes.reshape(-1)] all_axes[1][0].set_ylim(-2,2) fig.suptitle('New title here') plt.show()

如您所见,仅通过使用all_axes[1][0].set_ylim(-2,2)更改第二行中第一条轴的ylim即可更改整个行. all_axes[1][1].set_ylim(-2,2)将执行相同的操作,因为它们具有共享的y轴.

As you see by only changing the ylim of 1st axes in the 2nd row using all_axes[1][0].set_ylim(-2,2) the whole row is changed. all_axes[1][1].set_ylim(-2,2) would do the same since they have a shared y axis.

如果只希望在最后一行使用x轴,而只希望在第一列使用y轴标签,只需将循环更改为:

If you want the x-axis only in the last row and the y-axis label only in the first column, just change the loop to this:

for i in range(layout[0]): tmp_row_axes = [] for j in range(layout[1]): if j!=0 : exec "tmp_ax = fig.add_subplot(%d%d%d, sharey=tmp_row_axes[0])"%(layout[0],layout[1],counter) tmp_ax.get_yaxis().set_visible(False) else: exec "tmp_ax=fig.add_subplot(%d%d%d)" % (layout[0], layout[1], counter) if i!=layout[1]-1 : tmp_ax.get_xaxis().set_visible(False) tmp_row_axes.append(tmp_ax) counter+=1 all_axes.append(tmp_row_axes)

结果:

更多推荐

pandas 箱图中共享轴的不同ylim

本文发布于:2023-07-18 08:25:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1142818.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图中   pandas   ylim

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!