5月 272021
""" 使用scatter()绘制一系列点 """ import matplotlib.pyplot as plt # 向scatter()传递两个分别包含x值和y值的列表 x_values = [1, 2, 3, 4, 5] y_values = [1, 4, 9, 16, 25] # 列表传递给scatter()时,依次从每个列表中读取一个值来绘制一个点 plt.scatter(x_values, y_values, s=100) # 设置图表标题并给坐标轴加上标签 plt.title("Square Numbers", fontsize=24) plt.xlabel("Value", fontsize=14) plt.ylabel("Square of Value", fontsize=14) plt.tick_params(axis='both', which='major', labelsize=14) plt.show()
""" 使用scatter()绘制散点图 """ import matplotlib.pyplot as plt # 绘制单个点,使用scatter()函数并传递一对x和y坐标 # plt.scatter(2, 4) # 设置坐标并指定点的尺寸 plt.scatter(2, 4, s=200) # 设置图表标题并给坐标轴加上标签 plt.title("Square Numbers", fontsize=24) plt.xlabel("Value", fontsize=14) plt.ylabel("Square of Value", fontsize=14) # 设置刻度标记的大小 plt.tick_params(axis='both', which='major', labelsize=14) plt.show()
""" https://matplotlib.org/stable/index.html Matplotlib: Visualization with Python Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. """ # 导入模块并指定别名 import matplotlib.pyplot as plt # 提供输入值,解决x坐标从0开始的问题 input_values = [1, 2, 3, 4, 5] # 定义平方数列表 square = [1, 4, 9, 16, 25] # 将列表传递给函数 # plt.plot(square) # 改善图形可读性,线条加粗 # plt.plot(square, linewidth=5) # 同时提供输入值列表和输出值列表 plt.plot(input_values, square, linewidth=5) # 函数title()指定图表标题,参数fontsize指定文字大小 plt.title("Square Numbers", fontsize=24) # 函数xlable()和ylable()为每条轴设置标题并指定文字大小 plt.xlabel("Value", fontsize=14) plt.ylabel("Square of Value", fontsize=14) # 函数tick_parms()设置刻度样式,并将设置刻度文字大小 # axis n. 轴(旋转物体假想的中心线); (尤指图表中的)固定参考轴线,坐标轴; 对称中心线(将物体平分为二); plt.tick_params(axis='both', labelsize=14) # 调用matplotlib查看器,显示绘制的图形 plt.show()