2015-08-28 9 views
7

मैं निम्नलिखित कोड है:Seaborn 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() 

कौन इस साजिश में आता है:

enter image description here

आप देख सकते हैं मैं कथा गलत। मैं इसे सही कैसे बना सकता हूं?

+0

आप का उपयोग करना चाहिए 'factorplot', या आप वास्तव में' उपयोग करने के लिए FacteGrid' सीधे, आप 'map' में' hue' चर पास करना चाहते हैं। – mwaskom

+0

@mwaskom बहुत बहुत धन्यवाद। क्या आप उदाहरण दे सकते हैं? मैंने कोशिश की लेकिन असफल रहा 'grid.map (sns.barplot, "step", "position", hue = "step", palette = "set3")। Add_legend(); ' – neversaint

+0

यह तीसरा स्थितित्मक तर्क है। लेकिन आपको वास्तव में 'factorplot' का उपयोग करना चाहिए ... – mwaskom

उत्तर

10

कुछ सबप्लॉट के लिए एक किंवदंती वस्तु कैसे है। ऐसा लगता है कि अगर हम प्रत्येक सबप्लॉट में सलाखों के अनुरूप है, तो हमें मैन्युअल रूप से उन्हें बनाना होगा।

# 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

कहां से 'ए, बी, सी, डी, ई' आते हैं? यह डेटासेट में किसी भी चीज़ के अनुरूप नहीं है। – mwaskom

+3

मैंने अभी इसे बनाया है। यह डमी डेटासेट में नहीं था (या यह सिर्फ '[0, 1, 2, 3, 4]', x मान है)। मैं असली डेटासेट में अनुमान लगा रहा था, ओपी के पास प्रत्येक वर्ग से जुड़े सार्थक लेबल हो सकते हैं। –

+0

मुझे लगता है कि ओपी चाहता है कि किंवदंती लेबल डेटाफ्रेम 'चरण' कॉलम में अद्वितीय मान हों –

संबंधित मुद्दे