Overview
I have some data like so:
- X values: range of frequencies
- Y values:
float
value that can be positive or negative- The Y-value is electrical reactance, if you're curious
I represent the data in a log-log plot. Since you can only take the logarithm of a positive number, I plot abs(Y-value)
.
On the log-log plot, I would like to represent the original number's sign by changing the marker symbol:
+
marker if sign was+
-
marker if sign was-
Generally: below I have placed my current method. I would like to "do it better". Hopefully matplotlib
has a more standard way of doing this.
Details
Currently, here is what my plot looks like:
Here is some semblance of my current code (note: the data was pulled from equipment, so I just used random.uniform
in this case):
import numpy as np
import matplotlib.pyplot as plt
from random import uniform
# Generating data
num_pts = 150
freq_arr = np.logspace(start=2, stop=6, num=num_pts, base=10)
reactance_arr = [uniform(-1000,1000) for i in range(num_pts)]
abs_reactance_arr = [abs(i) for i in reactance_arr]
reactance_signed_marker = [1 if reactance_arr[i] >= 0 else -1 for i in range(len(reactance_arr))]
# Taken from here: https://stackoverflow.com/questions/28706115/how-to-use-different-marker-for-different-point-in-scatter-plot-pylab
x = np.array(freq_arr)
y = np.array(abs_reactance_arr)
grouping = np.array(reactance_signed_marker)
# Plotting
fig1, ax1 = plt.subplots()
positives_line = ax1.scatter(
x[grouping == 1],
y[grouping == 1],
s=16,
marker="+",
label="Reactance",
)
# Match color between the two plots
col = positives_line.get_facecolors()[0].tolist()
ax1.scatter(
x[grouping == -1],
y[grouping == -1],
s=16,
marker="_",
label="Reactance",
color=col,
)
ax1.set_xlim([freq_arr[0], freq_arr[-1]])
ax1.set_xscale("log")
ax1.set_xlabel("Frequency (Hz)")
ax1.set_yscale("log")
ax1.set_ylabel("Value (Ohm)")
ax1.legend()
ax1.set_title("Reactance")
How can I do this better? Currently, this feels very manual. I am wondering:
- Is there a better way to parse
-
and+
values into markers?- The current way is quite cumbersome with 1. plotting
+
, 2. extracting color, 3. plotting-
with the same color
- The current way is quite cumbersome with 1. plotting
- I would like the legend to be one united category