[PYTHON] I want to manually create a legend with matplotlib

Introduction

I think that the matplot legend should be created automatically in most cases, but since irregularities have occurred, make a note of how to set it manually.

Ordinary plot

Perhaps this is sufficient in most cases, and you should organize your data so that you can do this.

import numpy as np
from matplotlib import pyplot as plt

red_x, red_y = np.random.randn(10), np.random.randn(10)
blue_x, blue_y = np.random.randn(10), np.random.randn(10)
green_x, green_y = np.random.randn(10), np.random.randn(10)

plt.scatter(red_x, red_y, c="r", alpha=0.5, label="red")
plt.scatter(blue_x, blue_y, c="b", alpha=0.5, label="blue")
plt.scatter(green_x, green_y, c="g", alpha=0.5, label="green")

plt.legend()
plt.show()

sample.png

Set manually

If you are dealing with special situations or special data and cannot make the above code, or if the code becomes dirty, you can set it manually as follows.

import numpy as np
from matplotlib import pyplot as plt

red_x, red_y = np.random.randn(10), np.random.randn(10)
blue_x, blue_y = np.random.randn(10), np.random.randn(10)
green_x, green_y = np.random.randn(10), np.random.randn(10)

#Remove the label from the data part
plt.scatter(red_x, red_y, c="r", alpha=0.5)
plt.scatter(blue_x, blue_y, c="b", alpha=0.5)
plt.scatter(green_x, green_y, c="g", alpha=0.5)

#Plot empty data with label for legend (actually nothing is plotted)
plt.scatter([], [], c="r", alpha=0.5, label="red")
plt.scatter([], [], c="b", alpha=0.5, label="blue")
plt.scatter([], [], c="g", alpha=0.5, label="green")

plt.legend()
plt.show()

sample_changed.png

in conclusion

It seems that it can be used when changing the transparency and the size of dots only in the legend part.

Recommended Posts

I want to manually create a legend with matplotlib
I want to make matplotlib a dark theme
I want to easily create a Noise Model
I want to create a window in Python
I want to display multiple images with matplotlib.
I want to make a game with Python
I want to write to a file with Python
(Matplotlib) I want to draw a graph with a size specified in pixels
I want to create a graph with wavy lines omitted in the middle with matplotlib (I want to manipulate the impression)
I tried to create a table only with Django
I want to transition with a button in flask
I want to work with a robot in python.
I want to split a character string with hiragana
[Python] How to create a 2D histogram with Matplotlib
I want to run a quantum computer with Python
I want to bind a local variable with lambda
I want to do ○○ with Pandas
I want to debug with Python
I want to make a blog editor with django admin
I want to start a jupyter environment with one command
I want to make a click macro with pyautogui (desire)
I want to make a click macro with pyautogui (outlook)
I want to use a virtual environment with jupyter notebook!
[Visualization] I want to draw a beautiful graph with Plotly
I want to create a Dockerfile for the time being.
I tried to create a linebot (implementation)
I want to detect objects with OpenCV
I want to use a wildcard that I want to shell with Python remove
I want to print in a comprehension
I tried to create a linebot (preparation)
I want to blog with Jupyter Notebook
I wanted to create a smart presentation with Jupyter Notebook + nbpresent
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!
Qiskit: I want to create a circuit that creates arbitrary states! !!
I want to build a Python environment
I want to pip install with PythonAnywhere
I tried to create a list of prime numbers with python
I want to do a full text search with elasticsearch + python
I want to analyze logs with Python
I want to play with aws with python
I want to create a system to prevent forgetting to tighten the key 1
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I tried to create Bulls and Cows with a shell program
I want to create a pipfile and reflect it in docker
I want to create a machine learning service without programming! WebAPI
Create a poster with matplotlib to visualize multiplication tables that remember multiplication
I want to embed Matplotlib in PySimpleGUI
Create a graph with borders removed with matplotlib
I want to create a histogram and overlay the normal distribution curve on it. matplotlib edition
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
I tried to create a plug-in with HULFT IoT Edge Streaming [Development] (2/3)
I tried to create a plug-in with HULFT IoT Edge Streaming [Execution] (3/3)
[Outlook] I tried to automatically create a daily report email with Python
I tried to create a plug-in with HULFT IoT Edge Streaming [Setup] (1/3)
I want to create an Ubuntu chrome User Profile with Colab only
I want to create a machine learning service without programming! Text classification
I want to use MATLAB feval with python
I want to analyze songs with Spotify API 2
I want to INSERT a DataFrame into MSSQL
I want to mock datetime.datetime.now () even with pytest!
I want to knock 100 data sciences with Colaboratory