I'm trying to use the adjustText library with matplotlib to remove overlapping text. I'm using Python==3.8, matplotlib==1.5.0, adjustText==1.0.4, and scipy==1.5.3 For additional context, I'm running in Azure on the Python 3.8-AzureML Kernel. Could someone please recommend how to get around this issue? I'm not attached to using the adjsutText library with matplotlib, so if anyone's got other suggestions for how to fix overlapping text annotations, please let me know!! Thank you
This is the code I'm running:
def pca_biplot(original_transformed_clusters_df, num_dims, pca_components):
from adjustText import adjust_text
plt.figure(figsize=(15,10))
dim_1 = original_transformed_clusters_df.iloc[:,20:21]
dim_2 = original_transformed_clusters_df.iloc[:,21:22]
components_transposed = np.transpose(pca_components[:3,:])
num_features = components_transposed.shape[0]
features_names = list(original_transformed_clusters_df.columns[10:20])
scale_dim_1 = dim_1/(dim_1.max()-dim_1.min())
scale_dim_2 = dim_2/(dim_2.max()-dim_2.min())
plt.scatter(scale_dim_1, scale_dim_2, c = original_transformed_clusters_df['Cluster Labels'], alpha=0.2)
texts = []
for i in list(range(num_features)):
dx = components_transposed[i,0]
dy = components_transposed[i,1]
plt.arrow(0, 0, dx, dy, color = 'r', lw=1, head_width=.008, head_length=.01)
texts.append(plt.text(dx*1.05, dy*1.05, features_names[i], fontsize=14))
adjust_text(texts, only_move={'points':'dx*2', 'texts':'dy*2'}, arrowprops=dict(arrowstyle='wedge', color='black', lw=1))
plt.grid()
pca_biplot(df_x_and_pca_clustering, 3, pca.components_)
and I'm getting the following error:
TypeError Traceback (most recent call last)
Cell In[41], line 47
43 adjust_text(texts)
45 plt.grid()
---> 47 pca_biplot(df_x_and_pca_clustering, 3, pca.components_)
Cell In[41], line 43, in pca_biplot(original_transformed_clusters_df, num_dims, pca_components)
40 plt.xlabel('DIM1', fontsize=14)
41 plt.ylabel('DIM2', fontsize=14)
---> 43 adjust_text(texts)
45 plt.grid()
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/adjustText/__init__.py:505, in adjust_text(texts, x, y, objects, avoid_self, force_text, force_static, force_pull, force_explode, expand, explode_radius, ensure_inside_axes, expand_axes, only_move, ax, min_arrow_len, time_lim, iter_lim, *args, **kwargs)
501 explode_radius = max(
502 (coords[:, 1] - coords[:, 0]).max(), (coords[:, 3] - coords[:, 2]).max()
503 )
504 if explode_radius > 0 and np.all(np.asarray(force_explode) > 0):
--> 505 explode_x, explode_y = explode(coords, static_coords, explode_radius)
506 if "x" not in only_move["explode"]:
507 explode_x = np.zeros_like(explode_x)
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/adjustText/__init__.py:243, in explode(coords, static_coords, r)
241 points = np.vstack([points, static_centers])
242 tree = scipy.spatial.KDTree(points)
--> 243 pairs = tree.query_pairs(r, output_type="ndarray")
244 pairs = pairs[pairs[:, 0] < N]
245 pairs = pairs[pairs[:, 0] != pairs[:, 1]]
TypeError: query_pairs() got an unexpected keyword argument 'output_type'
Previously, this code worked fine when I had Python==3.9.15, matplotlib==3.6.2, adjustText== 0.7.3.1, and scipy==1.10.0, but this was on my local machine. I've now started working in Azure and I'm not sure how to fix the problem.