Cóż, wiem, jak dodać pasek kolorów do figury, gdy utworzę figurkę bezpośrednio przy pomocy matplotlib.pyplot.plt
.Jak dodać pasek kolorów dla wykresu hist2d
from matplotlib.colors import LogNorm
import matplotlib.pyplot as plt
import numpy as np
# normal distribution center at x=0 and y=5
x = np.random.randn(100000)
y = np.random.randn(100000) + 5
# This works
plt.figure()
plt.hist2d(x, y, bins=40, norm=LogNorm())
plt.colorbar()
Ale dlaczego następujące nie działa, i co muszę dodać na wezwanie colorbar(..)
aby to działało.
fig, ax = plt.subplots()
ax.hist2d(x, y, bins=40, norm=LogNorm())
fig.colorbar()
# TypeError: colorbar() missing 1 required positional argument: 'mappable'
fig, ax = plt.subplots()
ax.hist2d(x, y, bins=40, norm=LogNorm())
fig.colorbar(ax)
# AttributeError: 'AxesSubplot' object has no attribute 'autoscale_None'
fig, ax = plt.subplots()
h = ax.hist2d(x, y, bins=40, norm=LogNorm())
plt.colorbar(h, ax=ax)
# AttributeError: 'tuple' object has no attribute 'autoscale_None'
'fig.colorbar (IM)' również działa, i wydaje się bardziej spójna z resztą odpowiedź. – ThomasH