matplotlib第1部分中的高级图

    科技2025-03-06  23

    The Python visualisation landscape consists of several useful python libraries. Every library shines in its own unique way. While some libraries are easy to use, the others have superior functionalities. Matplotlib is one such component of this visualisation ecosystem and a vital one. Matplotlib offers multiple ways to represent numbers into meaningful graphs and plots. The following cheat sheet provides an excellent glimpse of the various functionalities of Matplotlib and how to make our visualizations more effective.

    Python可视化环境由几个有用的python库组成。 每个图书馆都以自己独特的方式闪耀。 尽管某些库易于使用,但其他库则具有出色的功能。 Matplotlib是该可视化生态系统中的此类组件之一,也是至关重要的组件。 Matplotlib提供了多种将数字表示为有意义的图形和图表的方法。 以下备忘单很好地介绍了Matplotlib的各种功能以及如何使我们的可视化效果更有效。

    Matplotlib cheat sheet. Full image: https://lnkd.in/dD5fE8V Matplotlib备忘单。 全图: https : //lnkd.in/dD5fE8V

    The basic capabilities of matplotlib, including the ability to create bar graphs, histograms, pie charts, etc. are well known. However, in this article, I will showcase some of the advanced plots in matplotlib, which can take our analysis a notch higher.

    matplotlib的基本功能(包括创建条形图,直方图,饼图等的功能)是众所周知的。 但是,在本文中,我将展示matplotlib中的一些高级图,这可以使我们的分析高出一个档次。

    Animations in matplotlib is another interesting functionality, and I have a dedicated a complete article to it. The article can be read here: Animations with Matplotlib.

    matplotlib中的动画是另一个有趣的功能,我有专门的完整文章。 可以在这里阅读本文: Matplotlib的动画。

    1.跨度选择器 (1. Span Selector)

    Span Selector is a mouse widget in matplotlib. Widgets are python objects which are used to include some interactive functionality. Span Selector returns the maximum and minimum values of a selected region in a graph, through the mouse selection.

    Span Selector是matplotlib中的鼠标小部件。 窗口小部件是python对象,用于包含一些交互功能。 跨度选择器通过鼠标选择返回图形中选定区域的最大值和最小值。

    In the following code snippet, we first create a basic line plot. Then we call the SpanSelector method and use it first to select a region and then print the maximum and minimum values in that region. Let’s see it in action below.

    在下面的代码片段中,我们首先创建一个基本折线图。 然后,我们调用SpanSelector方法并首先使用它来选择一个区域,然后在该区域中打印最大值和最小值。 让我们看看下面的操作。

    import from import def print(xmin, xmax)return fig, ax = ax.plot([1,2,3,4,5,6,7], [10, 50, 100, 23,15,28,45])span = =True, rectprops=dict(alpha=0.5, facecolor='red')) plt.show() Matplotlib’s span selector in action Matplotlib的跨度选择器在起作用

    2.破碎的条形图—破碎的水平条形图 (2. Broken Barh — Broken Horizontal Bar plot)

    A “broken” horizontal bar plot is a plot that has gaps. It is used in situations when the data has values that vary considerably — for instance, a dataset consisting of extreme temperature ranges. Broken bar charts are ideal in this case since they can plot both the maximum and minimum ranges perfectly.

    “折断”的水平条形图是具有间隙的图。 它用于数据值相差很大的情况下,例如,包含极端温度范围的数据集。 在这种情况下,折断的条形图非常理想,因为它们可以同时绘制最大和最小范围。

    The python module matplotlib.broken_barh() is used to plot a broken horizontal bar chart.

    python模块matplotlib.broken_barh()用于绘制折断的水平条形图。

    import matplotlib.pyplot as plt #Defining the x and y ranges xranges = [(5,5), (20,5),(20,7)] yrange = (2,1) #Plotting the broken bar chart plt.broken_barh(xranges, yrange, facecolors='green') xranges = [(6,2), (17,5),(50,2)] yrange = (15,1) plt.broken_barh(xranges, yrange, facecolors='orange') xranges = [(5,2), (28,5),(40,2)] yrange = (30,1) plt.broken_barh(xranges, yrange, facecolors='red') plt.xlabel('Sales') plt.ylabel('Days of the Month') plt.show() Broken Horizontal Bar plot 水平条形图损坏

    3.表演示 (3. Table Demo)

    Matplotlib’s table function can display a table within a plot. This is especially handy when one wants to see the quickly visualize values in a table in the form of a bar graph keeping the table alongside. The table can be positioned at top, bottom or on sides of the plot. Here is how you can create one easily.

    Matplotlib的表格功能可以在图中显示表格。 当人们希望以条形图的形式快速查看表格中的值时,这特别方便。 表格可以放置在图表的顶部,底部或侧面。 这是您可以轻松创建一个的方法。

    The following example has been taken from a tweet by Just Glowing Python(@ JustGlowing)

    以下示例摘自Just Glowing Python(@ JustGlowing)的一条推文

    import pandas as pd import numpy as np import matplotlib.pyplot as plt x = np.random.rand(5, 8)*.7 plt.plot(x.mean(axis=0), '-o', label='average per column') plt.xticks([]) plt.table(cellText=[['%1.2f' % xxx for xxx in xx] for xx in x],cellColours=plt.cm.GnBu(x),loc='bottom') plt.show() Table plots in matplotlib with a table at the bottom matplotlib中的表图,底部是表 Table plots in matplotlib with a table at the top matplotlib中的表图,顶部是表

    4.水印图像 (4. Watermark Images)

    Sometimes having an image as a watermark helps to add a unique flavour to a plot. For instance, if we were to analyze the earnings of top athletes over the years, having their photographs in the background would help us to differentiate between plots of different players, easily. Let’s analyze a dataset consisting of income of a number of athletes. We shall plot a graph of LeBron James earnings in US$(millions) over the years.

    有时将图像作为水印有助于为情节添加独特的风味。 例如,如果我们要分析这些年来顶尖运动员的收入,那么将他们的照片放在背景中将有助于我们轻松地区分不同运动员的情节。 让我们分析一个由许多运动员的收入组成的数据集。 我们将绘制多年来勒布朗·詹姆斯的收入(百万美元)图表。

    Let’s first import the dataset, picture to be used for watermark and the necessary libraries.

    首先,导入要用于水印的数据集 , 图片和必要的库。

    import numpy as np import matplotlib.image as image import matplotlib.pyplot as plt import pandas as pd df = pd.read_csv('income.csv') im = image.imread('Lebron_James.jpeg') # Image

    The dataset consists of income of a number of athletes. Let’s filter out the data consisting of only Lebron.

    该数据集由许多运动员的收入组成。 让我们过滤掉仅由勒布朗组成的数据。

    lebron_james = df[df['Name']=='LeBron James']

    Displaying the watermarked plot.

    显示水印图。

    fig, ax = plt.subplots() ax.grid() ax.plot('Year','earnings ($ million)',data=lebron_james) ax.set_title("LeBron James earnings in US$(millions)") fig.figimage(im, 60, 40,cmap='ocean', alpha=.2) plt.show()

    5. XKCD图 (5. XKCD Plots)

    Now let’s add some element of fun in our plots. xkcd is a webcomic by Randall Munroe and showcases a lot of humorous plots. These plots regularly make an appearance in a lot of data science presentations, for instance, the one below :

    现在,让我们在情节中添加一些乐趣。 xkcd是Randall Munroe的网络漫画,展示了许多幽默的情节。 这些图经常出现在许多数据科学演示中,例如,以下演示:

    xkcd.com xkcd.com

    Well, if you want to add some twist to your matplotlib plots, you can simply call the xkcd() method on the pyplot object as follows. Here we are working with GDP dataset of India, which shows the GDP growth rate percentage from 2010 from 2019.

    好吧,如果您想在matplotlib图上增加一些扭曲,可以简单地在pyplot对象上调用xkcd()方法,如下所示。 在这里,我们正在处理印度的GDP数据集,该数据集显示了2019年以来2010年的GDP增长率百分比。

    import pandas as pd import matplotlib.pyplot as plt df = pd.read_csv('https://raw.githubusercontent.com/parulnith/Website-articles-datasets/master/India%20GDP%20Growth%20Rate%20.csv', parse_dates=['Year']) df['Year'] = df['Year'].apply(lambda x: pd.Timestamp(x).strftime('%Y')) #calling xkcd() method plt.xkcd(scale=5, length=400) df.plot(x='Year',y='GDP Growth (%)',kind='bar') plt.ylabel('GDP Growth (%)') plt.xticks(rotation=-20) plt.figure(figsize=(10,8)) plt.show()

    结论 (Conclusion)

    These were some of the interesting and advanced functionalities available in matplotib. There are some other cool graphs and plots too, which I shall cover in my next article. In the meantime, grab an interesting dataset and put your newly learnt skills to use to get a good grasp of the topic.

    这些是matplotib中一些有趣且高级的功能。 还有其他一些很酷的图形和图表,我将在下一篇文章中介绍。 同时,获取一个有趣的数据集,并利用您新学习的技能来很好地理解该主题。

    Originally published at parulpandey.com

    最初发布在 parulpandey.com

    翻译自: https://towardsdatascience.com/advanced-plots-in-matplotlib-part-1-30dbb02b09ae

    相关资源:python高级模块matplotlib 数据可视化分析教程
    Processed: 0.013, SQL: 8