I am making a scatter plot where I relate the valuations of books with their price, depending on the type. The type, as it is a categorical variable, I have used LabelEncoder()
to assign a value to each category. Then, when the graph is represented, it is executed correctly, but the problem comes when putting the legend, because when putting it, each color of the graph is not associated with the type of book.
Code used:
for y, c in zip(data['Type'].unique(), book_type):
plt.scatter(data["Rating"], data['Price'], c=book_type, label=y)
plt.xlabel('Valoraciones')
plt.ylabel('Precio')
plt.legend(loc=0)
I attached an image so you can see the graph:
Notice how the variable book_type contains the values of each type of book, data['Rating'] are the ratings of the books and data['Price'] is the price.
As you can see, there are several comments in the code because I have been trying various ways to do it without success. I can't get the legend to have the colors of the graph.
I enclose a print on the data and a print on the 'book_type' variable:
data
book_type:
You can see that book_type has values, which each belong to each of the data['Type'] categories.
If someone could help me I would greatly appreciate it.
All the best.
I recommend you to use Pandas own .plot():