【问题标题】:Matplotlib - Scatter Points and text annotations are not aligning to each otherMatplotlib - 散点和文本注释彼此不对齐
【发布时间】:2021-01-19 07:34:41
【问题描述】:

我正在尝试绘制我的聚类结果,但散点图和文本注释没有相互对齐。我不确定要添加什么值,或者是否有任何简单且正确的方法可以做到这一点,下面是我的一段代码。

plt.figure(figsize=(17,9))
labs = statewise_us_finalDF_copy['State'].tolist()

#filter rows of original data
filtered_label0 = statewise_us_finalDF_copy[statewise_us_finalDF_copy['KMEANS_cluster'] == 0]
filtered_label1 = statewise_us_finalDF_copy[statewise_us_finalDF_copy['KMEANS_cluster'] == 1]
filtered_label2 = statewise_us_finalDF_copy[statewise_us_finalDF_copy['KMEANS_cluster'] == 2]
filtered_label3 = statewise_us_finalDF_copy[statewise_us_finalDF_copy['KMEANS_cluster'] == 3]
filtered_label4 = statewise_us_finalDF_copy[statewise_us_finalDF_copy['KMEANS_cluster'] == 4]
filtered_label5 = statewise_us_finalDF_copy[statewise_us_finalDF_copy['KMEANS_cluster'] == 5]
filtered_label6 = statewise_us_finalDF_copy[statewise_us_finalDF_copy['KMEANS_cluster'] == 6]
 
# Plotting the results
plt.scatter(filtered_label0['Unemployed_2019'] , filtered_label0['POVALL_2019'] , color = 'red')
plt.scatter(filtered_label1['Unemployed_2019'] , filtered_label1['POVALL_2019'] , color = 'blue')
plt.scatter(filtered_label2['Unemployed_2019'] , filtered_label2['POVALL_2019'] , color = 'green')
plt.scatter(filtered_label3['Unemployed_2019'] , filtered_label3['POVALL_2019'] , color = 'black')
plt.scatter(filtered_label4['Unemployed_2019'] , filtered_label4['POVALL_2019'] , color = 'magenta')
plt.scatter(filtered_label5['Unemployed_2019'] , filtered_label5['POVALL_2019'] , color = 'cyan')
plt.scatter(filtered_label6['Unemployed_2019'] , filtered_label6['POVALL_2019'] , color = 'orange')
plt.legend(['Cluster 1', 'Cluster 2', 'Cluster 3', 'Cluster 4', 'Cluster 5', 'Cluster 6', 'Cluster 7'], loc = 'best')

#use for loop to add annotations to each point in plot 
for i, txt in enumerate(labs):
    plt.annotate(txt, (statewise_us_finalDF_copy['Unemployed_2019'][i], statewise_us_finalDF_copy['POV017_2019'][i]))

    
plt.title('Clusters by Kmeans', fontsize = 30)    
plt.xlabel('Unmployment in 2019', fontsize = 18)
plt.ylabel('Poverty in 2019', fontsize = 18)
plt.xscale('log')
plt.yscale('log')


plt.show()

【问题讨论】:

    标签: python python-3.x pandas matplotlib


    【解决方案1】:

    你可以试试这个

    fig = plt.figure(figsize=(17,9))
    
    ax = fig.add_subplot(111)
    
    for i, txt in enumerate(labs):
            xx = statewise_us_finalDF_copy['Unemployed_2019'][i]
            yy = statewise_us_finalDF_copy['POV017_2019'][i]
            ax.text(xx, yy, txt, transform=ax.transAxes, axis="both", direction='in',top=True, right=True)
    
    

    【讨论】:

    • fig 和 ax defined 不适合我抛出错误
    • 您也可以将您的 plt.annotate 更改为 plt.annotate(txt, (statewise_us_finalDF_copy['Unemployed_2019'][i], statewise_us_finalDF_copy['POV017_2019'][i]), xytext=(1,1))xytext 控制文本到点的距离。
    猜你喜欢
    • 2015-11-12
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2021-10-27
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多