Python数据可视化--matplotlib.pyplot用法示例

绘制简单的折线图

import matplotlib.pyplot as plt
input_values = [1, 2, 3, 4, 5]
squares = [1, 4, 9, 16, 25]
plt.plot(input_values, squares)
plt.show()

导入模块pyplot

input_values为横坐标

squares为纵坐标

plt.show()打开matplotlib查看器,显示绘制图形

修改标签文字和线条粗细

import matplotlib.pyplot as plt
x_values = list(range(1, 6))
y_values = [x**2 for x in x_values]
plt.plot(x_values, y_values, linewidth=5)
# 设置图表标题,并给坐标轴加标签
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
x_values = list(range(1, 6))
y_values = [x**2 for x in x_values]

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()

修改数据点的颜色

plt.scatter(x_values, y_values,c='red', s=100)

或使用三原色格式

plt.scatter(x_values, y_values,c=(0, 0, 0.8), s=100)

自动保存图表

将plt.show替换为plt.savefig()

plt.savefig('squares_plot.png')
则自动将图表命名为squares_plot.png,保存在dang当前文件路径

猜你喜欢

转载自blog.csdn.net/Useless_csdn/article/details/84139818