4

I'm trying to plot spectrogram with y-scale depending on the period, so I want to have a reversed log-scale.

The thing is: I found how to do it using pcolormesh(), not by using imshow(). imshow() seems to be more efficient than pcolormesh(), that is, for me, a great reason to prefer it !

Did I miss something ?

I do not know how to say more clearly so here is a reproducible example:

import matplotlib.pyplot as plt
import numpy as np

size = 10
data = np.arange(size * size).reshape((size, size))

x_start = 1
x_end = 10
y_start = 1
y_end = 10

extent = [x_start, x_end, y_start, y_end]

fig, axes = plt.subplots(1,4)

axes[0].set_yscale('log')
im = axes[0].imshow(data, extent=extent, origin='upper', interpolation='None', cmap='viridis')


axes[1].set_yscale('log')
im2 = axes[1].imshow(data, extent=extent, origin='lower', interpolation='None', cmap='viridis')

axes[2].set_yscale('log')
im2 = axes[2].imshow(data, extent=extent, origin='lower', interpolation='None', cmap='viridis')
axes[2].invert_yaxis()

y = np.arange(1,11)*0.1
x = np.arange(0,10)
axes[3].set_yscale('log')
im3 = axes[3].pcolormesh(x, 1/y , data)

axes[0].set_title("not ok")
axes[1].set_title("not ok")
axes[2].set_title("not ok")
axes[3].set_title("OK")

plt.tight_layout()

plt.show()

enter image description here

You see on the previous image that tight ordinates do not change using imshow(), they are always on the top of the figure even if I use lower or upper origin. With pcolormesh(), I achieved to get tight ordinates on the bottom of the graph.

I would dream to get the "ok" figure by using imshow() !

This question is relative to this one: Aggregate several AxesSubplot after multiprocessing to draw a matplotlib figure

n0n0bstan
  • 1,790
  • 4
  • 15
  • 26
  • 1
    might be worth saying this is a follow on question from https://stackoverflow.com/q/57269797/1358308 – Sam Mason Jul 30 '19 at 13:28
  • also, how big is the data you want to work with? i.e. what is `data.shape` for some data you actually want to plot? – Sam Mason Jul 30 '19 at 13:37
  • shape: (2048,15357) – n0n0bstan Jul 30 '19 at 13:40
  • OK, so resampling this down to something more sensible in the process would probably be a good idea. if that can get down to approx (500,500) before matplotlib sees it things will be much faster – Sam Mason Jul 30 '19 at 13:46
  • The optimization question is https://stackoverflow.com/questions/57269797/aggregate-several-axessubplot-after-multiprocessing-to-draw-a-matplotlib-figure, let's try to keep the comments in the correct thread – n0n0bstan Jul 30 '19 at 13:53

1 Answers1

0

Is this what you are trying to get?

axes[1].set_yscale('log')
im2 = axes[1].imshow(data, extent=extent, origin='lower', interpolation='None', cmap='viridis')
axes[1].invert_yaxis()

enter image description here

Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
  • Hi ! Thanks for trying ! sadly, invert-yaxis() is not enough: it reverts the y legends too, that I do not want ... I update my question now – n0n0bstan Jul 30 '19 at 12:48
  • I think this is not possible with `imshow`. Possibly [`NonUniformImage`](https://matplotlib.org/3.1.0/api/image_api.html#matplotlib.image.NonUniformImage) would help. – ImportanceOfBeingErnest Jul 31 '19 at 15:06