您的位置:首页 > 游戏 > 手游 > 中国工程监理人才网_现在最靠谱的购物网站有哪些_长春网络推广优化_百度seo推广

中国工程监理人才网_现在最靠谱的购物网站有哪些_长春网络推广优化_百度seo推广

2025/3/3 17:40:12 来源:https://blog.csdn.net/Lin_Coder_/article/details/145919087  浏览:    关键词:中国工程监理人才网_现在最靠谱的购物网站有哪些_长春网络推广优化_百度seo推广
中国工程监理人才网_现在最靠谱的购物网站有哪些_长春网络推广优化_百度seo推广

目录

一、安装包

二、先画个折线图

1、修改标签文字和线条粗细

2、内置样式

3、scatter() 绘制散点图

4、scatter() 绘制多个点

5、设置样式

6、保存绘图


数据可视化指的是通过可视化表示来探索和呈现数据集内的规律。

一、安装包

win + R   打开终端

安装 Matplotlib,在终端提示符下执行命令:

python -m pip install --user matplotlib## python -m pip:这个部分表示使用 Python 解释器来运行 pip 模块## --user:这个选项表示将包安装到当前用户的目录中,而不是全局环境## matplotlib:这是安装的具体包的名

结果如下:

二、先画个折线图

import matplotlib.pyplot as pltnums = [1, 2, 4, 8, 16, 32, 64]
# nums = [1, 4, 9, 16, 25, 36]
fig, ax = plt.subplots()
ax.plot(nums)
plt.show()# fig, ax = plt.subplots() 创建了一个图形对象 fig 和一个坐标轴对象 ax# ax.plot(nums) 使用 plot 方法绘制 nums 中的数据

变量 fig 表示由生成的一系列绘图构成的整个图形,变量 ax 表示图形中的绘图。

1、修改标签文字和线条粗细

import matplotlib.pyplot as pltnums = [1, 2, 4, 8, 16, 32, 64]
# nums = [1, 4, 9, 16, 25, 36]
fig, ax = plt.subplots()
ax.plot(nums, linewidth=3)ax.set_title("2**n", fontsize=24)
ax.set_xlabel("Value_2", fontsize=14)
ax.set_ylabel("2**n", fontsize=14)# 设置刻度标记的样式
ax.tick_params(labelsize=14)
plt.show()

结果显示:

        参数 linewidth 决定了 plot() 绘制的线条的粗细;set_title() 方法给绘图指定标题;fontsize 用于指定图中各种文字的大小;tick_params() 方法设置刻度标记的样式。

当你运行如下代码时:

import matplotlib.pyplot as plt# nums = [1, 2, 4, 8, 16, 32, 64]
# input_value = [0, 1, 2, 3, 4, 5, 6]nums = [1, 4, 9, 16, 25, 36]
input_value = [1, 2, 3, 4, 5, 6]
fig, ax = plt.subplots()
# ax.plot(input_value, nums, linewidth=3)
ax.plot(nums, linewidth=3)ax.set_title("n**2", fontsize=24)
ax.set_xlabel("Value_n", fontsize=14)
ax.set_ylabel("n**2", fontsize=14)# 设置刻度标记的样式
ax.tick_params(labelsize=14)
plt.show()

        结果输出:

        

        注意:如果看的仔细的话,上边的图是不是不太对,5的平方怎么到36了。

现在我们改动一行代码:

ax.plot(input_value, nums, linewidth=3)

输出结果:

        在向 plot() 提供一个数值序列时,它假设第一个数据点对应的 x 坐标值为 0,但这里的第一个点对应的 x 坐标值应该为 1。因此将输入的数据也提供给 plot() 函数

2、内置样式

Matplotlib 有很多定义好的内置样式,如果想知道有哪些能用的,可以直接打印出来查看:

