![](/img/trans.png)
[英]How can I make the origin of the x-axis and the origin of the y-axis of a plot overlap in matplotlib?
[英]How can I plot the figure with the number of pixel as a x-axis and the grayscale color as a y-axis in python?
我下载了 MNIST 数据集和 plot 数据集的第一张图像,其中 imshow function 是灰度的。 这是我的代码。
import numpy as np
import matplotlib.pyplot as plt
import torch
import torchvision
from torchvision import transforms
# MNIST dataset
transform = transforms.Compose([transforms.ToTensor()])
train_dataset = torchvision.datasets.MNIST(root='./data',
train=True,
download=True,
transform=transform)
# Change data type : torch.Tensor -> numpy array
X = train_dataset.train_data.numpy()
y = train_dataset.train_labels.numpy()
print('Data size: ' + str(X.shape))
num_samples, height, width = X.shape
dim = height * width
# Visualize MNIST data (1st digit)
plt.figure(1)
plt.imshow(X[0],cmap='gray_r')
plt.colorbar()
plt.title('1st image label: 5')
我得到了这个数字
将二维图像向量重塑为一维向量后,我想将 plot 变成这样的图。
x 轴表示像素数,y 轴表示其像素值作为灰度颜色,plot 就是这样,光谱之类的。 我知道如何重塑矢量,但我不知道如何制作 plot。 我可以在 matplotlib 模块中使用 function 使 plot 像第二个一样吗? 还是在某个地方?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.