首页 > 编程知识 正文

Python画双折线图的实现方法

时间:2023-11-20 16:14:16 阅读:291112 作者:LFZE

如果你需要将两个对象的变化趋势对比,并且表达出它们的关联或者差异,双折线图是一种非常好的可视化方式。本文将以Python实现双折线图为中心,从多个方面为大家详细阐述。

一、matplotlib库的介绍

要在Python中绘制双折线图,最常用和最受欢迎的库是matplotlib。matplotlib 是一个 Python 的数据可视化库。它能将数据图形化,提供多种输出格式,支持多种操作系统,并且易于使用。使用matplotlib库可以帮助你向你的数据添加更多维度,方便他人阅读解释数据的趋势,从而做出更明智的决策。

二、绘制基础双折线图

下面的示例代码展示了如何使用matplotlib库在一个基础双折线图上加上数据。

import matplotlib.pyplot as plt

# 设置x轴和y轴的数据
x = [1, 2, 3, 4, 5]
y1 = [0.5, 0.6, 0.8, 0.7, 0.9]
y2 = [0.4, 0.7, 0.9, 0.6, 0.8]

# 绘制基础折线图
plt.plot(x, y1, label='Line 1', linewidth=3, color='green', linestyle='--')
plt.plot(x, y2, label='Line 2', linewidth=3, color='blue', linestyle='-.') 

# 设置图像标签
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Double Line Graph')

# 设置图例, legend函数中的loc参数指定了图例的位置
plt.legend(loc="upper right")

# 显示图像
plt.show()

在上述示例代码中,首先导入matplotlib库,然后定义了x轴和y轴的数据。我们使用plt.plot函数绘制了两个折线,它们分别对应数据y1和y2,两条线分别用绿色虚线和蓝色虚点线标识出来。xlabel函数和ylabel函数用来标识x轴和y轴的含义,而title函数用来添加标题。legend函数用来在图表中加入图例来标识两个数据线,最终使用plt.show函数将图像显示出来。

三、绘制数据点和折线

接下来我们将给大家介绍如何使用Python代码在折线图中加入数据点和折线。

import matplotlib.pyplot as plt

# 设置x轴和y轴的数据
x = [1, 2, 3, 4, 5]
y1 = [0.5, 0.6, 0.8, 0.7, 0.9]
y2 = [0.4, 0.7, 0.9, 0.6, 0.8]

# 绘制基础折线图及数据点
plt.plot(x, y1, label='Line 1', linewidth=3, color='green', linestyle='--', marker='o', markersize=10)
plt.plot(x, y2, label='Line 2', linewidth=3, color='blue', linestyle='-.', marker='s', markersize=10) 

# 设置图像标签
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Double Line Graph')

# 设置图例, legend函数中的loc参数指定了图例的位置
plt.legend(loc="upper right")

# 显示图像
plt.show()

在这个例子中,我们使用标记(marker),用来突出更多的数据点。用标记小圆圈代表 Line1 ,⩰型标注代表 Line2。markersize参数调整标记的大小。除此之外,其余部分与上一个示例类似。

四、添加网格线

在做数据分析时,有时候需要一些额外的信息来突出显示数据,例如绘制网格,这样可以使折线图看起来更加规整清晰。

import matplotlib.pyplot as plt

# 设置x轴和y轴的数据
x = [1, 2, 3, 4, 5]
y1 = [0.5, 0.6, 0.8, 0.7, 0.9]
y2 = [0.4, 0.7, 0.9, 0.6, 0.8]

# 绘制基础折线图及数据点
plt.plot(x, y1, label='Line 1', linewidth=3, color='green', linestyle='--', marker='o', markersize=10)
plt.plot(x, y2, label='Line 2', linewidth=3, color='blue', linestyle='-.', marker='s', markersize=10) 

# 设置网格line_style='-', color='gray', linewidth=0.5
plt.grid(axis='y', alpha=0.75, linestyle='--', color='gray', linewidth=0.5)

# 设置图像标签
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Double Line Graph')

# 设置图例legend函数中的loc参数指定了图例的位置
plt.legend(loc="upper right")

# 显示图像
plt.show()

在上述示例代码中,添加了一个网格,这样可以很好的突出数据轴线的数值。

五、添加填充区域

双折线图中的填充区域通常用来强调两个变量之间的差异或关联性,在matplotlib库中可以很容易地实现这一点。

import matplotlib.pyplot as plt

# 设置x轴和y轴的数据
x = [1, 2, 3, 4, 5]
y1 = [0.5, 0.6, 0.8, 0.7, 0.9]
y2 = [0.4, 0.7, 0.9, 0.6, 0.8]

# 绘制基础折线图及数据点
plt.plot(x, y1, label='Line 1', linewidth=3, color='green', linestyle='--')
plt.plot(x, y2, label='Line 2', linewidth=3, color='blue', linestyle='-.') 

# 添加填充区域
plt.fill_between(x, y1, y2, alpha=.25)

# 设置图像标签
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Double Line Graph')

# 设置图例legend函数中的loc参数指定了图例的位置
plt.legend(loc="upper right")

# 显示图像
plt.show()

在上述示例代码中,我们使用两个颜色不同的折线y1和y2,并使用plt.fill_between(x, y1, y2, alpha=.25)函数将两条线之间的区域填充起来。此外,除了添加填充区域之外,其他的代码与前一个示例相似。

六、总结

本文为大家详细介绍了使用Python绘制双折线图的实现方法。我们从多个方面阐述了Python绘制双折线图的方法,包括绘制基础双折线图、绘制数据点和折线、添加网格线、添加填充区域。这些技巧将帮助你更好地解释数据的趋势,以便做出更明智的决策。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。