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.mzph.cn/news/391530.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!