tried, still not yet
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
Data = pd.read_csv('fangjia2019_清洗.csv',sep=',')
plt.figure(figsize=(12,8))
plt.rcParams['font.sans-serif']=['SimHei']
Data=Data.sort_values('房价',ascending=False)
plt.plot(Data['城市'],Data['房价'],'b*--', alpha=0.5, linewidth=1)
for x, y in zip(Data['城市'], Data['房价']):
plt.text(x, y+0.3, '%.00f' % y, ha='left', va='top', fontsize=7.5)
plt.plot(Data['城市'],Data['房价'])
plt.title("房价与城市的关系",size=15)
plt.show()
Traceback (most recent call last):
File "C:\Users\ASUS\Desktop\毕设代码\题1.py", line 11, in <module>
plt.text(x, y+0.3, '%.00f' % y, ha='left', va='top', fontsize=7.5)
TypeError: can only concatenate str (not "float") to str
adding str also gets an error. That's why wow
0 Answer
tried, still not yet
Part of this answer references GPT, GPT_Pro for better problem solving
matplotlib is a very popular drawing library in python, It helps you create charts quickly and easily, including line charts, scatter charts, bar charts, etc. Error resolution is important when using matplotlib.
TypeError: can only concatenate str(not float) to str Indicates that you are trying to concatenate a floating point number to a string, which is not allowed. First, make sure your code is correct, especially in the above code, especially in the "plt.text" function, you should use strings instead of floating-point numbers. Even if you want to print a floating-point number on the screen, you must convert it to a string.
You can convert any number to a string using the str() function. For example, if you want to print a floating-point number x, you can convert it to a string using str(x) and then print it to the screen using the plt.text function.
import pandas as pd
import numpy as mp
import matplotlib.pyplot as plt
pata - pd.read_csv( ,sep-'…)
plt.figure(figsize-(12,8))
plt.rcparams[ ]-[ ]
pata-pata.sort_values( ,ascending-False)
plt.plot(pata[ ] ,pata[ ], ,alpha-8.5,linewidth=1)
for xy in zip(Data[ ],pata[ ])
plt.text(x,y+9.3,str(y)+'%' ,ha- ,va-'top',fontsize-7.5) # 改成str()函数
plt.plot(pata[ ] ,pata[ ])
plt.title( ,size-15)
plt.show()
The above code will fix the problem you encounter. In addition, the matplotlib documentation contains plenty of useful information and examples to help you quickly learn and understand different aspects of drawing.
If the answer is helpful, please accept it.
这家伙很懒,什么都没留下...