要查看使用matplotlib可制作的各种图表,请访问 http://matplotlib.org/ 的示例画廊。单击画廊中的图表,就可查看用于生成图表的代码。
import matplotlib.pyplot as plt
squares = [1, 4, 9, 16, 25]
plt.plot(squares)
plt.show()
解析:
- 首先要导入
pyplot
模块,这里还给它指定了别名plt
,以简化代码 - 创建一个平方数列
squares = [1, 4, 9, 16, 25]
- 将数列传给函数
plot()
,这个函数尝试根据这些数字绘制出有意义的图形 plt.show()
打开matplotlib
查看器,并显示绘制的图形
修改标签文字和线条的粗细
import matplotlib.pyplot as plt
squares = [1, 4, 9, 16, 25]
## 1. 线条粗细
plt.plot(squares, linewidth=5) # linewidth参数用来设置线条粗细
plt.show()
import matplotlib.pyplot as plt
squares = [1, 4, 9, 16, 25]
## 1. 线条粗细
plt.plot(squares, linewidth=5) # linewidth参数用来设置线条粗细
## 2.设置标题
plt.title('title', fontsize=24) # fontsize参数用来指定文字大小
plt.show()
import matplotlib.pyplot as plt
squares = [1, 4, 9, 16, 25]
## 1. 线条粗细
plt.plot(squares, linewidth=5) # linewidth参数用来设置线条粗细
## 2.设置标题
plt.title('title', fontsize=24) # fontsize参数用来指定文字大小
## 3.为坐标轴添加标签
plt.xlabel('xvalue', fontsize=24)
plt.ylabel('yvalue', fontsize=24)
plt.show()
import matplotlib.pyplot as plt
squares = [1, 4, 9, 16, 25]
## 1. 线条粗细
plt.plot(squares, linewidth=5) # linewidth参数用来设置线条粗细
## 2.设置标题
plt.title('title', fontsize