简体   繁体   English

黑条覆盖了 matplotlib plot 的我的 y 标签

[英]Black bar covering my y labels for matplotlib plot

I recently started using python for a project, so I apologize in advance for my inexperience.我最近开始在一个项目中使用 python,所以我为我的经验不足提前道歉。 I am working with the dataset ML1, for each user within the dataset I have to count how many ratings he has expressed.我正在使用数据集 ML1,对于数据集中的每个用户,我必须计算他表达了多少评分。 Then I have to sort them out and calculate various statistical functions.然后我必须将它们整理出来并计算各种统计函数。 After doing this I have to create a graph where it shows on the x= users axis sorted by ratings and y= number of ratings.完成此操作后,我必须创建一个图表,它显示在 x= users 轴上,按评级排序,y= 评级数。

get_id_user = lambda col: (line.split('::')[col-1] for line in open('../dataset-movie/ml-1m/users.dat'))
get_id_rating = lambda col: (line.split('::')[col-1] for line in open('../dataset-movie/ml-1m/ratings.dat'))
users_id = list(get_id_user(1))
ratings_id = list(get_id_rating(1))
users_rating_match = {}

for index in users_id:
    users_rating_match[index] = ratings_id.count(index)

sorted_users = {k: v for k, v in sorted(users_rating_match.items(), key=lambda v: v[1], reverse=True)}
x = list(sorted_users.keys())
y = list(sorted_users.values())

plt.plot(x, y)
plt.xlabel('Users sorted by rating')
plt.title('User distribution')
plt.show()

This is my code,Briefly, I read the columns I need from the file.dat.这是我的代码,简而言之,我从 file.dat 中读取了我需要的列。 I insert those values into two lists and associate them with a dictionary.我将这些值插入到两个列表中,并将它们与字典相关联。 Then I order the dictionary and build a graph with the specifications above.然后我订购字典并使用上述规范构建图表。 When I show the graph, black bars covering my y labels for matplotlib plot certainly because of the high number of values.当我显示图表时,黑色条覆盖了 matplotlib plot 的 y 标签,这肯定是因为值的数量很大。 I tried to put into practice some solutions of other posts, but without success.我试图将其他帖子的一些解决方案付诸实践,但没有成功。 Can someone explain/show me how to solve this problem?有人可以解释/告诉我如何解决这个问题吗? picture_problem图片问题

It's not a black bar, it's a bunch of labels on top of each other.它不是一个黑条,它是一堆相互叠加的标签。 With this y axis array, matplotlib (which I assume you are using) has difficulties in suggesting a good and readable number of ticks along the y axis.使用此 y 轴数组,matplotlib(我假设您正在使用)难以建议沿 y 轴的良好且可读的刻度数。 I can't really tell from your code, but it can happen if the values in the array are not completely numerical.我无法从您的代码中真正看出,但如果数组中的值不完全是数字,则可能会发生这种情况。 Look at set_yticks() and set_yticklabels() and see if that helps.查看set_yticks()set_yticklabels()看看是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM