matplotlib可視化_使用Matplotlib改善可視化設計的5個魔術技巧

matplotlib可視化

It is impossible to know everything, no matter how much our experience has increased over the years, there are many things that remain hidden from us. This is normal, and maybe an exciting motivation to search and learn more. And I am sure that this is what drove you to this article.

不可能知道所有事情,無論這些年來我們的經驗增加了多少,很多事情對我們來說都是隱藏的。 這是正常現象,可能是激發人們搜索和學習更多知識的動力。 而且我敢肯定,這就是促使您閱讀本文的原因。

We know that one of Matplotlib’s most important features is its ability to play well with many operating systems and graphics backends. Matplotlib supports dozens of backends and output types, which means you can count on it to work regardless of which operating system you are using or which output format you wish [1].

我們知道Matplotlib最重要的功能之一就是它能夠在許多操作系統和圖形后端上很好地發揮作用。 Matplotlib支持數十種后端和輸出類型,這意味著無論您使用哪種操作系統或希望哪種輸出格式,您都可以依靠它來工作[1]。

I am sharing with you 5 magical tricks and new features I didn’t know about before, to improve your design and visualization skills using Matplotlib. These tricks will lend a helping hand to your work and make it more professional.

我將與您分享5個我以前不知道的魔術和新功能,以提高使用Matplotlib的設計和可視化技能。 這些技巧將為您的工作提供幫助,并使其更加專業。

In case one of these features did not work for you, please update your Matplotlib version using:

如果這些功能之一對您不起作用,請使用以下方法更新Matplotlib版本:

pip install -U matplotlib

Without further ado, let’s get started!

事不宜遲,讓我們開始吧!

技巧1:劇情注釋 (Trick 1: Plots Annotation)

Our first trick for today is annotations which are types of comments added to a plot at a point to make it more understandable, clarify more information, or define the role of that point.

今天,我們的第一個技巧是注釋,即在某一點添加到圖上以使其更易于理解,闡明更多信息或定義該點的作用的注釋類型。

Image for post

To do so, we are going to use plt.annotate() function from Matplotlib. It allows you to create arrows, join them, and make them point to a specific zone. You can adapt the lines above to your own code:

為此,我們將使用Matplotlib中的plt.annotate()函數。 它允許您創建箭頭,將它們連接起來并使它們指向特定區域。 您可以將上面的代碼行修改為自己的代碼:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import randomX=list(range(10))
Y=[x+(x*random.random()) for x in X]fig=plt.figure(figsize=(12,6))plt.plot(X,np.exp(X))
plt.title('Annotation Trick on Anass Elhoud Article')
plt.annotate('Point 1',xy=(6,400),arrowprops=dict(arrowstyle='->'),xytext=(4,600))
plt.annotate('Point 2',xy=(7,1150),arrowprops=dict(arrowstyle='->',connectionstyle='arc3,rad=-.2'),xytext=(4.5,2000))
plt.annotate('Point 3',xy=(8,3000),arrowprops=dict(arrowstyle='-|>',connectionstyle='angle,angleA=90,angleB=0'),xytext=(8.5,2200))
plt.show()

This method will definitely help you to present your work either in writing, in a Latex report, Ph.D. defense, and so on…

這種方法肯定會幫助您以書面形式在Latex報告Ph.D中展示您的工作。 防御等等

技巧2:縮放方法 (Trick 2: Zoom Method)

I think this is the most magical trick to mention. This new feature is so helpful and interesting, especially for researchers and data scientists. The method indicate_inset_zoom() returns a rectangle showing where the zoom is located which helps you show a specific part of the curve without plotting another one.

我認為這是最不可思議的技巧。 這個新功能是如此有用和有趣,特別是對于研究人員和數據科學家而言。 方法notify_inset_zoom()返回 一個顯示縮放位置的矩形,可幫助您顯示曲線的特定部分而無需繪制另一部分。

Image for post

The code above explains how to call the method and add it to your plot:

上面的代碼說明了如何調用該方法并將其添加到繪圖中:

