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