2015-05-27 20 views
8

Tôi muốn tạo một chuỗi thời gian bằng cách sử dụng seaborn.tsplot như trong this example from tsplot documentation, nhưng với chú giải được di chuyển sang phải, bên ngoài hình.Di chuyển chú thích bên ngoài hình trong seaborn tsplot

example

Dựa trên các dòng 339-340 trong seaborn's timeseries.py, nó trông giống như seaborn.tsplot hiện không cho phép kiểm soát trực tiếp của vị trí huyền thoại:

if legend: 
     ax.legend(loc=0, title=legend_name) 

Có một workaround matplotlib? Tôi đang sử dụng seaborn 0.6-dev.

+2

Tôi nghĩ nếu bạn gọi 'ax.legend' một lần nữa, nó sẽ vẽ lại nó, chứ không phải thêm giây thứ hai. – mwaskom

+0

@mwaskom: Làm việc cho tôi. Cảm ơn! – bnelsj

Trả lời

20

Thật vậy, seaborn không xử lý truyền thuyết tốt cho đến thời điểm này. Bạn có thể sử dụng plt.legend() để kiểm soát thuộc tính chú giải trực tiếp thông qua matplotlib, theo Matplotlib Legend Guide.

Ví dụ:

import matpltlib.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.) 

Modified legend position

+0

Dòng cuối cùng là giải pháp. Nó cũng làm việc cho barplot của tôi. – Denziloe

0

Câu trả lời bởi Sergey làm việc tuyệt vời cho tôi sử dụng một seaborn.tsplot nhưng tôi đã không thể có được nó làm việc cho một seaborn.lmplot vì vậy tôi nhìn một chút sâu hơn và tìm thấy một giải pháp:

Ví dụ:

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) 

Có lẽ bạn phải chơi xung quanh với các giá trị để phù hợp với truyền thuyết của bạn. Câu trả lời This cũng sẽ hữu ích nếu bạn cần thêm ví dụ.

Các vấn đề liên quan