[英]How to identify labels overlapping with xaxis or yaxis in matplotlib?
[英]matplotlib overlapping yaxis
代码:
def Draw_RoomType_vs_Price():
plt.figure(figsize=(9,6))
plt.scatter(x=df['room_type'], y=df['price'])
plt.title('Room Type vs Price', size = 15, weight='bold')
plt.xlabel('Room Type', size = 12)
plt.ylabel('Price', size = 12)
plt.tight_layout()
plt.show()
Draw_RoomType_vs_Price()
更新!
我想下面的代码会起作用,但由于我没有数据,我不完全确定。
def Draw_RoomType_vs_Price():
plt.figure(figsize=(9, 6))
plt.scatter(x=df['room_type'], y=df['price'].apply(lambda x: float(x.replace('$', ''))))
plt.title('Room Type vs Price', size=15, weight='bold')
plt.xlabel('Room Type', size=12)
plt.ylabel('Price', size=12)
plt.tight_layout()
plt.show()
==============================================
似乎 df['price'] 类型是字符串而不是 int/float。 您需要首先使用正则表达式删除除数字以外的其他字符:
import re
re.sub("[^0-9]", "", "$102")
然后将结果转换为 int 或 float,您就可以使用 plot 了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.