Estou tentando fazer um gráfico de dispersão, com uma legenda de figura fora da caixa, conforme sugerido em How to put the legend outside the plot , mas também estou plotando vários conjuntos de dados Uma barra de cores para vários gráficos de dispersão . O exemplo de trabalho mínimo a seguir tem a maioria dos conjuntos removidos; pode haver 10 conjuntos no script real:
import matplotlib.pyplot as plt
norm = plt.Normalize(55,1954)
plt.scatter([75,75,63,145,47],[0.979687,1.07782,2.83995,4.35468,4.44244], c = [75,75,70,70,70], norm = norm, label = 's1', cmap = 'gist_rainbow', marker = 'o')
plt.scatter([173,65],[0.263218,3.12112], c = [77,68], norm = norm, label = 's2', cmap = 'gist_rainbow', marker = 'v')
plt.colorbar().set_label('score')
plt.legend(loc = "outside center left")
plt.savefig('file.svg', bbox_inches='tight', pad_inches = 0.1)
mas recebo um erro:
/home/con/.local/lib/python3.10/site-packages/matplotlib/projections/__init__.py:63: UserWarning: Unable to import Axes3D. This may be due to multiple versions of Matplotlib being installed (e.g. as a system package and as a pip package). As a result, the 3D projection is not available.
warnings.warn("Unable to import Axes3D. This may be due to multiple versions of "
Traceback (most recent call last):
File "/tmp/2Le599Dl8S.py", line 6, in <module>
plt.legend(loc = "outside center left")
File "/home/con/.local/lib/python3.10/site-packages/matplotlib/pyplot.py", line 3372, in legend
return gca().legend(*args, **kwargs)
File "/home/con/.local/lib/python3.10/site-packages/matplotlib/axes/_axes.py", line 323, in legend
self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
File "/home/con/.local/lib/python3.10/site-packages/matplotlib/legend.py", line 566, in __init__
self.set_loc(loc)
File "/home/con/.local/lib/python3.10/site-packages/matplotlib/legend.py", line 703, in set_loc
raise ValueError(
ValueError: 'outside' option for loc='outside center left' keyword argument only works for figure legends
Eu também tentei:
import matplotlib.pyplot as plt
norm = plt.Normalize(55,1954)
fig = plt.scatter([75,75,63,145,47],[0.979687,1.07782,2.83995,4.35468,4.44244], c = [75,75,70,70,70], norm = norm, label = 's1', cmap = 'gist_rainbow', marker = 'o')
plt.scatter([173,65],[0.263218,3.12112], c = [77,68], norm = norm, label = 's2', cmap = 'gist_rainbow', marker = 'v')
plt.colorbar().set_label('score')
fig.legend(loc = "outside center left")
plt.savefig('file.svg', bbox_inches='tight', pad_inches = 0.1)
mas isso produz outro problema:
/home/con/.local/lib/python3.10/site-packages/matplotlib/projections/__init__.py:63: UserWarning: Unable to import Axes3D. This may be due to multiple versions of Matplotlib being installed (e.g. as a system package and as a pip package). As a result, the 3D projection is not available.
warnings.warn("Unable to import Axes3D. This may be due to multiple versions of "
Traceback (most recent call last):
File "/tmp/2Le599Dl8S.py", line 6, in <module>
fig.legend(loc = "outside center left")
AttributeError: 'PathCollection' object has no attribute 'legend'
Também olhei Qual é a necessidade de plt.figure() no matplotlib? , mas não há nada lá sobre lendas, então aquela postagem não resolveu meu problema. Também não apareceu na pesquisa do Google.
Você está quase lá. Você simplesmente precisa criar um
plt.Figure()
e usarfig.legend()
.