3

I generate an array which I want to visualize. Here is a simplified example.

ArrayPlot[Array[Sin[#1 #2] &, {200, 200}],
  PixelConstrained -> 1,
  ColorFunction -> "TemperatureMap", 
  PlotLegends -> Automatic]

This gives perfect colors and the legend to understand the values in each dot. However, I need a frame with ticks to understand the size of the box and fluctuations. Adding FrameTicks -> All will not work since it crops the image.

The solution proposed in an answer to question 28282 is to write a myPlot function with Raster and Offset. Doing so I can add ColorFunction -> "TemperatureMap" option to Raster.

However, I get 2 problems here. First, the colors are different from the ones in the first example. Second, I can not figure out how to make it draw a legend as PlotLegends->Automatic does not seem to work.

xzczd
  • 65,995
  • 9
  • 163
  • 468
user40532
  • 215
  • 1
  • 6
  • What about this? ArrayPlot[Array[Sin[#1 #2] &, {200, 200}], PixelConstrained -> 1, ColorFunction -> "TemperatureMap", FrameTicks -> Automatic, PlotLegends -> Placed[Automatic, Below]] – zhk Jan 16 '17 at 05:42
  • Please add the other code that is causing problems to your post. It is as important to have that code too.. – m_goldberg Jan 16 '17 at 06:10
  • I have experimented with the code you've posted, and it looks to me that the behavior is buggy when I add frame ticks. – m_goldberg Jan 16 '17 at 06:12

2 Answers2

4

The behavior of ArrayPlot seems buggy when both the options FrameTicks -> All and PixelConstrained are given. But if PixelConstrained is removed, everything seem to work fine.

ArrayPlot[Array[Sin[#1 #2] &, {220, 220}],
  FrameTicks -> All,
  ColorFunction -> "TemperatureMap",
  PlotLegends -> Placed[Automatic, Below],
  ImageSize -> Large]

plot

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
3

First, the colors are different from the ones in the first example

Because your data isn't rescaled. You need to modify the Reverse@data in myPlot to Rescale@Reverse@data.

Second, I can not figure out how to make it draw a legend

This is possible by making use of Legended etc., but admittedly more complicated than adding PlotLegends->Automatic in ArrayPlot, so I'd like not to resolve your problem by modifying myPlot further.

I'm not sure if the behavior of PixelConstrained should be called a bug, but a possible work-around is to use ImageSize:

data = Array[Sin[N@#1 #2] &, {200, 200}];

With[{pad = 38}, 
 Show[ArrayPlot[data, ColorFunction -> "TemperatureMap", PixelConstrained -> 1, 
   FrameTicks -> All, PlotLegends -> Automatic, PlotRangeClipping -> False], 
  ImageSize -> Dimensions@data + pad]]

Mathematica graphics

pad = 38 is found by trial and error.

ImageSize -> Dimensions@data + pad is added in Show rather than ArrayPlot, otherwise the size of legend will be influenced, too. Again, I'm not sure if this should be called a bug.

xzczd
  • 65,995
  • 9
  • 163
  • 468