bne*_*lsj 37 matplotlib seaborn
我想创建一个时间序列图,使用seaborn.tsplot类似于此示例中的tsplot文档,但图例移到右侧,图中.

基于seaborn的timeseries.py中的 339-340行,看起来seaborn.tsplot目前不允许直接控制图例放置:
if legend:
ax.legend(loc=0, title=legend_name)
Run Code Online (Sandbox Code Playgroud)
是否有matplotlib解决方法?我正在使用seaborn 0.6-dev.
Ser*_*kiy 77
实际上,seaborn到目前为止还没有很好地处理传说.根据Matplotlib图例指南,您可以plt.legend()直接使用来控制图例属性.matplotlib
例:
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="darkgrid")
# Load the long-form example gammas dataset
gammas = sns.load_dataset("gammas")
# Plot the response with standard error
sns.tsplot(data=gammas, time="timepoint", unit="subject",
condition="ROI", value="BOLD signal")
# Put the legend out of the figure
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
Run Code Online (Sandbox Code Playgroud)
Sam*_*son 10
现有的解决方案似乎通过使用“错误”的位置参数使事情变得不必要地复杂;想想传说与锚点的关系。例如,如果您想要右侧的图例,那么锚点位置就是center left它。
我们可以将 Sergey Antopolskiy 的回答简化为:
import seaborn as sns
# Load the long-form example gammas dataset
g = sns.lineplot(data=gammas, x="timepoint", y="BOLD signal", hue="ROI")
# Put the legend out of the figure
g.legend(loc='center left', bbox_to_anchor=(1, 0.5))
Run Code Online (Sandbox Code Playgroud)
bbox_to_anchor说我们希望锚在右侧(即1在 x 轴上)和垂直居中(0.5在 y 轴上)。 loc说我们想要这个锚的图例左中。
在 Seaborn 0.11.0 版中,这给了我类似的东西:
小智 7
Sergey 的答案使用 a 对我很有用,seaborn.tsplot但我无法让它为 an 工作,seaborn.lmplot所以我更深入地研究并找到了另一个解决方案:
例子:
import seaborn as sns
import pandas as pd
# load data
df = pd.DataFrame.from_csv('mydata.csv')
# create with hue but without legend
g = sns.lmplot(x="x_data", y="y_data", hue="condition", legend=False, data=df)
# resize figure box to -> put the legend out of the figure
box = g.ax.get_position() # get position of figure
g.ax.set_position([box.x0, box.y0, box.width * 0.85, box.height]) # resize position
# Put a legend to the right side
g.ax.legend(loc='center right', bbox_to_anchor=(1.25, 0.5), ncol=1)
sns.plt.show(g)
Run Code Online (Sandbox Code Playgroud)
也许您必须调整这些值以使其适合您的传奇。 如果您需要更多示例,此答案也将有所帮助。
我试图将TW的答案用于海底线图,但没有成功。对他的答案进行了一些修改就可以完成这项工作...万一有人像我一样正在寻找线图版本!
import seaborn as sns
import pandas as pd
# load data
df = sns.load_dataset("gammas")
# EDIT: I Needed to ad the fig
fig, ax1 = plt.subplots(1,1)
# EDIT:
# T.W.' answer said: "create with hue but without legend" <- # I needed to include it!
# So, removed: legend=False
g = sns.lineplot(x="timepoint", y="BOLD signal", hue="ROI", data=df, ax=ax1)
# EDIT:
# Removed 'ax' from T.W.'s answer here aswell:
box = g.get_position()
g.set_position([box.x0, box.y0, box.width * 0.85, box.height]) # resize position
# Put a legend to the right side
g.legend(loc='center right', bbox_to_anchor=(1.25, 0.5), ncol=1)
plt.show()
Run Code Online (Sandbox Code Playgroud)
seaborn解决方案:FacetGrid基于 Seaborn 的图可以使用legend_outkwarg自动执行此操作。使用relplot, 通过字典传递legend_out给FacetGrid构造函数facet_kws:
import seaborn as sns
sns.set(style="darkgrid")
gammas = sns.load_dataset("gammas")
sns.relplot(
data=gammas,
x="timepoint",
y="BOLD signal",
hue="ROI",
kind="line",
facet_kws={"legend_out": True}
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
38061 次 |
| 最近记录: |