Put the second axis in 2dhistgram of matplotlib

Example: Add a second axis to the y-axis

image.png

A, B, and C are added to the second axis of the y-axis in the above figure.

In the following, a random array is put in (x, y) and a 2dhistogram is drawn.

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm

N_numbers = 100000
N_bins = 100

np.random.seed(0)
x, y = np.random.multivariate_normal(
        mean=[400.0, 1000.0], 
        cov=[[1000,500],
             [400,700]],  
        size=N_numbers
        ).T          


fig, ax = plt.subplots(figsize =(6, 6))
H=ax.hist2d(x, y, bins=N_bins, cmap='nipy_spectral',norm=LogNorm())
plt.xlim([0,1100])
plt.ylim([0,2100])

cb = fig.colorbar(H[3],ax=ax)
cb.set_label('Number')

plt.title('The title')
plt.xlabel('x axis')
plt.ylabel('y axis')

ax2 = ax.twinx()
ax2.set_ylim(0,2100)
plt.yticks([1100,1200,1500], ["A","B","C"])

plt.show()

To add the second axis, just add the 4th line from the back. Use ax.twinx () to share the x-axis of ax2 with ax.

Recommended Posts

Put the second axis in 2dhistgram of matplotlib
Put matplotlib in Centos7.
When the axis and label overlap in matplotlib
Omit the decimal point of the graph scale in matplotlib
Change the style of matplotlib
The story of participating in AtCoder
Put the module in the Pepper app
The story of the "hole" in the file
The meaning of ".object" in Django
Unravel the mystery of matplotlib specgram
About the size of matplotlib points
The first step in Python Matplotlib
Specify the color in the matplotlib 2D map
Check the behavior of destructor in Python
The story of an error in PyOCR
Summary of frequently used commands in matplotlib
Put together consecutive values in the list
Implement part of the process in C ++
The result of installing python in Anaconda
Let's claim the possibility of pyenv-virtualenv in 2021
The basics of running NoxPlayer in Python
Separation of design and data in matplotlib
[Matplotlib] Do not tilt the axis label
The second night of the loop with for
A memo explaining the axis specification of axis
In search of the fastest FizzBuzz in Python
Get the key for the second layer migration of JSON data in python
The date is displayed incorrectly in matplotlib.
How to make the font width of jupyter notebook put in pyenv equal width
Output the number of CPU cores in Python
The meaning of {version-number} in the mysql rpm package
[Python] Sort the list of pathlib.Path in natural sort
About the X-axis notation of Matplotlib bar graphs
Change the font size of the legend in df.plot
Get the caller of a function in Python
Match the distribution of each group in Python
View the result of geometry processing in Python
Increase the font size of the graph with matplotlib
Make a copy of the list in Python
One liner that lists the colors of matplotlib
Find the number of days in a month
Read the output of subprocess.Popen in real time
Find the divisor of the value entered in python
The story of finding the optimal n in N fist
Find the solution of the nth-order equation in python
Predict the second round of summer 2016 with scikit-learn
About the behavior of Model.get_or_create () of peewee in Python
Solving the equation of motion in Python (odeint)
Visualized the usage status of the sink in the company
Output in the form of a python array
The story of viewing media files in Django
The basis of graph theory with matplotlib animation
Code for checking the operation of Python Matplotlib
Search by the value of the instance in the list
Put Python3 in Docker container of Amazon Linux2
Visualize the behavior of the sorting algorithm with matplotlib
Make progress of dd visible in the progress bar
Factfulness of the new coronavirus seen in Splunk
matplotlib: Replace the axis itself with another one.
The story of the second year of new graduates passing Java Silver / Gold in two months
Put Scipy + Matplotlib in Ubuntu on Vagrant and display the graph with X11 Forwarding