我有一个分组的pandas boxplot,安排在(2,2)网格中:
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
。
我的想法是添加:
[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。 这可能是因为共享。但我不知道要摆脱它。
我的问题与此问题有些相关:pandas boxplot, groupby different ylim in each subplot但在我看来并不重复。此解决方案也不容易适用。
UPDATE:理想情况下,行应该共享一个共同的y,而不是每个绘图都有自己的
答案 0 :(得分:9)
解决方案是将 [HttpPost]
public ActionResult Login(LoginModel model, string returnUrl)
{
// Lets first check if the Model is valid or not
if (ModelState.IsValid)
{
using (var db = new MarketingLogsEntities())
{
bool userValid = db.RegisteredUsers.Any(x => x.UserEmail == model.UserEmail && x.Password == model.Password);
// User found in the database
if (userValid)
{
FormsAuthentication.SetAuthCookie(model.UserEmail, false);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
}
return View(model);
}
传递给使用fig,axes
自定义的pandas boxplot
:
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()
正如您所看到的那样,只使用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()
更改第2行中第1轴的ylim,整行就会改变。 all_axes[1][0].set_ylim(-2,2)
会做同样的事情,因为它们有一个共享的y轴。
如果您只希望x轴在最后一行而y轴标签只在第一列中,只需将循环更改为:
all_axes[1][1].set_ylim(-2,2)
结果: