1

I have a question regarding the plotting of a 2D array with matplotlib. In my code, I have a 2D array named z of len(z) = 20 , and z has the values :

   [[ 642.3774486   662.59980588  706.80142179  764.78786911  831.67963477
   904.67872269  982.01426528 1062.49208551 1145.27029231 1229.73549967
  1315.42936618 1402.00251422 1489.18433714 1576.7625077  1664.56866033
  1752.46813939 1840.35250424 1928.13395024 2015.74109019 2103.11572013]
 [ 554.60565024  560.31827232  591.87923587  638.51633542  695.03697015
   758.44479983  826.83191468  898.90395242  973.74278531 1050.67523901
  1129.19496311 1208.91328775 1289.52693752 1370.79606051 1452.52883572
  1534.57042218 1616.79485775 1699.09901217 1781.39800199 1863.6216653 ]
 [ 484.80770831  476.01059519  494.93090638  530.21865818  576.36816197
   630.18473341  689.62342052  753.28967576  820.18913475  889.58883479
   960.93441647 1033.79791772 1107.84339435 1182.80346976 1258.46286755
  1334.64656142 1411.2110677  1488.03793055 1565.02877024 1642.1014669 ]
 [ 432.98362283  409.67677451  415.95643334  439.89483737  475.67321023
   519.89852343  570.3887828   625.64925554  684.60934062  746.47628701
   810.64772628  876.65640413  944.13370762 1012.78473545 1082.37075581
  1152.6965571  1223.60113409 1294.95070536 1366.63339492 1438.55512495]
 [ 399.13339379  361.31681026  354.95581673  367.54487301  392.95211493
   427.58616989  469.12800152  515.98269176  567.00340294  621.33759567
   678.33489253  737.48874699  798.39787733  860.73985757  924.25250052
   988.72040921 1053.96505692 1119.83733661 1186.21187604 1252.98263943]
 [ 383.25702119  330.93070245  311.92905657  313.16876508  328.20487607
   353.24767279  385.84107667  424.28998442  467.37132169  514.17276077
   563.99591521  616.29494628  670.63590348  726.66883614  784.10810167
   842.71811777  902.30283619  962.6978243  1023.76421361 1085.38401036]
 [ 385.35450503  318.51845109  286.87615284  276.7665136   281.43149365
   296.88303213  320.52800827  350.57113352  385.71309689  424.98178231
   467.63079434  513.07500201  560.84778607  610.57167115  661.93755925
   714.68968276  768.6144719   823.53216843  879.29040761  935.75923772]
 [ 405.4258453   324.08005616  279.79710556  258.33811855  252.63196767
   258.49224791  273.18879631  294.82613906  322.02872853  353.76466029
   389.23952991  427.82891418  469.0335251   512.44836259  557.74087328
   604.6351042   652.89996405  702.340369    752.79045805  804.10832153]
 [ 443.47104202  347.61551768  290.69191471  257.88357994  241.80629812
   238.07532013  243.82344079  257.05500104  276.3182166   300.52139471
   328.82212191  360.55668279  395.19312056  432.29891048  471.51804375
   512.55438207  555.15931264  599.12242601  644.26436494  690.43126177]
 [ 499.49009518  389.12483563  319.56058031  275.40289778  248.95448502
   235.63224878  232.43194171  237.25771947  248.58156112  265.25198557
   286.37857036  311.25830784  339.32657247  370.12331481  403.26907065
   438.44751639  475.39251767  513.87833946  553.71212826  594.72805845]
 [ 573.48300477  448.60801002  366.40310234  310.89607205  274.07652836
   251.16303388  239.01429907  235.43429433  238.81876207  247.95643287
   261.90887525  279.93378933  301.43388082  325.92157557  352.993954
   382.31450714  413.59957914  446.60810935  481.13374802  516.99871158]
 [ 665.44977081  526.06504086  431.21948081  364.36310276  317.17242814
   284.66767542  263.57051287  251.58472563  247.02981947  248.63473661
   255.41303657  266.58312726  281.51504561  299.69369278  320.69269378
   344.15535434  369.78049705  397.31173568  426.52922422  457.24322114]
 [ 775.39039329  621.49592813  514.00971573  435.80398992  378.24218436
   336.1461734   306.10058311  285.70901337  273.2147333   267.28689679
   266.89105434  271.20632163  279.57006684  291.43966643  306.36529001
   323.97005797  343.9352714   365.98921845  389.89855687  415.46158715]
 [ 903.3048722   734.90067184  614.77380708  525.21873351  457.28579702
   405.59852782  366.60450978  337.80715755  317.37350358  303.91291341
   296.34292854  293.80337244  295.5989445   301.15949651  310.01174267
   321.75861805  336.06390219  352.64055766  371.24174595  391.65380959]
 [1049.19320756  866.27927199  733.51175488  632.60733354  554.30326611
   493.02473868  445.0822929   407.87915817  379.50613029  358.51278647
   343.76865919  334.37427969  329.60167861  328.85318304  331.63205178
   337.52103456  346.16638942  357.26575331  370.55879147  385.81988847]
 [1213.05539936 1015.63172859  870.22355911  757.96979001  669.29459165
   598.42480597  541.53393246  495.92501523  459.61261345  431.08651597
   409.16824628  392.91904338  381.57826916  374.520726    371.22621733
   371.25730752  374.24273309  379.8648054   387.84969343  397.9598238 ]
 [1394.89144759 1182.95804162 1024.90921978  901.30610293  802.25977363
   721.79872971  655.95942846  601.94472873  557.69295304  521.63410191
   492.5416898   469.43766351  451.52871614  438.16212541  428.79423931
   422.96743691  420.2929332   420.43771393  423.11445184  428.07361556]
 [1594.70135227 1368.25821109 1197.5687369  1062.61627228  953.19881205
   863.14650989  788.3587809   725.93829867  673.74714907  630.15554429
   593.88898977  563.93014008  539.45301957  519.77738125  504.33611774
   492.65142275  484.31698975  478.9844789   476.35306668  476.16126376]
 [1812.48511338 1571.532237   1388.20211045 1241.90029807 1122.11170691
  1022.46814651  938.73198977  867.90572504  807.77520155  756.65084311
   713.21014617  676.39647309  645.35117944  619.36649354  597.8518526
   580.30926502  566.31490274  555.50510031  547.56553796  542.22276841]
 [2048.24273094 1792.78011936 1596.80934044 1439.1581803  1308.9984582
  1199.76363956 1107.07905509 1027.84700786  959.77711046  901.11999837
   850.50515902  806.83666254  769.22319574  736.92946227  709.34144391
   685.94096373  666.28667217  649.99957816  636.75186568  626.25812949]]

