2015-08-28 33 views
7

Tôi có đoạn mã sau:Làm thế nào để thêm truyền thuyết về âm mưu thanh sanh ở biển facetgrid

import numpy as np 
import pandas as pd 
import matplotlib 
matplotlib.use('Agg') 
import matplotlib.pyplot as plt 
matplotlib.style.use('ggplot') 
import seaborn as sns 

sns.set(style="white") 

# Create a dataset with many short random walks 
rs = np.random.RandomState(4) 
pos = rs.randint(-1, 2, (10, 5)).cumsum(axis=1) 
pos -= pos[:, 0, np.newaxis] 
step = np.tile(range(5), 10) 
walk = np.repeat(range(10), 5) 
df = pd.DataFrame(np.c_[pos.flat, step, walk], 
        columns=["position", "step", "walk"]) 



# Initialize a grid of plots with an Axes for each walk 
grid = sns.FacetGrid(df, col="walk", hue="walk", col_wrap=5, size=5, 
     aspect=1) 


# Draw a bar plot to show the trajectory of each random walk 
grid.map(sns.barplot, "step", "position", palette="Set3").add_legend(); 

grid.savefig("/Users/mymacmini/Desktop/test_fig.png") 
#sns.plt.show() 

Mà làm cho âm mưu này:

enter image description here

Như bạn có thể thấy tôi nhận được huyền thoại sai. Làm thế nào tôi có thể làm cho nó đúng?

+0

Bạn nên sử dụng 'factorplot', hoặc nếu bạn thực sự muốn sử dụng' FacteGrid' trực tiếp, bạn phải vượt qua biến 'hue' trong' map'. – mwaskom

+0

@mwaskom Cảm ơn rất nhiều. bạn có thể cho ví dụ? Tôi đã thử điều này nhưng không thành công quá 'grid.map (sns.barplot," bước "," vị trí ", hue =" bước ", bảng =" Set3 "). Add_legend();' – neversaint

+0

Đó là đối số vị trí thứ ba. Nhưng bạn thực sự nên sử dụng 'factorplot' ... – mwaskom

Trả lời

10

Một số cách có một mục chú thích cho từng phần phụ. Có vẻ như nếu chúng ta muốn có truyền thuyết tương ứng với các thanh trong mỗi phần con, chúng ta phải tự tạo chúng.

# Let's just make a 1-by-2 plot 
df = df.head(10) 

# Initialize a grid of plots with an Axes for each walk 
grid = sns.FacetGrid(df, col="walk", hue="walk", col_wrap=2, size=5, 
     aspect=1) 

# Draw a bar plot to show the trajectory of each random walk 
bp = grid.map(sns.barplot, "step", "position", palette="Set3") 

# The color cycles are going to all the same, doesn't matter which axes we use 
Ax = bp.axes[0] 

# Some how for a plot of 5 bars, there are 6 patches, what is the 6th one? 
Boxes = [item for item in Ax.get_children() 
     if isinstance(item, matplotlib.patches.Rectangle)][:-1] 

# There is no labels, need to define the labels 
legend_labels = ['a', 'b', 'c', 'd', 'e'] 

# Create the legend patches 
legend_patches = [matplotlib.patches.Patch(color=C, label=L) for 
        C, L in zip([item.get_facecolor() for item in Boxes], 
           legend_labels)] 

# Plot the legend 
plt.legend(handles=legend_patches) 

enter image description here

+0

Nơi nào có 'a, b, c, d, e' đến từ đâu? Điều đó không tương ứng với bất kỳ điều gì trong tập dữ liệu. – mwaskom

+3

Tôi vừa mới tạo ra nó. Nó không có trong tập dữ liệu giả (hoặc nó chỉ là '[0, 1, 2, 3, 4]', giá trị x). Tôi đã loại dự đoán trong tập dữ liệu thực, OP có thể có các nhãn có ý nghĩa liên kết với mỗi lớp. –

+0

Tôi nghĩ OP muốn nhãn huyền thoại là các giá trị duy nhất trong cột 'bước' dataframe –

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