python - 无法在 matplotlib 图中设置 xlabel 和 ylabel
问题描述
我是 matplotlib 的新手,无法为我的绘图设置轴标签。我也试过 plt.xlabel("xlabel") 和 ax1.set(xlabel="Images") 但都失败了。有任何想法吗?
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
fig.suptitle("Cross Entropy Loss", fontsize=20)
ax1.set_xlabel('xlabel')
ax1.set_ylabel('ylabel')
def animate(i):
pullData = open("loss1.txt", "r").read()
dataArray = pullData.split('\n')
xar = []
yar = []
for eachLine in dataArray:
if len(eachLine) > 1:
x, y = eachLine.split(',')
xar.append(int(x))
yar.append(int(y))
ax1.clear()
ax1.plot(xar, yar)
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.title("Cross Entropy Loss")
plt.show()
解决方案
您在执行“ax1.clear()”时正在清除轴,因此“ax1.set_xlabel('xlabel')”和“ax1.set_ylabel('ylabel')”没有显示您想要的内容。
要解决这个问题,只需在清除 ax1后同时放置“set_x_label”和“set_y_label” 。代码应类似于以下内容:
...
for eachLine in dataArray:
if len(eachLine) > 1:
x, y = eachLine.split(',')
xar.append(int(x))
yar.append(int(y))
ax1.clear()
ax1.set_xlabel('xlabel')
ax1.set_ylabel('ylabel')
ax1.plot(xar, yar)
...
推荐阅读
- python - 如何在 Colab 中将长 DEAP 日志(10 MM)转换为数据帧时修复 RAM 限制?
- javascript - iOS 键盘未关闭,由 iframe 中的输入触发
- ios - 如何从xml解析字符串ios中删除或替换垃圾值或无效值&?
- r - 计算 R 中的所有列
- vba - 如何删除地址?
- node.js - Express cookie 在浏览器中不可见
- xaml - 如何仅为背景(而不是标签)设置透明度?
- node.js - 如何使用 Node.js 创建虚拟打印机
- bash - 从本地 .bat 脚本运行远程 .sh 脚本
- python - 如果我放一个像 s=1234567891 这样的字符串,代码可以工作,但是如果我有一个文本文件“yesno.txt”,其中有数据,则会出错