Search code examples
pythonexcelpandasmatplotlibcolorbar

Plotting Excel data using a function that specifies which column to use for the colourbar


Sorry I'm very new to coding and hope this is ok to ask.

I have an excel spreadsheet that has multiple sheets, and they have columns X, Y, Z1 and Z2, for example:

X   Y   Z1  Z2
726841.75   7836563.42  21.78   4.26
726709.69   7836391.55  29.81   14.99
726633.57   7836014.98  33.61   19.18
726596.34   7835726.40  55.67   38.04
726562.61   7835415.88  63.88   21.60
726529.13   7835185.27  58.39   28.52
726494.13   7834882.61  61.48   22.97

I would like to go through the sheets and plot X and Y (as coordinates) and have either Z1 or Z2 as the colour attribute by specifying which one in a function.

So far I can plot X and Y and have Z1 as the colorbar:

import pandas as pd
import matplotlib.pyplot as plt

def plot(workbook, names):
    for sheet in names:
        df = pd.read_excel(workbook, sheet_name=(sheet))
        plt.scatter(df["X"], df["Y"], c=df["Z1"],cmap='viridis', vmin=0, vmax=650)

workbook = pd.ExcelFile('test.xlsx')
names = workbook.sheet_names

But I want to interchange Z1 with Z2 as part of the function so it could be plot(workbook, names, Z) where Z can be either the Z1 or Z2 column. Hope this makes sense.


Solution

  • Just as an example:

    def plot(workbook, names, c_col):
        for sheet in names:
            df = pd.read_excel(workbook, sheet_name=(sheet))
            plt.scatter(df['X'], df['Y'], c=df[c_col], cmap='viridis', vmin=0, vmax=650)
    plot('test.xlsx', names, 'Z2')
    

    Note that pandas builds on top of matplotlib, so if your data is in a pandas dataframe, you have access to some convenient plotting functions from matplotlib.
    You might want to try e.g.:

    df.plot.scatter('X', 'Y', c=c_col, cmap='viridis', vmin=0, vmax=650)