matplotlib相关性_matplotlib的无限灵活性

matplotlib相关性

Matplotlib is a widely-used Python data visualization library that provides a numerous selection of 2D and 3D plots that are very useful for data analysis and machine learning tasks.

Matplotlib是一个广泛使用的Python数据可视化库,它提供了大量的2D和3D图形选择,这对于数据分析和机器学习任务非常有用。

The syntax for creating a complex plot may seem intimidating but it offers a great deal of flexibility. You can pretty much touch on any component on a plot and customize it.

创建复杂图的语法似乎令人生畏,但它提供了很大的灵活性。 您几乎可以触摸绘图上的任何组件并对其进行自定义。

In this post, I will show you how to create a basic plot and customize it with various touches. We will work on the scripting layer which is the matplotlib.pyplot interface. You are most likely to use this interface in your analysis.

在本文中,我将向您展示如何创建基本图并通过各种方式对其进行自定义。 我们将在脚本层(即matplotlib.pyplot接口)上工作。 您最有可能在分析中使用此界面。

import matplotlib.pyplot as plt
%matplotlib inline #render plots in notebook

Everything starts with creating a Figure which is the main Artist object that holds everything together.

一切都始于创建一个Figure ,这是将所有内容组合在一起的主要Artist对象。

plt.figure(figsize=(10,6))

We have created a Figure of size (10,6). Let’s plot a basic bar plot on this figure.

我们创建了一个尺寸为(10,6)的图形。 让我们在该图上绘制一个基本的条形图。

plt.figure(figsize=(10,6))plt.bar(x=[3,4,2,1], height=[3,5,1,6])
Image for post

The ticks on x-axis are too many. It will look better if only ticks for the bars are shown. Also, I want to extend the range on the y-axis so that the highest bar does not reach the top. We can also assign labels on x-ticks which I think looks nicer than plain numbers.

x轴上的刻度线太多。 如果仅显示条的刻度,则看起来会更好。 另外,我想扩展y轴上的范围,以使最高的条形图不会到达顶部。 我们还可以在x点上分配标签,我认为这些标签比普通数字好。

plt.figure(figsize=(10,6))plt.bar(x=[3,4,2,1], height=[3,5,1,6])plt.xticks(ticks=[1,2,3,4], 
labels=['Rome','Madrid','Istanbul','Houston'])plt.yticks(ticks=np.arange(10))
Image for post

The labels look better with a higher punto. Let’s increase it and also rotate the labels using the rotation parameter. It would also be nice to add a title.

较高的punto,标签看起来更好。 让我们增加它,并使用rotation参数旋转标签。 添加标题也很好。

plt.figure(figsize=(10,6))plt.title("Use Your Own Title", fontsize=15)plt.bar(x=[3,4,2,1], height=[3,5,1,6])plt.xticks(ticks=[1,2,3,4], labels=['Rome','Madrid','Istanbul','Houston'], rotation="45", fontsize=12)plt.yticks(ticks=np.arange(10), fontsize=12)
Image for post

We can add a frame to the Figure object with the facecolor parameter. The xlabel and ylabel functions can be used to add axis labels. The width of bars can also be adjusted using the width parameter of the bar function.

我们可以使用facecolor参数将框架添加到Figure对象。 xlabel和ylabel函数可用于添加轴标签。 条形的宽度也可以使用条形函数的width参数进行调整。

plt.figure(figsize=(10,6), facecolor="lightgreen")plt.title("Use Your Own Title", fontsize=15)plt.bar(x=[3,4,2,1], height=[3,5,1,6], width=0.5)plt.xticks(ticks=[1,2,3,4], labels=['Rome','Madrid','Istanbul','Houston'], rotation="45", fontsize=12)plt.yticks(ticks=np.arange(10), fontsize=12)plt.xlabel("Cities", fontsize=14)plt.ylabel("Measurement", fontsize=14)
Image for post

Annotations and texts can be used on plots to make them more informative or deliver a message. Matplotlib allows to add them as well.

注释和文本可用于绘图,以使其更具信息量或传递消息。 Matplotlib也允许添加它们。

We will use an Axes object on the Figure to add text and annotations. In order to locate these add-ons on the Axes, we need to use the coordinate. Thus, it makes sense to also add grid lines which can be achieved by plt.grid() function.

我们将在图上使用Axes对象添加文本和注释。 为了在轴上找到这些附加组件,我们需要使用坐标。 因此,有意义的是还添加可以通过plt.grid()函数实现的网格线。

Let’s first add the grid lines and a text box.

首先添加网格线和一个文本框。

ax = plt.figure(figsize=(10,6), facecolor="lightgreen").add_subplot(111)plt.title("Use Your Own Title", fontsize=15)plt.bar(x=[3,4,2,1], height=[3,5,1,6], width=0.5)plt.xticks(ticks=[1,2,3,4], labels=['Rome','Madrid','Istanbul','Houston'], rotation="45", fontsize=12)plt.yticks(ticks=np.arange(10), fontsize=12)plt.xlabel("Cities", fontsize=14)plt.ylabel("Measurement", fontsize=14)plt.grid(which='both')ax.text(3.5, 8, 'Daily Average', style='italic', fontsize=12,
bbox={'facecolor': 'grey', 'alpha': 0.5, 'pad': 5})
Image for post

The first two parameters of ax.text() function are x and y coordinates, respectively. After we specify the text and style, we add a box around it using the bbox parameter.

ax.text()函数的前两个参数分别是x和y坐标。 指定文本和样式后,使用bbox参数在其周围添加一个框。

Let’s also add an annotation. I don’t want to repeat the same part of code so I will just write the part for annotation. You can add it to the code that produced the previous plot.

我们还要添加一个注释。 我不想重复代码的相同部分,所以我只写该部分用于注释。 您可以将其添加到生成前一个图的代码中。

ax.annotate('highest', xy=(1, 6), xytext=(1.5, 7), fontsize=13,   arrowprops=dict(facecolor='black', shrink=0.05))
Image for post

We have added an arrow pointing at the top of the highest bar. The xy parameter contains the coordinates for the arrow and the xytext parameter specifies the location of the text. Arrowprops, as the name suggests, is used to style the arrow.

我们在最高的栏的顶部添加了一个箭头。 xy参数包含箭头的坐标,而xytext参数指定文本的位置。 顾名思义Arrowprops用于为箭头设置样式。

Once you are happy with your plot, you can save it using plt.savefig() function.

对图满意后,可以使用plt.savefig()函数将其保存。

The purpose of this post was to show you the great flexibility of Matplotlib. The plots we have produced may not be useful at all but they deliver the message. The syntax may look unnecessarily long but that is the price to pay for flexibility.

这篇文章的目的是向您展示Matplotlib的巨大灵活性。 我们制作的图可能根本没有用,但可以传达信息。 语法看起来可能不必要地冗长,但这是为灵活性付出的代价。

It is worth mentioning that this is only a part of what you can create with Matplotlib. For instance, the structure of subplots is a whole different topic. Once you are comfortable with Matplotlib, there is no limit on what you can create.

值得一提的是,这只是使用Matplotlib可以创建的一部分。 例如,子图的结构是一个完全不同的主题。 一旦您对Matplotlib感到满意,就可以创建任何东西。

Thanks for reading. Please let me know if you have any feedback.

谢谢阅读。 如果您有任何反馈意见,请告诉我。

翻译自: https://towardsdatascience.com/unlimited-flexibility-of-matplotlib-7c2a95f2e53e

matplotlib相关性

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值