繁体   English   中英

如何更改matplotlib中的y刻度label

[英]How to change the y tick label in matplotlib

下面的代码生成一个散点图 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()

在此处输入图像描述 用于生成 plot 的 dataframe 的样本是

{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: []}

当我尝试将标签放入数组并将其设置为 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)

我收到转换错误。

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

我希望将 dataframe 中的值用作标签。

我自己没有尝试过,但这样的事情可能会有所帮助:

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

暂无
暂无

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

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