#AttributeError: 'FigureCanvasInterAgg' object has no attribute 'tostring_rgb'. Did you mean: 'tostring_argb'?
#import matplotlib.pyplot as plt
#========================
# This can be work
# import matplotlib
# matplotlib.use('TkAgg')
# import matplotlib.pyplot as plt
#=========================
with open('notebook.txt', encoding='utf-8') as file:
# contents = file.read()
# print(contents)
# for line in file:
# print('line:', line)
contents = file.readlines()
print(contents)
newList = []
for content in contents:
newContent = content.replace('\n', '')
money = newContent.split(':')[-1]
newList.append(int(money))
# 6月: 9000
# contents = content.replace('\n', '')
print(newList)
x = [1, 2, 3, 4, 5, 6]
y = newList
plt.plot(x, y, 'r')
plt.xlabel('month')
plt.ylabel('money')
plt.legend()
plt.show()
1月: 7000
2月: 10000
3月: 15000
4月: 12000
5月: 13000
6月: 9000
I am learning to draw graphs with matplotlib, but import matplolib.plylot as plt does not recognize the data. I have pip installed matplotlib, but I suspect it is not installed in the right path. Is there any way to solve this problem?
#AttributeError: 'FigureCanvasInterAgg' object has no attribute 'tostring_rgb'. Did you mean: 'tostring_argb'?
#import matplotlib.pyplot as plt
#========================
# This can be work
# import matplotlib
# matplotlib.use('TkAgg')
# import matplotlib.pyplot as plt
#=========================
with open('notebook.txt', encoding='utf-8') as file:
# contents = file.read()
# print(contents)
# for line in file:
# print('line:', line)
contents = file.readlines()
print(contents)
newList = []
for content in contents:
newContent = content.replace('\n', '')
money = newContent.split(':')[-1]
newList.append(int(money))
# 6月: 9000
# contents = content.replace('\n', '')
print(newList)
x = [1, 2, 3, 4, 5, 6]
y = newList
plt.plot(x, y, 'r')
plt.xlabel('month')
plt.ylabel('money')
plt.legend()
plt.show()
1月: 7000
2月: 10000
3月: 15000
4月: 12000
5月: 13000
6月: 9000
I am learning to draw graphs with matplotlib, but import matplolib.plylot as plt does not recognize the data. I have pip installed matplotlib, but I suspect it is not installed in the right path. Is there any way to solve this problem?
Share Improve this question asked Jan 18 at 6:59 algh404algh404 532 silver badges3 bronze badges2 Answers
Reset to default 6This seems to be an issue with matplotlib 3.10.0 as can be seen in this GitHub issue.
Downgrading to matplotlib<3.10
solved it for a couple of users.
See whether installing version 3.9 fixes your issue pip install matplotlib==3.9.0
or try other lower versions mentioned in the issue.
Be aware that some older versions may not be compatible with recent releases of Python. See Release history.
The following code runs successfully on my computer, and my maplotlib verson is 3.7.1 if you don't know your matplotlib verson,you can press "ctrl" and 'r',then input "cmd" to open the terminal,and input "pip list",then you can find your matlotlib version
import matplotlib.pyplot as plt
from matplotlib import rcParams
# 设置支持中文字体
rcParams['font.sans-serif'] = ['SimHei'] # 使用黑体
rcParams['axes.unicode_minus'] = False # 正常显示负号
with open('notebook.txt', encoding='utf-8') as file:
contents = file.readlines() # 按行读取文件内容
newList = []
for content in contents:
newContent = content.replace('\n', '') # 去掉换行符
money = newContent.split(':')[-1].strip() # 提取“:”后面的金额部分并去掉空格
newList.append(int(money))
x = [1, 2, 3, 4, 5, 6]
plt.plot(x, newList, 'r', label='收入') # 绘制红色折线图,并设置图例标签
plt.xlabel('月份') # 设置 x 轴标签
plt.ylabel('金额') # 设置 y 轴标签
plt.legend() # 显示图例
plt.show() # 展示图形