I wanted to plot the first 20 set of data of z, so z[0] against my other variable M. I did the following:

M = np.arange(15.5,16.5, 0.05)
plt.plot(M, Z[0], label = r'$\chi^2$ for $\Omega_m[0] $ ')

and it gave me the folllwing plot (ignore the label whith color blue, there were 2 same datas plotted and only one label) :

enter image description here

Then I tried the following code, which gave me the other pic.

plt.plot(M, Z[0:20], label = r'$\chi^2$ for$\Omege_m = 0$ ')

enter image description here

But I don't understand why, with the same data, the shape of the function is obviously different between the two pics. Could anyone explain me why the second image is different from the first one, and what does it plot exactly ? How does matplotlib plot a 2D array ?

And if i can explain a bit the background of z, it is a function that depends on 2 parameters, M and Omega_M, Omega_m = np.arange(0.0, 1.0, 0.05) (len(Omega_m) =20) and z[0] corresponds to the 20 values of the Z function for each value of Omega_m and for M[0], z1 correspond of the 20 values of the Z function for each values of Omega_m for M1 etc, until the function is calculated for each value of each parameter.

6
  • it's not an easy task to answer your question when we don't have z and M. So, could you please post their values?
    – Anwarvic
    Commented May 17, 2020 at 8:40
  • sure ! I'm putting the values in my post
    – Apinorr
    Commented May 17, 2020 at 8:41
  • thanks for the update... now I need the values of M please
    – Anwarvic
    Commented May 17, 2020 at 8:46
  • It's added, juste above the first plot line, M = np.arange(15.5,16.5, 0.05)
    – Apinorr
    Commented May 17, 2020 at 8:47
  • yup... sorry :)
    – Anwarvic
    Commented May 17, 2020 at 8:48

1 Answer 1

2

First, let's explain why the two graphs differ. Because in the first graph, you're plotting the first row of Z with M. In the second graph, you're drawing the columns of Z with M. And this became so clear when I tried to plot the first three columns of Z:

plt.plot(M, Z[:, 0], label = r'$\chi^2$ for $\Omega_m[0] $ ')
plt.plot(M, Z[:, 1], label = r'$\chi^2$ for $\Omega_m[0] $ ')
plt.plot(M, Z[:, 2], label = r'$\chi^2$ for $\Omega_m[0] $ ')
plt.show()

Which produced this graph: enter image description here

And that makes total sense as it will throw an error when I pass Z with one less row:

plt.plot(M, Z[0:19], label = r'$\chi^2$ for $\Omega_m[0] $ ')
ValueError: x and y must have same first dimension, but have shapes (20,) and (19, 20)

So, to produce 20 curves that match the rows of Z, not the columns, you need to transpose your Z array using Z.T notation like so:

plt.plot(M, Z.T, label = r'$\chi^2$ for $\Omega_m[0] $ ')
plt.show()

Which will get this graph: enter image description here

2
  • Thank you so much, that makes total sense ! And just to be clear that I understand well, Z[:, 0], basically plots the first element of each columns against M ?
    – Apinorr
    Commented May 17, 2020 at 9:13
  • thank you ! I had an hard time understanding the syntaxe of 2D array :))
    – Apinorr
    Commented May 17, 2020 at 9:17

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.