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

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()返回 一個顯示縮放位置的矩形,可幫助您顯示曲線的特定部分而無需繪制另一部分。

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

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行,將徽標或公司徽標用作水印。

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版本,因為此方法是新發布的功能。

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

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,一經查實,立即刪除!