matplotlib可视化

安装matplotlibpython

sudo apt-get install python-matplotlib

使用code

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from matplotlib import pyplot as plt

input_values = [1, 2, 3, 4, 5]
squares = [1, 4, 9, 16, 25]
plt.plot(input_values, squares, 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', labelsize=14)
plt.show()

使用 scatter() 绘制一系列点orm

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from matplotlib import pyplot as plt

x_values = [1, 2, 3, 4, 5]
y_values = [1, 4, 9, 16, 25]
plt.scatter(x_values, y_values, s=100)  # s为点的大小
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()

计算数据绘制1000个点utf-8

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from matplotlib import pyplot as plt

x_values = list(range(1, 1001))
y_values = [x ** 2 for x in x_values]
plt.scatter(x_values, y_values, s=40)  # s为点的大小
plt.title("Square Numbers", fontsize=24)
plt.xlabel("Value", fontsize=14)
plt.ylabel("Square of Value", fontsize=14)
# 设置每一个坐标轴的取值范围
plt.axis([0, 1100, 0, 1100000])
plt.show()

自定义颜色get

plt.scatter(x_values, y_values, c=(0.8, 0, 0), edgecolors='none', s=40)

颜色映射(colormap)渐变效果input

plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Blues, edgecolors='none', s=40)

要让程序自动将图表保存到文件中,可将对 plt.show() 的调用替换为对 plt.savefig() 的 调用。第一个实参指定要以什么样的文件名保存图表,这个文件将存储到scatter_squares.py(当前文件)所在的 目录中;第二个实参指定将图表多余的空白区域裁剪掉。若是要保留图表周围多余的空白区域, 可省略这个实参。it

plt.savefig('squares_plot.png', bbox_inches='tight')
相关文章
相关标签/搜索