ECSHackWeek / impedance.py

A Python package for working with electrochemical impedance data
MIT License
218 stars 103 forks source link

[BUG] Subplot aspect ratio #263

Open philsw opened 1 year ago

philsw commented 1 year ago

Hello, I am not able to change the aspect ratio of the subplot generated by plot_nyquist function. The size can be altered, but the aspect ratio is always around 1:5, regardless of x and y in fig, ax = plt.subplots(figsize = (x,y)). I used readBioLogic funciton to read.mpt files and changed the equivalent circuit, otherwise the script is identical to the getting_started script. Strangely, the plot size and ratio can be altered in the getting_started script, but not in my script. Thanks so much for your help.

Full code:

from impedance import preprocessing
import matplotlib.pyplot as plt
from impedance.visualization import plot_nyquist

frequencies, Z = preprocessing.readBioLogic('../data/file.mpt')

frequencies, Z = preprocessing.ignoreBelowX(frequencies, Z)

from impedance.models.circuits import CustomCircuit

initial_guess = [20,0.8,5]
circuit = CustomCircuit('R0-p(R1,CPE1)-Wo1-C1', initial_guess=initial_guess, constants={'R0': 0.9, "R1": 1.32,"Wo1_0": 8.4,"Wo1_1":24})

circuit.fit(frequencies, Z)
Z_fit = circuit.predict(frequencies)

fig, ax = plt.subplots(figsize = (20,8))

plot_nyquist(Z, fmt='o', ax=ax, scale = 1)
plot_nyquist(Z_fit, fmt='-', ax=ax, scale = 1)

plt.legend(['Data', 'Fit'])
plt.show()
mdmurbach commented 1 year ago

Hi @philsw, I believe the issue here is that we use ax.set_aspect('equal') to make the aspect ratio square (as is typical in Nyquist plots). You can override this by setting the aspect ratio in matplotlib to 'auto' which should allow the ratio to be defined by whatever figsize you specify instead

fig, ax = plt.subplots(figsize = (20,8))

plot_nyquist(Z, fmt='o', ax=ax, scale = 1)
plot_nyquist(Z_fit, fmt='-', ax=ax, scale = 1)
ax.set_aspect('auto')