step=.1
x=np.arange(0,10+step,step)
y=x**3fig,ax=plt.subplots(figsize=(12,6))
ax.plot(x,y)
axins=ax.inset_axes([0.1,0.5,0.4,0.4])
axins.plot(x[:10],y[:10])
ax.indicate_inset_zoom(axins,linewidth=3)
axins.set_xticklabels('')
axins.set_xticklabels('')
plt.show()

絕招3:劇情中的水印 (Trick 3: Watermark on the plot)

This trick is useful for solving copyrights issues. It helps you add your watermark to your visualization design. It is not very widely used but it is still an important feature to know about when preparing your visualization project. In this feature, you can either use a text watermark or an image watermark.

此技巧對于解決版權問題很有用。 它可以幫助您將水印添加到可視化設計中。 它的用途不是很廣泛,但在準備可視化項目時仍要知道這一點。 在此功能中,您可以使用文本水印或圖像水印。

Image for post
Text Watermark
文字水印

To add a text watermark, you can use the line below:

要添加文本水印,可以使用以下行:

X=list(range(10))
Y=[x+(x*random.random()) for x in X]fig=plt.figure(figsize=(12,6))plt.plot(X,np.exp(X))
plt.plot(X,2*np.exp(X))
plt.plot(X,4*np.exp(X))
plt.plot(X,5*np.exp(X))
# Text Watermark
fig.text(0.75,0.15, 'Anass Elhoud',fontsize=65, color='gray',ha='right', va='bottom', alpha=0.4,rotation=25)
plt.legend(loc='upper center',ncol=2,frameon=False)  
plt.show()

You can also use your logo or your company’s logo as a watermark by changing the lines 11, 12, and 13 on the code above.

您還可以通過更改上面代碼中的第11、12和13行,將徽標或公司徽標用作水印。

Image for post
Image Watermark
圖像水印

Once you added your logo to the same directory as your main file, you can use this code instead of the text watermark. Do not forget to modify “tds.png” by your own logo name:

將徽標添加到與主文件相同的目錄后,就可以使用此代碼代替文本水印。 不要忘記用您自己的徽標名稱修改“ tds.png”:

import matplotlib.image as imgav_logo=img.imread(fname='tds.png')
fig.figimage(av_logo,900,400,alpha=0.3)
plt.show()

技巧4:共享軸 (Trick 4: Sharing axes)

These new methods allow sharing axes immediately after creating them. Which gives an attractive and arranged look to your visualizations. Please make sure you update your Matplotlib version before using the code because this method is a newly published feature.

這些新方法允許在創建軸后立即共享軸。 這為您的可視化提供了吸引人的外觀。 使用此代碼之前,請確保您更新了Matplotlib版本,因為此方法是新發布的功能。

Image for post

The code source is written below:

代碼源如下所示:

np.random.seed(0)
x = np.random.random(100) * 100 + 20
y = np.random.random(100) * 50 + 25
c = np.random.random(100) - 0.5fig = plt.figure(constrained_layout=True)
axd = fig.subplot_mosaic([['.', 'histx'], ['histy', 'scat']],gridspec_kw={'width_ratios': [1, 7],'height_ratios': [2, 7]})axd['histy'].invert_xaxis()
axd['histx'].sharex(axd['scat'])
axd['histy'].sharey(axd['scat'])im = axd['scat'].scatter(x, y, c=c, cmap='RdBu', picker=True)
fig.colorbar(im, orientation='horizontal', ax=axd['scat'], shrink=0.8)
axd['histx'].hist(x)
axd['histy'].hist(y, orientation='horizontal')
plt.show()

技巧5:無限線 (Trick 5: Infinite lines)

This trick is useful for Data Scientists and Machine Learning Engineers as well. It creates an infinite line passing through two points. It can be useful for separating clusters, groups, or plots.

這個技巧對數據科學家和機器學習工程師也很有用。 它創建了一條穿過兩個點的無限線。 這對于分離聚類,組或圖很有用。

Image for post

Use the following code source to check this feature and let me see how would you apply it in your data visualization project:

使用以下代碼源檢查此功能,讓我看看如何將其應用到數據可視化項目中:

