Home > Enterprise >  Adjusting Plotted Values of Contour Plots
Adjusting Plotted Values of Contour Plots

Time:11-29

I'm making contour plots which are basically analytical or numerical solutions to a fluid dynamic system. I don't think the technical stuff really matters too much, but here's my plots. The first plot is the numerical (Matrix system) solution, and the second plot is the nice closed form (single forumla) solution. Numerical solution to PDE Analytic solution to PDE

As can be seen, my second plot has the bubbles on the right hand side. Looking at the legend/scale, I have negative values. I'd like to not have negative values, or not plot them, although I'm not sure how to adjust this within my code. I've spent some time looking into how to adjust the z values to being positive only, but I can't seem to get it. I'll drop my plot code, and then my nice closed form function that is used in the plot.

import numpy as np 
import matplotlib.pyplot as plt
import scipy as sp
import scipy.special as sp1
from mpl_toolkits.mplot3d import Axes3D

def v(r,z,gamma):
    a=r*(1-z/gamma) 
    sums = 0
    for n in range(1,26):
        sums  = ((sp1.iv(1(n*np.pi*r)/gamma))/(n*sp1.iv(1(n*np.pi)/gamma)))*np.sin(n*np.pi*z/gamma)    
    return a-(2/np.pi)*sums

def plot_contour(a, filename=None, zlabel='v(r,z)',cmap=plt.cm.gnuplot):
    fig = plt.figure(figsize=(5,4))
    ax = fig.add_subplot(111)

    x = np.arange(a.shape[0])
    y = np.arange(a.shape[1])
    X, Y = np.meshgrid(x, y)
    Z = a[X, Y]
    cset = ax.contourf(X, Y, Z, 20, cmap=cmap)
    ax.set_xlabel('r')
    ax.set_ylabel('z')
    ax.set_title('\u0393=2.5')
    ax.axis('off')
    ax.set_aspect(1)

    cb = fig.colorbar(cset, shrink=0.5, aspect=5)
    cb.set_label(zlabel)
    
    if filename:
        fig.savefig(filename,dpi=1600)
        plt.close(fig)
        return filename
    else:
        return ax
...
plot_contour(v1, 'gamma25e 1')

This is all the necessary code. The rest of it is the matrix solution stuff, which is just a bunch of linear algebra. Any help on what I need to add or adjust to prevent negative values from showing up on the second plot. It should look exactly like the first.

CodePudding user response:

I've spent some time looking into how to adjust the z values to being positive only

what you can do depends greatly on what you want to do with the results below zero, if your sole purpose is to make the points below zero show as zero, you can simply make them zero, however that would be showing a wrong result.

    x = np.arange(a.shape[0])
    y = np.arange(a.shape[1])
    X, Y = np.meshgrid(x, y)
    Z = a[X, Y]
    Z[Z < 0] = 0

another solution is to subtract the minimum value of you data so that the minimum value of the result is 0.

    x = np.arange(a.shape[0])
    y = np.arange(a.shape[1])
    X, Y = np.meshgrid(x, y)
    Z = a[X, Y]
    Z -= np.amin(Z)
  • Related