简体   繁体   中英

How to change the y tick label in matplotlib

The below code generates a scatter plot.

#KNNClassifier_weighted
import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize=(100, 30)) 
xy = np.array([
    (x, y) for x, lst in df_param.items()
    for sublst in lst for y in sublst
])
plt.scatter(*xy.T, s=500,  edgecolors='black', linewidth=3)
plt.title("KNNClassifier: weighted",fontsize=80)
 
# Setting the x and y labels
plt.xlabel("Iteration",fontsize=80)
plt.ylabel("value",fontsize=80)
#labels=["True", "False"]
# Setting the number of ticks
plt.xticks(np.arange(0, len(df_param)+1, 10),fontsize=34, rotation=90)
plt.yticks(fontsize=45)
plt.xlim(xmin=0)
plt.show()

在此处输入图像描述 A sample of the dataframe that is used to generate the plot is

{0: [[True], [False], [True], [False], [False], [False]], 1: [[False], [True], [False], [False], [False]], 2: [[False], [True], [False], [False]], 3: [[False], [False], [False]], 4: [[False], [False]], 5: [[False]], 6: [], 7: [], 8: [[False]], 9: [], 10: []}

When I try putting the labels in an array and set it as yticks.

labels=["True", "False"]
# Setting the number of ticks
plt.xticks(np.arange(0, len(df_param)+1, 10),fontsize=34, rotation=90)
plt.yticks(labels, fontsize=45)

I get the conversion error.

ConversionError: Failed to convert value(s) to axis units: ['True', 'False']

I want the values in the dataframe to be used as labels.

I haven't tried this myself but something like this may help:

plt.yticks([1.0, 0.0], labels, fontsize=45)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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