fig, ax = plt.subplots()ax.axline((.1, .1), slope=5, color='C0', label='by slope')
ax.axline((.1, .2), (.8, .7), color='C3', label='by points')ax.legend()
plt.show()

Data Scientist, ML Engineer, Data Analyst, or Business Analyst, you are surely aware of the power of Matplotlib and what is capable of. It is one of the best tools that help us tell stories efficiently and powerfully, linking our analysis to business objectives and get interpretation and decisions as results.

數據科學家,ML工程師,數據分析師或業務分析師肯定會知道Matplotlib的功能以及功能。 它是幫助我們高效有力地講故事,將我們的分析與業務目標聯系起來并獲得解釋和決策結果的最佳工具之一。

With these features, you will be able to add both elegance and professionalism to your academic or official projects. And you will make it easier for the readers and reviewers to understand and keep track of the information and the interpretation resulted from the data visualization.

有了這些功能,您將能夠為您的學術或官方項目增添優雅和專業。 而且,您將使讀者和審閱者更容易理解和跟蹤信息以及數據可視化產生的解釋。

There is just something extraordinary about a well-designed visualization: the colors stand out, the layers harmonize, the contours fit into the whole design, and it not only has a beautiful aesthetic quality but also gives us a meaningful insight. You know that data visualization is like telling a story, that’s why you should make it professional and enjoyable as much as you can.

精心設計的可視化功能有一些與眾不同:顏色突出,層次協調,輪廓適合整個設計,不僅具有美麗的美學品質,而且還為我們提供了有意義的見解。 您知道數據可視化就像講一個故事,這就是為什么您應該使它盡可能專業和有趣。

In the end, note that if you are not familiar with Matplotlib, I recommend you check this interesting article for beginners.

最后,請注意,如果您不熟悉Matplotlib,建議您閱讀此有趣的文章供初學者使用。

Thank you for reading. Stay tuned and follow for upcoming features!

感謝您的閱讀。 請繼續關注并關注即將推出的功能!

翻譯自: https://towardsdatascience.com/5-magical-tricks-to-improve-your-visualization-design-using-matplotlib-dc47623f8cea

matplotlib可視化

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391530.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391530.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391530.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

adb 多點觸碰_無法觸及的神話

adb 多點觸碰On Twitter, in Slack, on Discord, in IRC, or wherever you hang out with other developers on the internet, you may have heard some formulation of the following statements:在Twitter上,在Slack中,在Discord中,在IRC…

robot:循環遍歷數據庫查詢結果是否滿足要求

使用list類型變量{}接收查詢結果,再for循環遍歷每行數據,取出需要比較的數值 轉載于:https://www.cnblogs.com/gcgc/p/11424114.html

leetcode 74. 搜索二維矩陣(二分)

