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.pswp.cn/news/390749.shtml 繁體地址,請注明出處:http://hk.pswp.cn/news/390749.shtml 英文地址,請注明出處:http://en.pswp.cn/news/390749.shtml
如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!