Home > OS >  How to adapt this python script to apt installed matplotlib vs pip3 installed
How to adapt this python script to apt installed matplotlib vs pip3 installed

Time:01-27

I have a script (MWE supplied)

import matplotlib.pyplot as plt
import matplotlib

s_xLocs = [864]
s_yLocs = [357]
s_score = [0.33915146615180547]
sMax = 0.34704810474264386
for i in range(len(s_xLocs)):
    plt.scatter(s_xLocs[i], s_yLocs[i],  c=s_score[i], s=(20*(s_score[i] 1.5)**4), cmap="plasma", marker='.', vmin=0, vmax=sMax)
matplotlib.pyplot.close()

which was being used to generate some plots using matplotlib. On my dev machine, I used matplotlib installed via pip3. The script is now being used on some other machines managed by IT and limited to using the version of matplotlib installed via apt install python3-matplotlib. This has caused my script to fail, throwing the error

Traceback (most recent call last):
  File "./heatmaps.py", line 9, in <module>
    plt.scatter(s_xLocs[i], s_yLocs[i],  c=s_score[i], s=(20*(s_score[i] 1.5)**4), cmap="plasma", marker='.', vmin=0, vmax=sMax)
  File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 2836, in scatter
    __ret = gca().scatter(
  File "/usr/lib/python3/dist-packages/matplotlib/__init__.py", line 1601, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/axes/_axes.py", line 4451, in scatter
    self._parse_scatter_color_args(
  File "/usr/lib/python3/dist-packages/matplotlib/axes/_axes.py", line 4264, in _parse_scatter_color_args
    n_elem = c_array.shape[0]
IndexError: tuple index out of range

After reading this Q/A I was able to seemingly narrow down the issue to the colormap c argument. After also reading the documentation I also tried passing in the entire list of s_score with no indexing ala

plt.scatter(s_xLocs[i], s_yLocs[i],  c=s_score, s=(20*(s_score[i] 1.5)**4), cmap="plasma", marker='.', vmin=0, vmax=sMax)

but that gave a different and more confusing (IMO) error:

...
ValueError: Invalid RGBA argument: 0.33915146615180547
During handling of the above exception, another exception occurred:
...
ValueError: 'c' argument has 1 elements, which is not acceptable for use with 'x' with size 1, 'y' with size 1.

I am hoping someone can provide a solution to this issue which will work with python3-matplotlib and perhaps also clarify the errors/what is different between the version installed with pip3 vs apt.

CodePudding user response:

This could be occasioned because of different versions of matplotlib installed. As the problem is with the c parameter, I suggest creating a pallet and then getting the color based on the float value:

import matplotlib.pyplot as plt
from matplotlib import cm
import matplotlib as mpl

s_xLocs = [864]
s_yLocs = [357]
s_score = [0.33915146615180547]
sMax = 0.34704810474264386

palette = cm.get_cmap('plasma')
norm = mpl.colors.Normalize(vmin=0, vmax=sMax)

for i in range(len(s_xLocs)):
    color = palette(norm(s_score[i]))
    plt.scatter(s_xLocs[i], s_yLocs[i],  color=color, s=(20*(s_score[i] 1.5)**4), marker='.')

CodePudding user response:

Another solution that did not break other functionality was to change plotting to this method:

import matplotlib.pyplot as plt
import matplotlib

s_xLocs = [864]
s_yLocs = [357]
s_score = [0.33915146615180547]
sMax = 0.34704810474264386
sSizes = [(20*(size 1.5)**4) for size in s_score]

plt.scatter(s_xLocs, s_yLocs,  c=s_score, s=sSizes, cmap="plasma", marker='.', vmin=0, vmax=sMax)
plt.show()
  • Related