Sien Smania
Sien Smania

Reputation: 21

How to have third variable control the color gradient on a log-scaled plot in python?

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.sstatic.net/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.

Upvotes: 1

Views: 463

Answers (1)

astoeriko
astoeriko

Reputation: 890

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",
)

Upvotes: 0

Related Questions