plt.imshow()更改seaborn.countplot()的输出

时间:2022-01-12 20:05:44

I'm trying to run this kaggle kernel on my Spyder IDE. Because I'm not using the Jupyter notebook, I can't use %matplotlib inline, however, I'm quite sure it is not related to my problem...

我正在尝试在我的Spyder IDE上运行这个kaggle内核。因为我没有使用Jupyter笔记本,我不能使用%matplotlib内联,但是,我很确定它与我的问题无关...

I read the data and plotted it once using seaborn, as shown in the kernel, and got the expected output:

我读取数据并使用seaborn绘制一次,如内核中所示,并得到预期的输出:

# Load the data
train = pd.read_csv("./input/train.csv")
test = pd.read_csv("./input/test.csv")

Y_train = train["label"]

# Drop 'label' column
X_train = train.drop(labels = ["label"],axis = 1) 

# free some space
del train 

# print and plot digit count
g = sns.countplot(Y_train)
#print (Y_train.value_counts())

plt.imshow()更改seaborn.countplot()的输出

I added the next lines in the kernel:

我在内核中添加了下一行:

# Normalize the data
X_train = X_train / 255.0
test = test / 255.0

# Reshape image in 3 dimensions (height = 28px, width = 28px , canal = 1)
X_train = X_train.values.reshape(-1,28,28,1)
test = test.values.reshape(-1,28,28,1)

# Encode labels to one hot vectors (ex : 2 -> [0,0,1,0,0,0,0,0,0,0])
Y_train = to_categorical(Y_train, num_classes = 10)

# Set the random seed
random_seed = 2

# Split the train and the validation set for the fitting
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=random_seed)

# Some examples
g = plt.imshow(X_train[0][:,:,0])

The kernel's output is this:

内核的输出是这样的:

plt.imshow()更改seaborn.countplot()的输出

but for some reason mine is:

但出于某种原因,我的是:

plt.imshow()更改seaborn.countplot()的输出

I don't understand why it changes my original image (now only the squeezed image is shown) and does not show the digit image

我不明白为什么它会改变我的原始图像(现在只显示挤压图像)并且不显示数字图像


Here is my entire code so far (removed the %matplotlib inline line and that's about it (add prints)):

这是我到目前为止的整个代码(删除了%matplotlib内联行,这是关于它(添加打印)):

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import seaborn as sns

from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import itertools

from keras.utils.np_utils import to_categorical # convert to one-hot-encoding
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras.optimizers import RMSprop
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau

np.random.seed(2)
sns.set(style='white', context='notebook', palette='deep')

def system_info():
    print ('keras: %20s' % keras.__version__)
    print ('numpy: %20s' % np.__version__)
    print ('pandas: %19s' % pd.__version__)
    print ('seaborn: %18s' % sns.__version__)

# Load the data
train = pd.read_csv("./input/train.csv")
test = pd.read_csv("./input/test.csv")

Y_train = train["label"]

# Drop 'label' column
X_train = train.drop(labels = ["label"],axis = 1) 

# free some space
del train 

# print and plot digit count
g = sns.countplot(Y_train)               # <------ 1st plot. Works fine if the code ends here
#print (Y_train.value_counts())

# Check the data - check for missing data
#print(X_train.isnull().any().describe())
#print(test.isnull().any().describe())

# Normalize the data
X_train = X_train / 255.0
test = test / 255.0

# Reshape image in 3 dimensions (height = 28px, width = 28px , canal = 1)
X_train = X_train.values.reshape(-1,28,28,1)
test = test.values.reshape(-1,28,28,1)

# Encode labels to one hot vectors (ex : 2 -> [0,0,1,0,0,0,0,0,0,0])
Y_train = to_categorical(Y_train, num_classes = 10)

# Set the random seed
random_seed = 2

# Split the train and the validation set for the fitting
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=random_seed)

# Some examples
g = plt.imshow(X_train[0][:,:,0])   # <------- 2nd plot. Removing this gives me back the bar table, but not the digit image expected

1 个解决方案

#1


1  

You're plotting one plot on top of another. Try something along the lines of:

你正在将一块地块绘制在另一块之上。尝试以下方面的内容:

# beginning of your code here...
# open new figure and make first plot
plt.figure()
g = sns.countplot(Y_train) 
# rest of your code here
# open second figure and make second plot
plt.figure()
s = plt.imshow(X_train[0][:,:,0])
# showing your plots will show both plots separately
plt.show()

#1


1  

You're plotting one plot on top of another. Try something along the lines of:

你正在将一块地块绘制在另一块之上。尝试以下方面的内容:

# beginning of your code here...
# open new figure and make first plot
plt.figure()
g = sns.countplot(Y_train) 
# rest of your code here
# open second figure and make second plot
plt.figure()
s = plt.imshow(X_train[0][:,:,0])
# showing your plots will show both plots separately
plt.show()