<i id='y6znZ'><tr id='y6znZ'><dt id='y6znZ'><q id='y6znZ'><span id='y6znZ'><b id='y6znZ'><form id='y6znZ'><ins id='y6znZ'></ins><ul id='y6znZ'></ul><sub id='y6znZ'></sub></form><legend id='y6znZ'></legend><bdo id='y6znZ'><pre id='y6znZ'><center id='y6znZ'></center></pre></bdo></b><th id='y6znZ'></th></span></q></dt></tr></i><div id='y6znZ'><tfoot id='y6znZ'></tfoot><dl id='y6znZ'><fieldset id='y6znZ'></fieldset></dl></div>
    1. <legend id='y6znZ'><style id='y6znZ'><dir id='y6znZ'><q id='y6znZ'></q></dir></style></legend>

      1. <tfoot id='y6znZ'></tfoot>
        • <bdo id='y6znZ'></bdo><ul id='y6znZ'></ul>

        <small id='y6znZ'></small><noframes id='y6znZ'>

      2. 如何在子图中绘制多个 Seaborn 联合图

        How to plot multiple Seaborn Jointplot in Subplot(如何在子图中绘制多个 Seaborn 联合图)

            • <bdo id='Bqazs'></bdo><ul id='Bqazs'></ul>

              <tfoot id='Bqazs'></tfoot>
                <legend id='Bqazs'><style id='Bqazs'><dir id='Bqazs'><q id='Bqazs'></q></dir></style></legend>

                <small id='Bqazs'></small><noframes id='Bqazs'>

                    <tbody id='Bqazs'></tbody>
                  <i id='Bqazs'><tr id='Bqazs'><dt id='Bqazs'><q id='Bqazs'><span id='Bqazs'><b id='Bqazs'><form id='Bqazs'><ins id='Bqazs'></ins><ul id='Bqazs'></ul><sub id='Bqazs'></sub></form><legend id='Bqazs'></legend><bdo id='Bqazs'><pre id='Bqazs'><center id='Bqazs'></center></pre></bdo></b><th id='Bqazs'></th></span></q></dt></tr></i><div id='Bqazs'><tfoot id='Bqazs'></tfoot><dl id='Bqazs'><fieldset id='Bqazs'></fieldset></dl></div>

                  本文介绍了如何在子图中绘制多个 Seaborn 联合图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在将 Seaborn Jointplot 放入多列 subplot 时遇到问题.

                  将 pandas 导入为 pd将 seaborn 导入为 snsdf = pd.DataFrame({'C1': {'a': 1,'b': 15,'c': 9,'d': 7,'e': 2,'f': 2,'g': 6,'h': 5,'k': 5,'l': 8},'C2': {'a': 6,'b': 18,'c': 13,'d': 8,'e': 6,'f': 6,'g': 8,'h': 9,'k': 13,'l': 15}})无花果 = plt.figure();ax1 = fig.add_subplot(121);ax2 = fig.add_subplot(122);sns.jointplot("C1", "C2", data=df, kind='reg', ax=ax1)sns.jointplot("C1", "C2", data=df, kind='kde', ax=ax2)

                  注意 jointplot 的一部分是如何放置在子图中的,而其余部分则留在另外两个图框内.我想要的是将 distributions 也插入到 subplots 中.

                  有人可以帮忙吗?

                  解决方案

                  在 matplotlib 中移动轴不像以前那样容易.以下是使用当前版本的 matplotlib.

                  正如在几个地方指出的那样(

                  请注意,复制轴可能有几个缺点,并且上述内容尚未(尚未)彻底测试.

                  I'm having problem placing Seaborn Jointplot inside a multicolumn subplot.

                  import pandas as pd
                  import seaborn as sns
                  
                  df = pd.DataFrame({'C1': {'a': 1,'b': 15,'c': 9,'d': 7,'e': 2,'f': 2,'g': 6,'h': 5,'k': 5,'l': 8},
                            'C2': {'a': 6,'b': 18,'c': 13,'d': 8,'e': 6,'f': 6,'g': 8,'h': 9,'k': 13,'l': 15}})
                  
                  fig = plt.figure();   
                  ax1 = fig.add_subplot(121);  
                  ax2 = fig.add_subplot(122);
                  
                  sns.jointplot("C1", "C2", data=df, kind='reg', ax=ax1)
                  sns.jointplot("C1", "C2", data=df, kind='kde', ax=ax2)
                  

                  Notice how only a portion of the jointplot is placed inside the subplot and the rest left inside another two plot frames. What I'd want is to have both the distributions also inserted inside the subplots.

                  Can anyone help with this?

                  解决方案

                  Moving axes in matplotlib is not as easy as it used to be in previous versions. The below is working with the current version of matplotlib.

                  As has been pointed out at several places (this question, also this issue) several of the seaborn commands create their own figure automatically. This is hardcoded into the seaborn code, so there is currently no way to produce such plots in existing figures. Those are PairGrid, FacetGrid, JointGrid, pairplot, jointplot and lmplot.

                  There is a seaborn fork available which would allow to supply a subplot grid to the respective classes such that the plot is created in a preexisting figure. To use this, you would need to copy the axisgrid.py from the fork to the seaborn folder. Note that this is currently restricted to be used with matplotlib 2.1 (possibly 2.0 as well).

                  An alternative could be to create a seaborn figure and copy the axes to another figure. The principle of this is shown in this answer and could be extended to Searborn plots. The implementation is a bit more complicated that I had initially expected. The following is a class SeabornFig2Grid that can be called with a seaborn grid instance (the return of any of the above commands), a matplotlib figure and a subplot_spec, which is a position of a gridspec grid.

                  Note: This is a proof of concept, it may work for most easy cases, but I would not recommend using it in production code.

                  import matplotlib.pyplot as plt
                  import matplotlib.gridspec as gridspec
                  import seaborn as sns
                  import numpy as np
                  
                  class SeabornFig2Grid():
                  
                      def __init__(self, seaborngrid, fig,  subplot_spec):
                          self.fig = fig
                          self.sg = seaborngrid
                          self.subplot = subplot_spec
                          if isinstance(self.sg, sns.axisgrid.FacetGrid) or 
                              isinstance(self.sg, sns.axisgrid.PairGrid):
                              self._movegrid()
                          elif isinstance(self.sg, sns.axisgrid.JointGrid):
                              self._movejointgrid()
                          self._finalize()
                  
                      def _movegrid(self):
                          """ Move PairGrid or Facetgrid """
                          self._resize()
                          n = self.sg.axes.shape[0]
                          m = self.sg.axes.shape[1]
                          self.subgrid = gridspec.GridSpecFromSubplotSpec(n,m, subplot_spec=self.subplot)
                          for i in range(n):
                              for j in range(m):
                                  self._moveaxes(self.sg.axes[i,j], self.subgrid[i,j])
                  
                      def _movejointgrid(self):
                          """ Move Jointgrid """
                          h= self.sg.ax_joint.get_position().height
                          h2= self.sg.ax_marg_x.get_position().height
                          r = int(np.round(h/h2))
                          self._resize()
                          self.subgrid = gridspec.GridSpecFromSubplotSpec(r+1,r+1, subplot_spec=self.subplot)
                  
                          self._moveaxes(self.sg.ax_joint, self.subgrid[1:, :-1])
                          self._moveaxes(self.sg.ax_marg_x, self.subgrid[0, :-1])
                          self._moveaxes(self.sg.ax_marg_y, self.subgrid[1:, -1])
                  
                      def _moveaxes(self, ax, gs):
                          #https://stackoverflow.com/a/46906599/4124317
                          ax.remove()
                          ax.figure=self.fig
                          self.fig.axes.append(ax)
                          self.fig.add_axes(ax)
                          ax._subplotspec = gs
                          ax.set_position(gs.get_position(self.fig))
                          ax.set_subplotspec(gs)
                  
                      def _finalize(self):
                          plt.close(self.sg.fig)
                          self.fig.canvas.mpl_connect("resize_event", self._resize)
                          self.fig.canvas.draw()
                  
                      def _resize(self, evt=None):
                          self.sg.fig.set_size_inches(self.fig.get_size_inches())
                  

                  The usage of this class would look like this:

                  import matplotlib.pyplot as plt
                  import matplotlib.gridspec as gridspec
                  import seaborn as sns; sns.set()
                  import SeabornFig2Grid as sfg
                  
                  
                  iris = sns.load_dataset("iris")
                  tips = sns.load_dataset("tips")
                  
                  # An lmplot
                  g0 = sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips, 
                                  palette=dict(Yes="g", No="m"))
                  # A PairGrid
                  g1 = sns.PairGrid(iris, hue="species")
                  g1.map(plt.scatter, s=5)
                  # A FacetGrid
                  g2 = sns.FacetGrid(tips, col="time",  hue="smoker")
                  g2.map(plt.scatter, "total_bill", "tip", edgecolor="w")
                  # A JointGrid
                  g3 = sns.jointplot("sepal_width", "petal_length", data=iris,
                                     kind="kde", space=0, color="g")
                  
                  
                  fig = plt.figure(figsize=(13,8))
                  gs = gridspec.GridSpec(2, 2)
                  
                  mg0 = sfg.SeabornFig2Grid(g0, fig, gs[0])
                  mg1 = sfg.SeabornFig2Grid(g1, fig, gs[1])
                  mg2 = sfg.SeabornFig2Grid(g2, fig, gs[3])
                  mg3 = sfg.SeabornFig2Grid(g3, fig, gs[2])
                  
                  gs.tight_layout(fig)
                  #gs.update(top=0.7)
                  
                  plt.show()
                  

                  Note that there might be several drawbacks from copying axes and the above is not (yet) tested thoroughly.

                  这篇关于如何在子图中绘制多个 Seaborn 联合图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)

                    <i id='y3VSD'><tr id='y3VSD'><dt id='y3VSD'><q id='y3VSD'><span id='y3VSD'><b id='y3VSD'><form id='y3VSD'><ins id='y3VSD'></ins><ul id='y3VSD'></ul><sub id='y3VSD'></sub></form><legend id='y3VSD'></legend><bdo id='y3VSD'><pre id='y3VSD'><center id='y3VSD'></center></pre></bdo></b><th id='y3VSD'></th></span></q></dt></tr></i><div id='y3VSD'><tfoot id='y3VSD'></tfoot><dl id='y3VSD'><fieldset id='y3VSD'></fieldset></dl></div>

                        <small id='y3VSD'></small><noframes id='y3VSD'>

                        <tfoot id='y3VSD'></tfoot>
                      • <legend id='y3VSD'><style id='y3VSD'><dir id='y3VSD'><q id='y3VSD'></q></dir></style></legend>

                          <tbody id='y3VSD'></tbody>

                          <bdo id='y3VSD'></bdo><ul id='y3VSD'></ul>