編寫一個高效的算法來判斷 m x n 矩陣中,是否存在一個目標值。該矩陣具有如下特性: 每行中的整數從左到右按升序排列。 每行的第一個整數大于前一行的最后一個整數。 示例 1: 輸入:matrix [[1,3,5,7],[10,11,16,20],[23,30,34…

rm命令

命令 ‘rm’ (remove):刪除一個目錄中的一個或多個文件或目錄,也可以將某個目錄及其下屬的所有文件及其子目錄均刪除掉 語法:rm(選項)(參數) 默認會提示‘是否’刪除&am…

javascript消除字符串兩邊空格的兩種方式,面向對象和函數式編程。python oop在調用時候的優點...

主要是javascript中消除字符串空格,比較兩種方式的不同 //面向對象,消除字符串兩邊空格 String.prototype.trim function() { return this.replace(/(^\s*)|(\s*$)/g, ""); };//去左右空格的函數; function trim(s){return s.replace(/(^\s*)…

如何使用Retrofit,OkHttp,Gson,Glide和Coroutines處理RESTful Web服務

Kriptofolio應用程序系列-第5部分 (Kriptofolio app series — Part 5) These days almost every Android app connects to internet to get/send data. You should definitely learn how to handle RESTful Web Services, as their correct implementation is the core knowle…

leetcode 90. 子集 II(回溯算法)

給你一個整數數組 nums ,其中可能包含重復元素,請你返回該數組所有可能的子集(冪集)。 解集 不能 包含重復的子集。返回的解集中,子集可以按 任意順序 排列。 示例 1: 輸入:nums [1,2,2] 輸…

robot:linux下安裝robot環境

https://www.cnblogs.com/lgqboke/p/8252488.html 轉載于:https://www.cnblogs.com/gcgc/p/11425588.html

感知器 機器學習_機器學習感知器實現

感知器 機器學習In this post, we are going to have a look at a program written in Python3 using numpy. We will discuss the basics of what a perceptron is, what is the delta rule and how to use it to converge the learning of the perceptron.在本文中&#xff0…

JS解析格式化Json插件,Json和XML互相轉換插件

Json對象轉換為XML字符串插件 http://www.jsons.cn/Down/jquery.json2xml.js var xml_content $.json2xml(json_object);XML字符串轉換為Json對象插件 http://www.jsons.cn/Down/jquery.xml2json.js var json_obj $.xml2json(xml_content);json序列化和反序列化方法插件 …

Python之集合、解析式,生成器,函數

一 集合 1 集合定義: 1 如果花括號為空,則是字典類型2 定義一個空集合,使用set 加小括號使用B方式定義集合時,集合內部的數必須是可迭代對象,數值類型的不可以 其中的值必須是可迭代對象,其中的元素必須是可…

深度神經網絡課程總結_了解深度神經網絡如何工作(完整課程)

深度神經網絡課程總結Even if you are completely new to neural networks, this course from Brandon Rohrer will get you comfortable with the concepts and math behind them.即使您是神經網絡的新手,Brandon Rohrer的本課程也會使您熟悉其背后的概念和數學。 …

leetcode 1006. 笨階乘

通常,正整數 n 的階乘是所有小于或等于 n 的正整數的乘積。例如,factorial(10) 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1。 相反,我們設計了一個笨階乘 clumsy:在整數的遞減序列中,我們以一個固定順序的操作符序列來…

python:如何傳遞一個列表參數

轉載于:https://www.cnblogs.com/gcgc/p/11426356.html

curl的安裝與簡單使用

2019獨角獸企業重金招聘Python工程師標準>>> windows 篇: 安裝篇: 我的電腦版本是windows7,64位,對應的curl下載地址如下: https://curl.haxx.se/download.html 直接找到下面的這個版本: curl-7.57.0.tar.g…

gcc 編譯過程

gcc 編譯過程從 hello.c 到 hello(或 a.out)文件, 必須歷經 hello.i、 hello.s、 hello.o,最后才得到 hello(或a.out)文件,分別對應著預處理、編譯、匯編和鏈接 4 個步驟,整個過程如圖 10.5 所示。 這 4 步大致的工作內容如下&am…

虎牙直播電影一天收入_電影收入

虎牙直播電影一天收入“美國電影協會(MPAA)的首席執行官J. Valenti提到:“沒有人能告訴您電影在市場上的表現。 直到電影在黑暗的劇院里放映并且銀幕和觀眾之間都散發出火花。 (“The CEO of Motion Picture Association of America (MPAA) J. Valenti mentioned th…

郵箱如何秘密發送多個人郵件_如何發送秘密消息

郵箱如何秘密發送多個人郵件Cryptography is the science of using codes and ciphers to protect messages, at its most basic level. Encryption is encoding messages with the intent of only allowing the intended recipient to understand the meaning of the message.…

leetcode 面試題 17.21. 直方圖的水量(單調棧)

給定一個直方圖(也稱柱狀圖),假設有人從上面源源不斷地倒水,最后直方圖能存多少水量?直方圖的寬度為 1。 上面是由數組 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的直方圖,在這種情況下,可以接 6 個單位的水(藍色部分表示水&a…

python:動態參數*args

動態參數 顧名思義,動態參數就是傳入的參數的個數是動態的,可以是1個、2個到任意個,還可以是0個。在不需要的時候,你完全可以忽略動態函數,不用給它傳遞任何值。 Python的動態參數有兩種,分別是*args和**kw…