matplotlib布局
For Visualization in Python, Matplotlib library has been the workhorse for quite some time now. It has held its own even after more nimble rivals with easier code interface and capabilities like seaborn, plotly, bokeh etc. have arrived on the scene. Though Matplotlib may lack the interactive capabilities of the new kids on the block, it does a more than adequate job of visualizing our data exploration tasks in Exploratory Data Analysis(EDA).
对于Python中的可视化而言,Matplotlib库已经成为相当长一段时间的主力军。 即使具有更灵活的代码界面的更灵活的竞争对手以及诸如seaborn,plot,bokeh等功能已经出现在现场,它也保持了自己的地位。 尽管Matplotlib可能缺少新手的互动功能,但它在可视化探索性数据分析(EDA)中可视化我们的数据探索任务所做的工作远远不够。
During EDA, one may come across situations where we want to display a group of related plots as part of a larger picture to drive home our insight. The subplot
function of matplotlib does the job for us. However, in certain situations, we may want to combine several subplots and want to have different aspect ratios for each subplot. How can we achieve this layout, where, essentially some subplots span across several rows/columns of the overall figure?
在EDA期间,我们可能会遇到一些情况,在这些情况下,我们希望将一组相关的图显示为大图的一部分,以推动我们的洞察力。 matplotlib的subplot
功能为我们完成了工作。 但是,在某些情况下,我们可能希望合并多个子图,并希望每个子图具有不同的纵横比。 我们如何才能实现这种布局,在此布局中,实际上有些子图跨越了整个图形的几行/几列?
Enter gridspec
submodule of Matplotlib.
输入gridspec
子模块。
We first need to create an instance of GridSpec
which allows us to specify the total number of rows and columns as arguments in the overall figure along with a figure
object.
首先,我们需要创建一个GridSpec
实例,该实例允许我们指定行和列的总数作为整体图形中的参数以及figure
对象。
We store the GridSpec
instance in a variable called gs
and specify that we want to have 4 rows and 4 columns in the overall figure.
我们将GridSpec
实例存储在名为gs
的变量中,并指定我们希望在整个图中有4行4列。
Now, we need to specify the details of how each subplot will span the rows and columns in the overall figure. It is useful to make a rough sketch on paper as to how you want the subplots to be laid out, so that they don't overlap. Once done, we convey this information through the GridSpec
object we created. The row/column span info is passed in the same index notation we use for subsetting arrays/dataframes with rows and column index numbers starting from zero and using the :
to specify range. The GridSpec
object with the index is passed to the add_subplot
function of the figure
object.
现在,我们需要指定每个子图将如何跨越整个图中的行和列的详细信息。 在纸上粗略地绘制草图,以了解子图的布局方式是有用的,这样子图就不会重叠。 完成后,我们将通过创建的GridSpec
对象传达此信息。 行/列跨度信息以相同的索引符号传递,该索引符号用于设置数组和数据框,其中行和列的索引号从零开始,并使用:
指定范围。 所述GridSpec
与索引对象被传递到add_subplot
所述的功能figure
的对象。
We add an overall title for the figure and remove the ticks to visualize the layout better as the objective here is to demonstrate how we can achieve subplots spanning multiple rows /columns. When you implement this, obviously you will want to add your axis ticks, labels etc. from your dataframe and tweak the spacing and figure size to accommodate these plot elements.
我们为图形添加一个整体标题,并删除刻度线以更好地显示布局,因为此处的目的是演示如何实现跨越多行/列的子图。 当您实现此功能时,显然您会希望从数据框中添加轴刻度,标签等,并调整间距和图形大小以容纳这些绘图元素。
Boom! This may come in handy in multi-variable time series plots where we may want to show the time series plot stretching across the columns in the top row and other uni-variate, multi-variate visualization in the other subplots below. You can customize how your jigsaw looks like by specifying your row/columns in the overall figure and spans of your individual subplots.
繁荣! 这在多变量时间序列图中可能会派上用场,在这里我们可能想要显示跨越顶部行中各列的时间序列图,并在下面的其他子图中显示其他单变量,多变量可视化。 您可以通过在整体图形中指定行/列以及各个子图的跨度来自定义拼图的外观。
In R, achieving the above is ridiculously easy with the patchwork
package in a single line of code with nothing more than +
and /
operators and ( )
to even have nested subplots if you want to go bonkers. Click on the below link to see how you can get this done in R.
在R语言中,使用单行代码中的patchwork
程序包就可以轻松地实现上述目标,而只需要+
和/
运算符和( )
甚至嵌套嵌套的子图就可以了。 单击下面的链接,查看如何在R中完成此操作。
Thanks for reading. If you liked this article, you may also like the one below on how to do EDA with minimal lines of code with maximum output.
谢谢阅读。 如果您喜欢这篇文章,那么您可能也喜欢以下关于如何用最少的代码行和最大的输出量进行EDA的文章。
Would love to hear your feedback and comments. Thanks!
很想听听您的反馈和意见。 谢谢!
翻译自: https://towardsdatascience.com/matplotlib-multi-column-row-spanning-layouts-f026eb7c3c27
matplotlib布局
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390749.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!