AdjustText TypeError: query_pairs() got an unexpected keyword argument 'output_type'

2 min read 04-10-2024
AdjustText TypeError: query_pairs() got an unexpected keyword argument 'output_type'


Troubleshooting AdjustText's TypeError: "query_pairs() got an unexpected keyword argument 'output_type'"

Have you encountered the frustrating "TypeError: query_pairs() got an unexpected keyword argument 'output_type'" error while using AdjustText in your Python code? This error often pops up when trying to label plots with overlapping text, and it can leave you scratching your head.

Let's break down the problem and find a solution.

Understanding the Error

This error arises because you are likely using an older version of AdjustText. The output_type parameter was introduced in a newer release of the library. If your version is outdated, it won't recognize this parameter, leading to the error.

Replicating the Scenario

Here's a simple example demonstrating the error:

import matplotlib.pyplot as plt
from adjustText import adjust_text

x = [1, 2, 3, 4]
y = [2, 4, 1, 3]
labels = ["Point 1", "Point 2", "Point 3", "Point 4"]

fig, ax = plt.subplots()
ax.scatter(x, y)

texts = [ax.text(x[i], y[i], labels[i]) for i in range(len(x))]
adjust_text(texts, output_type='dict')  # This will raise the error!

plt.show()

The Solution: Update AdjustText

The solution is simple: upgrade your AdjustText library to the latest version. Here's how you can do it:

  1. Open your terminal or command prompt.

  2. Type the following command and press enter:

    pip install --upgrade adjusttext
    
  3. This will update the AdjustText library to its newest version.

Now, when you run your code, the error should be resolved, and the output_type parameter should work correctly.

Additional Tips

  • Verify the version: After updating, you can check the AdjustText version by importing it and printing its __version__ attribute:

    import adjustText
    print(adjustText.__version__)
    
  • Consider output_type: The output_type parameter lets you control how the output from AdjustText is returned. You can choose:

    • 'dict': Returns a dictionary mapping the original text objects to their adjusted positions.
    • 'list': Returns a list of tuples, where each tuple contains the original text object and its adjusted position.
    • None (default): Adjusts the text objects in place without returning any information.

Resources

By updating your AdjustText library, you can eliminate this TypeError and effectively utilize the output_type parameter for more control over your text label adjustments. Enjoy creating visually appealing and informative plots!