2
$\begingroup$

I'm the beginner in Mathematica Plot, so I recently met many situations that I could not understand. Here is my code for drawing 10 squares across the diagonal line in a square:

RegionPlot[{x - y <= 0.1 && y - x <= 0.1 && 0 <= x <= 0.1 &&
      0 <= y <= 0.1, x - y <= 0.1 && y - x <= 0.1 && 0.1 <= x <= 0.2 && 0.1 <= y <= 0.2,
      x - y <= 0.1 && y - x <= 0.1 && 0.2 <= x <= 0.3 && 0.2 <= y <= 0.3,
      x - y <= 0.1 && y - x <= 0.1 && 0.3 <= x <= 0.4 && 0.3 <= y <= 0.4,
      x - y <= 0.1 && y - x <= 0.1 && 0.4 <= x <= 0.5 && 0.4 <= y <= 0.5,
      x - y <= 0.1 && y - x <= 0.1 && 0.5 <= x <= 0.6 &&0.5 <= y <= 0.6,
      x - y <= 0.1 && y - x <= 0.1 && 0.6 <= x <= 0.7 &&0.6 <= y <= 0.7,
      x - y <= 0.1 && y - x <= 0.1 && 0.7 <= x <= 0.8 &&0.7 <= y <= 0.8,
      x - y <= 0.1 && y - x <= 0.1 && 0.8 <= x <= 0.9 &&0.8 <= y <= 0.9,
      x - y <= 0.1 && y - x <= 0.1 && 0.9 <= x <= 1.0 &&0.9 <= y <= 1.0},
      {x, 0, 1}, {y, 0, 1}, PlotStyle -> Blue,PlotPoints -> 100, MaxRecursion -> 10,
      FrameLabel -> {"\!\(\*SubscriptBox[\(p\), \(1\)]\)", "\!\(\*SubscriptBox[\(p\), \(2\)]\)"}, BoundaryStyle -> None]

But it gives me three squares:

Enter image description here

Where are the other seven squares?

$\endgroup$
3
  • $\begingroup$ Your input data contains irregular symbol after the third square. That's why. $\endgroup$
    – A. Kato
    Commented Aug 20, 2022 at 0:16
  • 1
    $\begingroup$ The is wrong. Please use a suitable editor. $\endgroup$
    – cvgmt
    Commented Aug 20, 2022 at 0:22
  • $\begingroup$ Yes, that's it, it's not very obvious on my laptop, but become clear in the quote mode! Thank you so much! $\endgroup$
    – 0o0o0o0
    Commented Aug 20, 2022 at 0:24

3 Answers 3

6
$\begingroup$

ArrayMesh make the task easy.

m = Reverse@SparseArray[{Band[{1, 1}] -> 1}, {10, 10}];
m // MatrixForm
ArrayMesh[m, DataRange -> {{0, 1}, {0, 1}}, 
 MeshCellStyle -> {{2, ;;} -> Blue}, PlotTheme -> "Detailed"]

enter image description here

Or

ArrayMesh[SparseArray[{{x_, y_} /; x + y == 10 -> 1}, {10, 10}], 
 DataRange -> {{0, 1}, {0, 1}}, 
 MeshCellStyle -> {2 -> Green, 1 -> Red}, PlotTheme -> "Detailed"]

enter image description here

$\endgroup$
6
$\begingroup$
RegionPlot[
 y > Floor[x, 0.1] && y < Ceiling[x, 0.1], {x, 0, 1}, {y, 0, 1}, 
 PlotPoints -> 100]

enter image description here

$\endgroup$
2
$\begingroup$
m = Reverse@SparseArray[{Band[{1, 1}] -> 1}, {10, 10}];
MatrixPlot[m, ColorRules -> {1 -> Blue}]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ Nice : would work with IdentityMatrix too $\endgroup$
    – chris
    Commented Aug 20, 2022 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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