您的位置:首页 > 编程语言 > PHP开发

matplotlib plot( ) 绘制简单折线图

2017-11-22 20:44 776 查看

matplotlib plot 绘制简单折线图

1. example 1

import matplotlib.pyplot as plt

squares = [1, 4, 9, 16, 25]
plt.plot(squares)
plt.show()




2. example 2

# example 2
import matplotlib.pyplot as plt

squares = [1, 4, 9, 16, 25]
plt.plot(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()




参数 linewidth 决定了 plot( ) 绘制的线条的粗细。函数 title( ) 给图表指定标题。在上述代码中,出现了多次的参数 fontsize 指定了图表中文字的大小。

函数 xlabel( ) 和 ylabel( ) 让你能够为每条轴设置标题;而函数 tick_params( ) 设置刻度的样式,其中指定的实参将影响 x 轴和 y 轴上的刻度 (axes='both'),并将刻度标记的字号设置为14 (labelsize=14)。

3. example 3

当你向 plot( ) 提供一系列数字时,它假设第一个数据点对应的 x 坐标值为0,但我们的第一个点对应的 x 值为1。为改变这种默认行为,我们可以给plot( ) 同时提供输入值和输出值。

import matplotlib.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()




4. example 4

5. example 5
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: