Search code examples
pythonmatplotlibplotscatter-plotscatter

Getting matplotlib figure axes type


I wanted to plot a scatter from a csv data file (refer below).

enter image description here

I have already define for the plot and it already print the graph that I wanted.

def plot_calibration_curve(calib_data):
    plot = calib_data.plot(kind='scatter', x="conc X", y="Abs")
    plot.set_xlabel("$\mathrm{[X]\ /\ mol\ dm}^{-3}$")
    plot.set_ylabel('Absorbance')

plot_calibration_curve(calibration_data_X)

Plot output:

enter image description here

But when I ran this test, it gives a blank error;

assert isinstance(plot_calibration_curve(calibration_data_X), matplotlib.figure.Axes)
Output= AssertionError:

How do I get the matplotlib.figure.Axes type for this?


Solution

  • Your function

    def plot_calibration_curve(calib_data):
        plot = calib_data.plot(kind='scatter', x="conc X", y="Abs")
        plot.set_xlabel("$\mathrm{[X]\ /\ mol\ dm}^{-3}$")
        plot.set_ylabel('Absorbance')
    

    just plots, doesn't return anything. That is, it returns None. You will get True if you do

    plot_calibration_curve(calib_data) is None
    

    You want to try:

    def plot_calibration_curve(calib_data):
        plot = calib_data.plot(kind='scatter', x="conc X", y="Abs")
        plot.set_xlabel("$\mathrm{[X]\ /\ mol\ dm}^{-3}$")
        plot.set_ylabel('Absorbance')
    
        # return
        return plot