2016-03-15 16 views
8

Tôi bắt đầu tìm hiểu một chút python (đang sử dụng R) để phân tích dữ liệu. Tôi đang cố gắng để tạo ra hai lô bằng cách sử dụng seaborn, nhưng nó giữ tiết kiệm thứ hai trên đầu trang của người đầu tiên. Làm cách nào để ngăn chặn hành vi này?Dừng seaborn vẽ nhiều hình trên đầu trang của nhau

import seaborn as sns 
iris = sns.load_dataset('iris') 

length_plot = sns.barplot(x='sepal_length', y='species', data=iris).get_figure() 
length_plot.savefig('ex1.pdf') 
width_plot = sns.barplot(x='sepal_width', y='species', data=iris).get_figure() 
width_plot.savefig('ex2.pdf') 

Trả lời

14

Bạn phải bắt đầu một nhân vật mới để làm điều đó. Có nhiều cách để làm điều đó, giả sử bạn có matplotlib. Đồng thời, hãy loại bỏ get_figure() và bạn có thể sử dụng plt.savefig() từ đó.

Phương pháp 1

Sử dụng plt.clf()

import seaborn as sns 
import matplotlib.pyplot as plt 

iris = sns.load_dataset('iris') 

length_plot = sns.barplot(x='sepal_length', y='species', data=iris) 
plt.savefig('ex1.pdf') 
plt.clf() 
width_plot = sns.barplot(x='sepal_width', y='species', data=iris) 
plt.savefig('ex2.pdf') 

Phương pháp 2

Gọi plt.figure() trước mỗi một

plt.figure() 
length_plot = sns.barplot(x='sepal_length', y='species', data=iris) 
plt.savefig('ex1.pdf') 
plt.figure() 
width_plot = sns.barplot(x='sepal_width', y='species', data=iris) 
plt.savefig('ex2.pdf') 
+0

Cảm ơn. Tôi đoán nó là một cái gì đó như thế này - chỉ không thể tìm thấy các lệnh đúng! – Alex

+1

Câu trả lời này "hoạt động", nhưng nó là một IMO ít được ưa thích hơn vì nó dựa trên giao diện máy trạng thái matplotlib hơn là hoàn toàn ôm lấy giao diện hướng đối tượng. Nó là tốt cho lô nhanh chóng, nhưng tại một số điểm khi mở rộng quy mô phức tạp nó sẽ là tốt hơn để sử dụng sau này. – mwaskom

5

Tạo con số cụ thể và cốt truyện lên chúng:

import seaborn as sns 
iris = sns.load_dataset('iris') 

length_fig, length_ax = plt.subplots() 
sns.barplot(x='sepal_length', y='species', data=iris, ax=length_ax) 
length_fig.savefig('ex1.pdf') 

width_fig, width_ax = plt.subplots() 
sns.barplot(x='sepal_width', y='species', data=iris, ax=width_ax) 
width_fig.savefig('ex2.pdf') 
Các vấn đề liên quan