1

Is there a way to have a third variable control the color gradient on a log-scaled plot? Also: how would I make a color legend for it? I want it to look something like the image linked below. (https://i.stack.imgur.com/iNkHw.png)

#creating arrays
sulfate = np.array(master['SO4-2_(input)'])
chloride = np.array(master['Cl-_(input)'])
pH = np.array(master['pH'])

#create plot
fig, ax = plt.subplots()
plt.figure(1)
ax.loglog(chloride,sulfate,'.',c=pH,cmap='hsv')

#add 1:1 ratio line
plt.plot( [0,1],[0,1] )
#x and y axes lims
plt.xlim(10.0E-7,10.0E-1)
plt.ylim(10.0E-7,10.0E-1)

plt.show()

When I try to use the technique for a typical scatter plot is says that the variable is not a valid value for color.

  • Could you explain (in code) what you mean by *"technique for a typical scatter plot "*? Could you provide some limited test data? What is the `dtype` of the `pH` array? Note that you need to leave out `plt.figure(1)` as you already created the figure the line before. Instead of `ax.loglog(...)` it would be much handier to use `ax.scatter(...)` and then `ax.set_xscale('log')` and `ax.set_yscale('log')`. – JohanC Feb 02 '23 at 21:55

1 Answers1

0

As suggested in JohanC's comment, use the scatter function and then set the axis scales to logarithmic separately. To get a colorbar, use colorbar. If you also want the colorbar to have logarithmic scaling (I am not sure if that is what you want), use the norm argument of scatter and provide a matplotlib.colors.LogNorm.

from matplotlib.colors import LogNorm
import matplotlib.pyplot as plt
import numpy as np

# Create come mock data
sulfate = np.random.rand(20)
chloride = np.random.rand(20)
pH = np.arange(20) + 1

# Create the plot
plt.scatter(sulfate, chloride, c=pH, norm=LogNorm(), cmap="cividis")
plt.xscale("log")
plt.yscale("log")
plt.colorbar()

Scatter plot with logarithmic x and y axis, and points colored by a third variable

Depending on what data format your original variable master is in, there might be easier ways to produce this plot. For example, with xarray:

import xarray as xr

ds = xr.Dataset(
    data_vars={"sulfate": ("x", sulfate), "chloride": ("x", chloride), "pH": ("x", pH)}
)
ds.plot.scatter(
    x="sulfate",
    y="chloride",
    hue="pH",
    xscale="log",
    yscale="log",
    norm=LogNorm(),
    cmap="cividis",
)

Or with pandas:

df = ds.to_dataframe()
ax = df.plot.scatter(
    x="sulfate",
    y="chloride",
    c="pH",
    loglog=True,
    colorbar=True,
    norm=LogNorm(),
    cmap="cividis",
)
astoeriko
  • 730
  • 8
  • 20