2015-07-19 4 views
6

मैं पाइथन Seaborn मॉड्यूल के sns.boxplot() के साथ बनाए गए बॉक्सप्लॉट (हरे और नारंगी बक्से के बीच) के बीच एक जगह सेट करने का प्रयास कर रहा हूं। कृपया ग्राफ़ संलग्न करें, कि हरे और नारंगी सबप्लॉट बॉक्स एक-दूसरे पर फंस गए हैं, जिससे यह सबसे आकर्षक नहीं है।पाइथन ग्राफ में बॉक्सप्लॉट्स के बीच स्थान सेट करें Seaborn के साथ नेस्टेड बॉक्स प्लॉट जेनरेट किया?

ऐसा करने का कोई तरीका नहीं मिल रहा है, कोई भी रास्ता ढूंढ सकता है (कोड संलग्न)?

Seaborn Boxplots

import numpy as np 
import pandas as pd 
import matplotlib as mpl 
import matplotlib.pyplot as plt 
import seaborn as sns 
tips = sns.load_dataset("tips") 
sns.set(style="ticks", palette='Set2', font='Roboto Condensed') 
sns.set_context("paper", font_scale=1.1, rc={"lines.linewidth": 1.1}) 
g=sns.factorplot(x="time", y="total_bill", hue="smoker", 
       col="day", data=tips, kind="box", size=4, aspect=0.5, 
       width=0.8,fliersize=2.5,linewidth=1.1, notch=False,orient="v") 
sns.despine(trim=True) 
g.savefig('test6.png', format='png', dpi=600) 

Seaborn boxplot प्रलेखन यहाँ है: http://stanford.edu/~mwaskom/software/seaborn/generated/seaborn.boxplot.html

उत्तर

0

खतरा यह है कि यह अब और की जरूरत नहीं है चल रहा है, मैं इस समस्या का समाधान मिल गया। boxplots सीधे matplotlib के साथ ड्राइंग करते समय, बक्से की व्यवस्था width और position कीवर्ड के साथ नियंत्रित की जा सकती है। हालांकि, जब sns.factorplot(kind='box',...) करने के लिए positions कीवर्ड गुजर, एक एक

TypeError: boxplot() got multiple values for keyword argument 'positions' 

इस के आसपास पाने के लिए, एक के बक्से 'मैन्युअल' के बाद boxplot बना दिया गया है चौड़ाई निर्धारित कर सकते हैं हो जाता है। यह थोड़ा कठिन है, क्योंकि बक्से PatchPatches के रूप में AxesFacedGrid के उदाहरणों के भीतर sns.factorplot द्वारा लौटाए गए हैं। सरल (x,y,width,height) सिंटैक्स के बजाय Rects में PathPatches कोनों को परिभाषित करने के लिए शिखर का उपयोग करें, जिसमें कोई भी अधिक गणना करता है जब कोई बॉक्स को समायोजित करना चाहता है। अन्य सभी के शीर्ष पर, PathPatchesmatplotlib.boxplot द्वारा लौटा Path.CLOSEPOLY कोड के लिए एक अतिरिक्त (अनदेखा) वर्टेक्स होता है, जो (0,0) पर सेट होता है और इसे सबसे अच्छा अनदेखा किया जाता है। बॉक्स के अलावा, मध्यस्थ को चिह्नित करने वाली क्षैतिज रेखा अब बहुत व्यापक है और इसे भी समायोजित करने की आवश्यकता है।

नीचे मैं एक समारोह है कि ओपी के उदाहरण कोड द्वारा उत्पन्न बॉक्स की चौड़ाई को समायोजित कर देता परिभाषित करते हैं (ध्यान दें अतिरिक्त आयात):

from matplotlib.patches import PathPatch 
def adjust_box_widths(g, fac): 
    """ 
    Adjust the withs of a seaborn-generated boxplot. 
    """ 

    ##iterating through Axes instances 
    for ax in g.axes.flatten(): 

     ##iterating through axes artists: 
     for c in ax.get_children(): 

      ##searching for PathPatches 
      if isinstance(c, PathPatch): 
       ##getting current width of box: 
       p = c.get_path() 
       verts = p.vertices 
       verts_sub = verts[:-1] 
       xmin = np.min(verts_sub[:,0]) 
       xmax = np.max(verts_sub[:,0]) 
       xmid = 0.5*(xmin+xmax) 
       xhalf = 0.5*(xmax - xmin) 

       ##setting new width of box 
       xmin_new = xmid-fac*xhalf 
       xmax_new = xmid+fac*xhalf 
       verts_sub[verts_sub[:,0] == xmin,0] = xmin_new 
       verts_sub[verts_sub[:,0] == xmax,0] = xmax_new 

       ##setting new width of median line 
       for l in ax.lines: 
        if np.all(l.get_xdata() == [xmin,xmax]): 
         l.set_xdata([xmin_new,xmax_new]) 

adjust_box_widths(g, 0.9) 

साथ इस कार्यप्रणाली को कॉल निम्नलिखित उत्पादन देता है :

seaborn boxplot with adjusted box width

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