print(plt.style.available)# plt 是别名
import matplotlib.pyplot as pltnums = [1, 2, 4, 8, 16, 32, 64]
input_value = [0, 1, 2, 3, 4, 5, 6]plt.style.use('classic')
fig, ax = plt.subplots()
ax.plot(input_value, nums, linewidth=3)ax.set_title("2**n", fontsize=24)
ax.set_xlabel("Value_2", fontsize=14)
ax.set_ylabel("2**n", fontsize=14)# 设置刻度标记的样式
ax.tick_params(labelsize=14)
plt.show()
print(plt.style.available)

输出结果:

3、scatter() 绘制散点图

绘制单个点,可使用 scatter() 方法,并向它传递该点的 x 坐标值和 y 坐标值:

import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
fig, ax = plt.subplots()
ax.scatter(2, 4)
plt.show()
print(plt.style.available)

输出结果:

设置样式后:

import matplotlib.pyplot as plt
plt.style.use('bmh')
fig, ax = plt.subplots()
ax.scatter(2, 4, s=200)
# 设置图题并给坐标轴加上标签
ax.set_title("Pingfang", fontsize=24)
ax.set_xlabel("x_value", fontsize=14)
ax.set_ylabel("y_value", fontsize=14)# 设置刻度标记的样式
ax.tick_params(labelsize=14)
plt.show()
print(plt.style.available)

输出结果:

4、scatter() 绘制多个点

import matplotlib.pyplot as pltx_values = [1, 2, 3, 4, 5]
y_values = [1, 4, 9, 16, 25]plt.style.use('bmh')
fig, ax = plt.subplots()
ax.scatter(x_values, y_values, s=100)
# 设置图题并给坐标轴加上标签
ax.set_title("Pingfang", fontsize=24)
ax.set_xlabel("x_value", fontsize=14)
ax.set_ylabel("y_value", fontsize=14)# 设置刻度标记的样式
ax.tick_params(labelsize=14)
plt.show()
print(plt.style.available)

输出结果:

        两个列表传递给 scatter() 时,Matplotlib 会依次从每个列表中读取一个值来绘制一个点。要绘制的点的坐标分别为(1, 1)、(2, 4)、(3, 9)、(4, 16)和(5, 25)。但是这样手动设置列表数据,效率不高,试试自动生成数据,并画图。

import matplotlib.pyplot as pltx_values = range(1, 101)
y_values = [x**2 for x in x_values]plt.style.use('seaborn-v0_8-dark-palette')
fig, ax = plt.subplots()
ax.scatter(x_values, y_values, s=10)
# 设置图题并给坐标轴加上标签
ax.set_title("Pingfang", fontsize=24)
ax.set_xlabel("x_value", fontsize=14)
ax.set_ylabel("y_value", fontsize=14)# 设置刻度标记的样式
ax.tick_params(labelsize=14)
# 设置每个坐标轴的取值范围
ax.axis([0, 110, 0, 11_000])plt.show()
print(plt.style.available)

        使用 axis() 方法指定每个坐标轴的取值范围,x轴和 y 轴各自的最小值和最大值。这里将 x 轴的取值范围设置为 0~110,将 y 轴的取值范围设置为 0~11000。

5、设置样式

# 刻度数值的常规表示,当数字过大时会自动转换为科学计数,如下可以常规表示
ax.ticklabel_format(style='plain')# scatter() 传递参数 color 并将其设置为要使用的颜色
ax.scatter(x_values, y_values, color='red', s=10)# 颜色映射:是一个从起始颜色渐变到结束颜色的颜色序列
ax.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Blues, s=10)

        参数 c 设置成了一个 y 坐标值列表,并使用参数 cmap 告诉 pyplot 使用哪个颜色映射。这些代码将 y 坐标值较小的点显示为浅蓝色,将 y 坐标值较大的点显示为深蓝色。

6、保存绘图

        将绘图保存到文件中:

plt.savefig('nums_plot.png', bbox_inches='tight')

        第一个实参指定要以什么文件名保存绘图,这个文件将被存储到 xxxx.py 所在的程序目录中。第二个实参指定将绘图多余的空白区域裁剪掉。如果要保留绘图周围多余的空白区域,只需省略这个实参。注意:使用 Path对象,将输出文件存储到其他目录。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com