matplotlib第2部分中的高级图

    科技2022-08-01  111

    This article comes as a second part in the Advanced Plots in Matplotlib series. In Part 1, we learnt how to use plots like Span selector, Broken Horizontal Bar plot and the table function in our visualisation work. We also learnt how to watermark images and add a twist to the plots in Matplotlib by using the XKCD theme. In part 2, we shall go over some of the other interesting capabilities of Matplotlib in action and use it to jazz up our existing plots.

    本文是Matplotlib系列中“ 高级图解”的第二部分。 在第1部分中 ,我们学习了如何在可视化工作中使用诸如跨度选择器,水平横断条图和表格功能之类的图。 我们还学习了如何使用XKCD主题为图像加水印并在Matplotlib中为绘图添加扭曲。 在第2部分中,我们将介绍Matplotlib在运行中的其他一些有趣功能,并使用它来完善我们现有的情节。

    1.事件图 (1. Event Plots)

    Event plots come in handy when one wants to plot identical parallel lines at pre-defined positions. Such plots are commonly used in neuroscience to display the spiking activity of the neurons over time, where it is frequently referred to as a spike raster or simply a raster plot. However, there are other uses for such kind of plots too. Here is an excerpt from the event plot documentation :

    当需要在预定位置绘制相同的平行线时,事件图会派上用场。 此类图通常用于神经科学中,显示随时间变化的神经元的尖峰活动,在这里通常将其称为尖峰栅格或简单地称为栅格图。 但是,这种绘图也有其他用途。 这是事件图解文档的摘录:

    It is useful in any situation where you wish to show the timing or position of multiple sets of discrete events, such as the arrival times of people to a business on each day of the month or the date of hurricanes each year of the last century.

    在您希望显示多组离散事件的时间或位置的情况下,例如当人们在一个月的每一天到达公司的时间或上世纪每年的飓风发生的时间时,它都非常有用。

    Let’s look at an eventplot showing sequences of events with various line properties. The plot is shown in both horizontal and vertical orientations. The plot has been adapted from official Matplotlib's Eventplot documentation.

    让我们看一个事件图,该eventplot显示具有各种线属性的事件序列。 该图以水平和垂直方向显示。 该图已从Matplotlib的Eventplot官方文档改编而来。

    Event/Spike Raster plot in Matplotlib | Image by Author Matplotlib中的事件/峰值栅格图 图片作者

    2.时间表 (2. Timelines)

    Did you know that it is possible to create a simple timeline using Matplotlib alone? Well yes. This is because technically, timelines are simply collections of dates and text which can be obtained with simple variations in a stem plot. You can read more about it here.

    您是否知道可以单独使用Matplotlib创建简单的时间表? 嗯,是。 这是因为从技术上讲,时间轴只是日期和文本的集合,可以通过在茎图上进行简单的更改来获得。 您可以在此处了解更多信息。

    Here is a timeline showing the Android version history using the code provided in the official documentation.

    以下是使用官方文档中提供的代码显示Android版本历史记录的时间表。

    Timelines of the Android Version history in Matplotlib| Image by Author Matplotlib |中的Android版本历史的时间表| 图片作者

    3.馅饼 (3. Bar of Pie)

    Have you ever wanted to further drill down into a pie chart? Maybe you wanted to expand one of its slices and ‘explode’ it into a bar chart? Matplotlib makes it possible through a ‘Bar of Pie’ functionality. It uses a ConnectionPatch that connects two points (possibly in different axes).

    您是否曾经想进一步深入查看饼图? 也许您想扩展其一部分并将其“爆炸”为条形图? Matplotlib通过“饼图”功能使之成为可能。 它使用一个ConnectionPatch连接两个点(可能在不同的轴上) 。

    The idea is simple. Create a pie chart and its corresponding bar chart as subplots and then use a connection patch to draw lines between the two subplots.

    这个想法很简单。 创建一个饼图及其对应的条形图作为子图,然后使用连接修补程序在两个子图之间绘制线。

    Here is an example from the official documentation.

    这是官方文档中的示例。

    Bar of Pie Chart from Matplotlib Matplotlib中的饼形图栏

    4.样式表参考 (4. Style sheets reference)

    While creating plots in matplotlib, a lot of times, we tend to stick with the default style. However, Matplotlib offers a bunch of great style options which make even the mundane visualisations really stand out. To list all of the styles, enter the following line of code.

    在matplotlib中创建图时,很多时候,我们倾向于使用默认样式。 但是,Matplotlib提供了许多出色的样式选项,这些选项甚至使平凡的可视化效果也非常突出。 要列出所有样式,请输入以下代码。

    Here I’ll showcase some of the popular ones. In case you are interested, you can find the complete list here. Let’s create a basic line plot and then apply some of the different styles. When no style has been specified, matplotlib will use the default style:

    在这里,我将展示一些受欢迎的产品。 如果您有兴趣,可以在此处找到完整列表。 让我们创建一个基本的线条图,然后应用一些不同的样式。 如果未指定样式,则matplotlib将使用默认样式:

    import matplotlib.pyplot as pltplt.plot([1, 3, 9, 5, 2, 1, 1], marker='o')plt.plot([4, 5, 5, 7, 9, 8, 6], marker='v')plt.plot([2, 3, 4, 3, 4, 5, 3], marker='s')plt.show() Default style in matplotlib (Image by Author) matplotlib中的默认样式(作者提供的图像)

    To add a style of your choice, insert the following line in your code:

    要添加您选择的样式,请在代码中插入以下行:

    plt.style.use('stylename') #Replace 'stylename' with the desired style

    常用样式: (Commonly used styles:)

    Let’s see some of the commonly used styles:

    让我们看一些常用的样式:

    经典 (Classic)

    Classic is Matplotlib’s older style.

    经典是Matplotlib的较旧样式。

    import matplotlib.pyplot as pltplt.style.use(“classic”)plt.plot([1, 3, 9, 5, 2, 1, 1], marker=’o’)plt.plot([4, 5, 5, 7, 9, 8, 6], marker=’v’)plt.plot([2, 3, 4, 3, 4, 5, 3], marker=’s’)plt.show() Classic Style ( Image by Author) 经典风格(作者提供)

    ggplot (ggplot)

    This style emulates the aesthetics of ggplot (a popular plotting package for R)

    这种样式模仿了ggplot ( R的流行绘图包)的美学。

    import matplotlib.pyplot as pltplt.style.use(“ggplot”)plt.plot([1, 3, 9, 5, 2, 1, 1], marker=’o’)plt.plot([4, 5, 5, 7, 9, 8, 6], marker=’v’)plt.plot([2, 3, 4, 3, 4, 5, 3], marker=’s’)plt.show() ggplot style ( Image by Author) ggplot样式(作者提供的图像)

    五十八 (FiveThirtyEight)

    This shows an example of the “fivethirtyeight” styling, which tries to replicate the styles from fivethirtyeight.com

    这显示了一个“ fivethirtyeight”样式的示例,该示例试图复制Fivethirtyeight.com中的样式

    import matplotlib.pyplot as pltplt.style.use(“fivethirtyeight”)plt.plot([1, 3, 9, 5, 2, 1, 1], marker=’o’)plt.plot([4, 5, 5, 7, 9, 8, 6], marker=’v’)plt.plot([2, 3, 4, 3, 4, 5, 3], marker=’s’)plt.show() FiveThirtyEight style( Image by Author) FiveThirtyEight样式(图片由作者)

    贝叶斯黑客方法— bmh (Bayesian Methods for Hackers — bmh)

    This example demonstrates the style used in the Bayesian Methods for Hackers online book.

    此示例演示了贝叶斯黑客方法在线丛书中使用的样式。

    import matplotlib.pyplot as pltplt.style.use("bmh")plt.plot([1, 3, 9, 5, 2, 1, 1], marker='o')plt.plot([4, 5, 5, 7, 9, 8, 6], marker='v')plt.plot([2, 3, 4, 3, 4, 5, 3], marker='s')plt.show() bmh style( Image by Author) bmh风格(作者提供)

    赛博朋克风格 (Cyberpunk style)

    Recently, I stumbled upon a package named mplcyberpunk, which is a Python package on top of matplotlib to create 'cyberpunk' style plots with just three additional lines of code. How cool is that 🤘?

    最近,我偶然发现了一个名为mplcyberpunk的软件包,它是matplotlib之上的Python软件包,用于仅用三行代码创建“ cyberpunk”样式图。 🤘有多酷?

    To use the cyberpunk style; you will need to install and import the library first.

    使用赛博朋克风格; 您将需要先安装和导入该库。

    !pip install mplcyberpunkimport matplotlib.pyplot as pltimport mplcyberpunkplt.style.use("cyberpunk")plt.plot([1, 3, 9, 5, 2, 1, 1], marker='o')plt.plot([4, 5, 5, 7, 9, 8, 6], marker='v')plt.plot([2, 3, 4, 3, 4, 5, 3], marker='s')# Add glow effects-Optionalmplcyberpunk.add_glow_effects()plt.show() CyberPunk Style( Image by Author) 赛博朋克风格(图片由作者提供)

    5. AdjustText — matplotlib自动标签放置 (5. adjustText — automatic label placement for matplotlib)

    Lastly, let’s look at a useful Third-party package in Matplotlib. As the name suggests, third party packages build and extend the existing Matplotlib capabilities. An important point to keep in mind is that these packages are not included in Matplotlib by default and have to be installed separately.

    最后,让我们看一下Matplotlib中有用的第三方程序包 。 顾名思义,第三方软件包可构建和扩展现有的Matplotlib功能。 要记住的重要一点是,这些软件包默认情况下不包含在Matplotlib中,必须单独安装。

    AdjustText (adjustText)

    A lot of times, we struggle to adjust the text positions in a graph. This happens when there are multiple labels, and these labels start overlapping. adjustText is a pretty useful library for such situations as it automates the placement of labels.

    很多时候,我们很难调整图形中的文本位置。 当有多个标签,并且这些标签开始重叠时,会发生这种情况。 AdjustText在这种情况下非常有用,因为它可以自动放置标签。

    安装 (Installation)

    As already stated, you’ll need to install the library first, which can be done by either of the three ways:

    如前所述,您需要首先安装该库,这可以通过以下三种方式之一来完成:

    pip install adjustText # pip installorconda install -c conda-forge adjusttext # condaor# For the latest version from GitHub:pip install https://github.com/Phlya/adjustText/archive/master.zip

    用法 (Usage)

    Let’s look at a basic example from adjustText’s documentation itself to highlight the power of adjustText. The figures on the left show the overlapping labels while the figure on the right shows the auto-adjusted labels obtained after calling the adjust_text function.

    让我们看一下adjustText 文档本身的一个基本示例,以突出adjustText的功能。 左图显示了重叠的标签,而右图显示了在调用adjust_text函数之后获得的自动调整的标签。

    L: before; R: After( Images by Author) L:之前; R:之后(作者提供的图片)

    结语 (Wrap Up)

    This brings us to the end of our two-part series on advanced Matplotlib plots. In this series, we saw how the Matplotlib visualisation library could be leveraged to produce some unique charts. This goes a long way in creating a compelling storyline. Whereas Event Plots are super useful in plotting discrete events, timelines help to portray specific events in history. Again, a bar of pie conveys much more information than a traditional pie chart. Styles like ‘Cyberpunk’ provide an element of classy style which can make some of the visualisations stand out. Hopefully, you’ll be able to utilise some of the information shared in these articles in your visualisations and share your cool projects with the world.

    这使我们结束了有关高级Matplotlib图的两部分系列的结尾。 在本系列中,我们看到了如何利用Matplotlib可视化库来生成一些独特的图表。 这在创建引人入胜的故事情节方面大有帮助。 事件图对于绘制离散事件非常有用,而时间线则有助于描绘历史中的特定事件。 同样,一个饼形图比传统的饼形图传达了更多的信息。 诸如“ Cyber​​punk”之类的样式提供了优雅的风格,可以使某些视觉效果脱颖而出。 希望您能够在可视化中利用这些文章中共享的一些信息,并与世界分享您的出色项目。

    Recommended Course for Matplotlib²

    Matplotlib²推荐课程

    If you are a beginner and want to get started with data visualisation in Matplotlib, Datacamp offers a great course titled — Introduction to Data Visualization with Matplotlib.

    如果您是初学者,并且想开始在Matplotlib中进行数据可视化, Datacamp会提供一门很好的课程,标题为“使用Matplotlib进行数据可视化入门”。

    [1]Originally published at parulpandey.com

    [1]最初发布在parulpandey.com

    [2]This is an Affiliate link

    [2]这是会员链接

    翻译自: https://towardsdatascience.com/advanced-plots-in-matplotlib-part-2-e88f91ce9e31

    Processed: 0.011, SQL: 8