Matplotlib 中将数据绘制为点
本文介绍了我们如何在 Matplotlib 中使用 matplotlib.pyplot.scatter()
方法和 matplotlib.pyplot.plot()
方法将数据绘制为点。
使用 matplotlib.pyplot.scatter()
方法将数据绘制成点
matplotlib.pyplot.scatter()
是 Matplotlib 中绘制数据为点的最直接、最标准的方法。我们将要绘制的数据坐标作为参数传递给该方法。
import matplotlib.pyplot as plt
x=[1,2,3,4,5,6]
y=[2,1,5,6,3,9]
plt.scatter(x, y)
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Scatter Plot")
plt.show()
输出:
它从给定的数据点生成一个简单的散点图。我们将 X 和 Y 坐标作为参数传给 scatter()
方法来生成散点图。xlabel()
和 ylabel()
方法将分别设置 X 轴和 Y 轴的标签。title()
方法将设置图的标题。
我们还可以通过改变 scatter()
方法中的 color
和 marker
参数来自定义散点图。
import matplotlib.pyplot as plt
x=[1,2,3,4,5,6]
y=[2,1,5,6,3,9]
plt.scatter(x,y,color="red",marker="v")
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Scatter Plot")
plt.show()
输出:
它生成红色和 v
标记的散点图。
使用 matplotlib.pyplot.plot()
方法将数据绘制成点
默认情况下,matplotlib.pyplot.plot()
方法将用一条线连接所有的点。为了使用 matplotlib.pyplot.plot()
生成散点图,我们将代表标记的字符设置为方法中的第三个参数。
import matplotlib.pyplot as plt
x=[1,2,3,4,5,6]
y=[2,1,5,6,3,9]
plt.plot(x,y,"o",color="red",)
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Scatter Plot")
plt.show()
输出:
它从数据中生成散点图,以 o
为标记,用红色表示数据点。
相关文章
Pandas DataFrame DataFrame.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:Python
-
DataFrame.shift() 函数是将 DataFrame 的索引按指定的周期数进行移位。
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
Pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:Python
-
Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。
Pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:Python
-
本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。
Pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:Python
-
本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串