plt.rcParams['font.sans-serif']=['Arial Unicode MS'] #显示中文字体,这段代码我可是找了好长时间 plt.rcParams['axes.unicode_minus']=False def plot_image(i, predictions_array, true_labels, images): predictions_array, true_label, img = predictions_array[i], true_labels[i], images[i] plt.grid(False) plt.xticks([]) plt.yticks([]) plt.grid(False) # 显示照片,以cm 为单位。 plt.imshow(images[i], cmap=plt.cm.binary) # 预测的图片是否正确,黑色底表示预测正确,红色底表示预测失败 predicted_label = np.argmax(prediction[i]) true_label = y_test[i][0] if predicted_label == true_label: color = 'black' else: color = 'red' # plt.xlabel("{} ({})".format(class_names[predicted_label], # class_names[true_label]), # color=color) plt.xlabel("预测{:2.0f}%是{}(实际{})".format(100*np.max(predictions_array), class_names[predicted_label], class_names[true_label]), color=color) def plot_value_array(i, predictions_array, true_label): predictions_array, true_label = predictions_array[i], true_label[i][0] plt.grid(False) plt.xticks(range(10)) plt.yticks([]) thisplot = plt.bar(range(10), predictions_array, color="#777777") plt.ylim([0, 1]) predicted_label = np.argmax(predictions_array) thisplot[predicted_label].set_color('red') thisplot[true_label].set_color('blue') num_rows = 5 num_cols = 3 num_images = num_rows*num_cols plt.figure(figsize=(2*2*num_cols, 2*num_rows)) for i in range(num_images): plt.subplot(num_rows, 2*num_cols, 2*i+1) plot_image(i, prediction, y_test, x_test) plt.subplot(num_rows, 2*num_cols, 2*i+2) plot_value_array(i, prediction, y_test)解释如下代码
时间: 2024-04-02 22:31:36 浏览: 16
这段代码是一个用于绘制图像和数值数组的函数。首先,通过设置字体和解决中文显示问题的代码来准备绘图环境。然后,定义了两个函数:`plot_image()` 和 `plot_value_array()`。`plot_image()`函数用于绘制图像,并根据预测结果的正确性将其标记为黑色或红色。`plot_value_array()`函数用于绘制数值数组,并将预测结果的最大值标记为红色,将真实标签标记为蓝色。接下来,通过循环调用这两个函数来绘制一组图像和相应的数值数组。最终,使用`plt.figure()`函数定义了一个绘图窗口,并在其中调用这两个函数来绘制图像和数值数组。
相关问题
%matplotlib inline import matplotlib.pyplot as plt # Mac 设置显示中文 # plt.rcParams['font.sans-serif'] = 'Arial Unicode MS' # Windows 设置显示中文 plt.rcParams['font.sans-serif'] = 'SimHei' result.plot.bar(figsize=(20, 8))
这段代码是用于绘制一个条形图,并设置显示中文。首先使用 `%matplotlib inline` 命令,确保在 Jupyter Notebook 中绘图的结果能够直接显示出来。然后导入 `matplotlib.pyplot` 模块,并将其命名为 `plt`。接下来,通过设置 `font.sans-serif` 参数来指定字体为中文字体。如果你是在 Mac 上运行代码,可以将注释符号 `#` 去掉,使用 'Arial Unicode MS' 字体;如果你是在 Windows 上运行代码,可以将 `#` 去掉,使用 'SimHei' 字体。最后调用 `result.plot.bar(figsize=(20, 8))` 来绘制条形图,其中 `result` 是要绘制的数据,`figsize=(20, 8)` 设置图像的大小为宽度为20英寸,高度为8英寸。
plt.rcParams['font.sans-serif']=['Arial Unicode MS']
这是一个Python语言中Matplotlib库中的设置字体的代码,将字体设为Arial Unicode MS。这样可以解决在绘制图形时中文乱码的问题。其中plt代表matplotlib库中的pyplot模块,rcParams代表图形参数的设置。通过设置font.sans-serif参数,将字体设为Arial Unicode MS,使得图形中的中文可以正确显示。