Move existing jointplot legend

2024/9/20 1:06:50

I tried answers from a previous question to no avail in Matplotlib 1.5.1.

I have a seaborn figure:

import seaborn as sns
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
tips = sns.load_dataset("tips")
g = sns.jointplot("total_bill", "tip",  data = tips[["total_bill", "tip"]].applymap(lambda x : -np.log10(x)))

This does not work:

g.ax_joint.legend(loc = 'lower right')

As well as this:

plt.legend(bbox_to_anchor=(1.05, 1), loc=4, borderaxespad=0.)/usr/local/lib/python3.4/dist-packages/matplotlib/axes/_axes.py:520: UserWarning: No labelled objects found. Use label='...' kwarg on individual plots.warnings.warn("No labelled objects found. "

What is the way to locate an existing legend to lower right in this case?


Not an elegant solution for now is:

ll = g.ax_joint.get_legend().get_texts()[0]._text
g.ax_joint.get_legend().remove()
g.ax_joint.text( -12, -12, ll,  fontsize=14)

However, I believe there should be a better way.

Answer

There is no easy (using strings like 'lower right') way to relocate an existing legend that I am aware of.

To get the handles of an existing legend you can use legend.legendHandles(). For the labels, legend.get_texts() will give you the Text objects. In order to retrieve the actual labels you'd better use .get_text() instead of the private attribute _text.

The following will copy the handles and labels of an existing legend to a new one. Other properties of the legend won't be copied.

legend = g.ax_joint.get_legend()
labels = (x.get_text() for x in legend.get_texts())
g.ax_joint.legend(legend.legendHandles, labels, loc='lower right')

I previously suggested using ax.get_legend_handles_labels() but this will search in the axes, not the legend, and is not useful in this case.

https://en.xdnf.cn/q/119476.html

Related Q&A

timezone conversion of a large list of timestamps from an excel file with python

I have an excel file named "hello.xlsx". There is a column of timestamps that has a lot of rows (more than 80,000 rows for now). The file basically looks like this:03/29/2018 19:24:5003/29/20…

N_gram frequency python NTLK

I want to write a function that returns the frequency of each element in the n-gram of a given text. Help please. I did this code fo counting frequency of 2-gramcode:from nltk import FreqDistfrom nltk.…

Is there a way to have a list of 4 billion numbers in Python?

I made a binary search function and Im curious what would happen if I used it on 4 billion numbers, but I get a MemoryError every time I use it. Is there a way to store the list without this issue?

ValueError: invalid literal for int() with base 10: when it worked before

Im having some issues with my program, basically what Im trying to do is Stenography, insert an image into another image and then extract the secret image.My program is able to insert just fine, but ex…

How to fetch the current branch from Jenkins?

I would like to query Jenkins using its API and Python to fetch the branch that is currently ready to be built.How can I do that?

How to vertically stretch graphs with matplotlib subplot [duplicate]

This question already has answers here:How do I change the size of figures drawn with Matplotlib?(16 answers)Closed 5 years ago.With the following code, I try to plot 12 different histograms in one pi…

Python Selenium Traceback (most recent call last):

Im trying to use selenium for a python web scraper but when I try to run the program I get the following error: "/Applications/Python 3.8/IDLE.app/Contents/MacOS/Python" "/Applications/P…

getting error while installing opencv via pip

python version = Python 3.8.0pip version = 19.3.1C:\Users\Sami Ullah Ch>pip3 install opencv-pythonERROR: Could not find a version that satisfies the requirement opencv-python (from versions: none)

Check whether text contains x numbers in a row [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 8 years ago.Improve…

How to add polling interval for a GET request in python

I have a case where I have to keep checking the response of a GET call until I see the status as success in the api response. And it takes around 20 to 50 mins to get the status from active to success.…