matplotlib使用汇总

0. matplotlib.pyplot.plot

https://matplotlib.org/3.5.3/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot

  • plot(x, y) # plot x and y using default line style and color,有博客是说-和b,其中b表示的是蓝色
By default, matplotlib uses a solid blue line for plots. Specifically:

- Line style: solid line (no dashes)
- Line color: blue 
- Line width: 1.5 points

These defaults can be changed by passing arguments to the plot function or by changing the default rcParams. For example:

```
import matplotlib.pyplot as plt

# Plot with default blue solid line
plt.plot([1, 2, 3]) 

# Plot with red dashed line 
plt.plot([1, 2, 3], linestyle='--', color='red')
````

So in summary, the default line style is a solid line, the default line color is blue, and the default line width is 1.5 points. These can be overridden as needed.

1. 画布对象

  画图之前首先要设置画布(figure)对象,使得后面的图形输出在这块规定了大小的画布上,其中参数figsize设置画布大小。

# 得到画布对象
plt.figure(figsize=(width, height))  # unit is inch(英寸)
# 绘制子图,其中index是从1开始计算
plt.subplot(nrows, ncols, index, **kwargs)  # 将画布分为nrows*ncols个子区域, index表示第N个子区域

2. 设置坐标轴的起始和终止值

plt.xlim(0, 30)   # x in [0, 30]
plt.ylim(0, 100) # y in [0, 100]
# 显示单张黑白图片
def show_single_image(img_arr):
    plt.imshow(img_arr, cmap="binary")
    plt.show()

show_single_image(x_train[0])

# 显示多张黑白图片
def show_imgs(n_rows, n_cols, x_data, y_data, class_names):
    assert len(x_data) == len(y_data)
    assert n_rows * n_cols <= len(x_data)

    plt.figure(figsize = (n_cols * 1.4, n_rows * 1.6))
    for row in range(n_rows):
        for col in range(n_cols):
            index = n_cols * row + col 
            plt.subplot(n_rows, n_cols, index+1)
            plt.imshow(x_data[index], cmap="binary",
                       interpolation = 'nearest')
            plt.axis('off')
            plt.title(class_names[y_data[index]])
    plt.show()

class_names = ['T-shirt', 'Trouser', 'Pullover', 'Dress',
               'Coat', 'Sandal', 'Shirt', 'Sneaker',
               'Bag', 'Ankle boot']
show_imgs(3, 5, x_train, y_train, class_names)

3. 显示中文

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

herosunly

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值