-1

While making this contour plot, I'm always not able to plot either the first row and first column or the last row and the last column. The .dat file has a 44 X 54 matrix of float values whose Contour plot I'm trying to make.

Every time I try to take my x1 list of 56 size in order to populate my plot with all the values from my .dat file, it says the size of x1 should be equal to the number of columns in z.

In this image you can see the first row and column is being omitted

In this image you can see the last row and column is being omitted

import numpy as np

import matplotlib.pyplot as plt

def contour_plot(filename):

#Reading the porosity data from the dat file

    #data = (np.loadtxt(filename))
    data = np.random.random((44,54))

#Making two empty lists to account for the size of each grid block

    x1=[]
    for i in range(1,55):
        x1.append(i*130.75)

    x2=[]
    for j in range(1,45):
        x2.append(j*130.75)

#Using pyplot.contourf() to make contours using the porosity values and pyplot.colorbar() to show the chart for correlation

    plt.contourf(x1, x2, data[::-1])
    plt.colorbar()
    plt.title("Porosity")
    plt.savefig('Porosity.png',bbox_inches='tight')

contour_plot("Nechelik.dat")

I want all the 44 rows and 54 columns to be plotted in the Contour plot however that is not happening.

wwii
  • 23,232
  • 7
  • 37
  • 77
Kunal Singhal
  • 33
  • 1
  • 7
  • why are you taking off the last data point with `data[::-1]` then? Also please present a complete example with a sample of your data and the errors you get – mobelahcen Jul 30 '19 at 15:30
  • 1
    Welcome to So. please provide a [mcve], including some example data. We should be able to easily copy and paste from your question and reproduce your problem. When asking about code that produces an Exception, always include the complete Traceback in the question. Copy the Traceback and paste it in the question, then format it as code (select it and type ctrl-k). – wwii Jul 30 '19 at 15:46
  • 2
    @MohamedBelahcen - the last value in an extended slice determines the step, in this case a step of `-1` *reverses* the sequence it does not exclude the last element. [Understanding slice notation](https://stackoverflow.com/questions/509211/understanding-slice-notation). – wwii Jul 30 '19 at 15:53
  • What is `data`'s shape - `print(data.shape)`? – wwii Jul 30 '19 at 18:54

1 Answers1

0

I think that the error is on your range(1, 55) and range(1, 45).

From the docs: https://docs.python.org/2/library/functions.html

>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1, 11)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

What I can assume is that you want the first use case.

>>> x1=[]
>>> for i in range(55):
...     x1.append(i*130.75)
... 

>>> x1

[0.0, 130.75, 261.5, 392.25, 523.0, 653.75, 784.5, 915.25, 1046.0, 1176.75, 1307.5, 1438.25, 1569.0, 1699.75, 1830.5, 1961.25, 2092.0, 2222.75, 2353.5, 2484.25, 2615.0, 2745.75, 2876.5, 3007.25, 3138.0, 3268.75, 3399.5, 3530.25, 3661.0, 3791.75, 3922.5, 4053.25, 4184.0, 4314.75, 4445.5, 4576.25, 4707.0, 4837.75, 4968.5, 5099.25, 5230.0, 5360.75, 5491.5, 5622.25, 5753.0, 5883.75, 6014.5, 6145.25, 6276.0, 6406.75, 6537.5, 6668.25, 6799.0, 6929.75, 7060.5]

Same with x2.

Adrián
  • 3
  • 1
  • Hi Adrian! I did try this, but when I do this, the size of x1 becomes 56 but the z is a 44 X 54 matrix and therefore I'm missing out on one row and one column – Kunal Singhal Jul 30 '19 at 16:06
  • You could use [`np.linspace`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html#numpy.linspace) along with the [shape of `data`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.shape.html#numpy.ndarray.shape) to produce the row and column grids. Using `data`'s shape alleviates you from *hardcoding* the length of those arrays. Like this: `cols = np.linspace(130.75, 130.75*data.shape[1], data.shape[1])` and similarly for the rows. For this question, `x1 = cols`. – wwii Jul 30 '19 at 16:20
  • A little easier might be `cols = np.linspace(1, data.shape[1], data.shape[1]) * 130.75` or `np.arange(1, data.shape[1] + 1) * 130.75 == cols` – wwii Jul 30 '19 at 16:27