3
$\begingroup$

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.

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

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.

$\endgroup$
4
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.