Change between log and line color bar imshow matplotlib
I have a matplotlib exponent generated using imshow () and I want to update it with new data and switch between log and linear color and normalization. I could just delete all axes and start over, but this is slow. I figured out how to refresh data and normalize, but cannot switch between log and linear color without removing the color bar.
This draws the original image using the colored log bar:
import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt data = np.random.rand(100, 100) figure = plt.figure('test') axes = figure.add_subplot(1, 1, 1) image = axes.imshow(data, interpolation='nearest', norm=mpl.colors.LogNorm()) colorbar = figure.colorbar(image) figure.canvas.draw()
Then change the data:
data2 = 10000 * np.random.rand(5, 10)
rows, cols = np.shape(data2)
image.set_data(data2)
image.set_extent([-0.5, cols - 0.5, -0.5, rows - 0.5]) # is there a better way to do this?
image.autoscale()
figure.canvas.draw()
Then change the normalization and try to change the color bar, but the ticks are in the wrong place as if the color bar were still recording:
colorbar.locator = mpl.ticker.LinearLocator()
colorbar.formatter = mpl.ticker.ScalarFormatter()
image.set_norm(mpl.colors.Normalize())
# colorbar.update_ticks() # doesn't change anything
figure.canvas.draw()
If I start with a linear colorbar (omit the norm argument in the first section), going to the linear colorbar doesn't work either:
colorbar.locator = mpl.ticker.LogLocator() colorbar.formatter = mpl.ticker.LogFormatterMathtext() image.set_norm(mpl.colors.LogNorm()) figure.canvas.draw()
source to share
No one has answered this question yet
Check out similar questions: