'matplotlib to plot a multicolored line - Error in savefig

I am having trouble saving the figure from multicolored line plots. I have tried many different iterations of .savefig but cannot get it to work correctly. I work on a unix HPC server and am running the script and keep getting the same error every time. The code works fine until I try to save the figure in which I get this error.

login-3:/<2>second/guiLIGN2/namd/clustered: python better.py
Traceback (most recent call last):
  File "better.py", line 68, in <module>
    plt.savefig(fname)
  File "/software/spack-software/2020.05.14/linux-rhel8-ivybridge/gcc-8.4.0/py-matplotlib-3.2.1-7nlj5xaillyxgtlitzmkwd4mrusj2654/lib/python3.7/site-packages/matplotlib/pyplot.py", line 723, in savefig
    res = fig.savefig(*args, **kwargs)
  File "/software/spack-software/2020.05.14/linux-rhel8-ivybridge/gcc-8.4.0/py-matplotlib-3.2.1-7nlj5xaillyxgtlitzmkwd4mrusj2654/lib/python3.7/site-packages/matplotlib/figure.py", line 2203, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/software/spack-software/2020.05.14/linux-rhel8-ivybridge/gcc-8.4.0/py-matplotlib-3.2.1-7nlj5xaillyxgtlitzmkwd4mrusj2654/lib/python3.7/site-packages/matplotlib/backend_bases.py", line 2105, in print_figure
    **kwargs)
  File "/software/spack-software/2020.05.14/linux-rhel8-ivybridge/gcc-8.4.0/py-matplotlib-3.2.1-7nlj5xaillyxgtlitzmkwd4mrusj2654/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py", line 514, in print_png
    FigureCanvasAgg.draw(self)
  File "/software/spack-software/2020.05.14/linux-rhel8-ivybridge/gcc-8.4.0/py-matplotlib-3.2.1-7nlj5xaillyxgtlitzmkwd4mrusj2654/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py", line 393, in draw
    self.figure.draw(self.renderer)
  File "/software/spack-software/2020.05.14/linux-rhel8-ivybridge/gcc-8.4.0/py-matplotlib-3.2.1-7nlj5xaillyxgtlitzmkwd4mrusj2654/lib/python3.7/site-packages/matplotlib/artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/software/spack-software/2020.05.14/linux-rhel8-ivybridge/gcc-8.4.0/py-matplotlib-3.2.1-7nlj5xaillyxgtlitzmkwd4mrusj2654/lib/python3.7/site-packages/matplotlib/figure.py", line 1736, in draw
    renderer, self, artists, self.suppressComposite)
  File "/software/spack-software/2020.05.14/linux-rhel8-ivybridge/gcc-8.4.0/py-matplotlib-3.2.1-7nlj5xaillyxgtlitzmkwd4mrusj2654/lib/python3.7/site-packages/matplotlib/image.py", line 137, in _draw_list_compositing_images
    a.draw(renderer)
  File "/software/spack-software/2020.05.14/linux-rhel8-ivybridge/gcc-8.4.0/py-matplotlib-3.2.1-7nlj5xaillyxgtlitzmkwd4mrusj2654/lib/python3.7/site-packages/matplotlib/artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/software/spack-software/2020.05.14/linux-rhel8-ivybridge/gcc-8.4.0/py-matplotlib-3.2.1-7nlj5xaillyxgtlitzmkwd4mrusj2654/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 2630, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "/software/spack-software/2020.05.14/linux-rhel8-ivybridge/gcc-8.4.0/py-matplotlib-3.2.1-7nlj5xaillyxgtlitzmkwd4mrusj2654/lib/python3.7/site-packages/matplotlib/image.py", line 137, in _draw_list_compositing_images
    a.draw(renderer)
  File "/software/spack-software/2020.05.14/linux-rhel8-ivybridge/gcc-8.4.0/py-matplotlib-3.2.1-7nlj5xaillyxgtlitzmkwd4mrusj2654/lib/python3.7/site-packages/matplotlib/artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/software/spack-software/2020.05.14/linux-rhel8-ivybridge/gcc-8.4.0/py-matplotlib-3.2.1-7nlj5xaillyxgtlitzmkwd4mrusj2654/lib/python3.7/site-packages/matplotlib/collections.py", line 297, in draw
    self.update_scalarmappable()
  File "/software/spack-software/2020.05.14/linux-rhel8-ivybridge/gcc-8.4.0/py-matplotlib-3.2.1-7nlj5xaillyxgtlitzmkwd4mrusj2654/lib/python3.7/site-packages/matplotlib/collections.py", line 787, in update_scalarmappable
    if self._A.ndim > 1:
AttributeError: 'list' object has no attribute 'ndim' 

I work in terminal and cannot show the figure so I cannot see the result but without the savefig it goes with no errors. I attached the code to generate below.

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib.colors import ListedColormap, BoundaryNorm

dist = np.genfromtxt('proa.dat')
num_dist = len(dist)

dcd = np.genfromtxt('cluster_1.dat', usecols=0)
cluster1 = np.genfromtxt('cluster_1.dat', usecols=1)
clust1 = []
cluster2 = np.genfromtxt('cluster_2.dat', usecols=1)
clust2 = []
cluster3 = np.genfromtxt('cluster_3.dat', usecols=1)
clust3 = []

ns = len(dcd) * 2
x = np.linspace(0, ns, num_dist)
step = int(num_dist / len(dcd)) 

for i in dcd:
    v = i - 1
    for b in range(step):
        clust1.append(int(cluster1[int(v)]))
        clust2.append(int(cluster2[int(v)]))
        clust3.append(int(cluster3[int(v)]))


points = np.array([x, dist]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)

fig, axs = plt.subplots(3) #, sharex=True, sharey=True
#fig = Figure()

cmap = ListedColormap(['r', 'g', 'b', 'y', 'm', 'c', 'k', 'w'])
norm = BoundaryNorm([1 , 2, 3, 4, 5, 6, 7, 8], cmap.N)
lc = LineCollection(segments, cmap=cmap, norm=norm)
lc.set_array(clust1)
lc.set_linewidth(2)
line = axs[0].add_collection(lc)
fig.colorbar(line, ax=axs[0])

cmap = ListedColormap(['r', 'g', 'b', 'y', 'm', 'c', 'k', 'w'])
norm = BoundaryNorm([1 , 2, 3, 4, 5, 6, 7, 8], cmap.N)
lc = LineCollection(segments, cmap=cmap, norm=norm)
lc.set_array(clust2)
lc.set_linewidth(2)
line = axs[1].add_collection(lc)
fig.colorbar(line, ax=axs[1])

cmap = ListedColormap(['r', 'g', 'b', 'y', 'm', 'c', 'k', 'w'])
norm = BoundaryNorm([1 , 2, 3, 4, 5, 6, 7, 8], cmap.N)
lc = LineCollection(segments, cmap=cmap, norm=norm)
lc.set_array(clust3)
lc.set_linewidth(2)
line = axs[2].add_collection(lc)
fig.colorbar(line, ax=axs[2])


axs[0].set_xlim(x.min(), x.max())
axs[0].set_ylim(dist.min(), dist.max())


fname = 'one.png'
plt.savefig(fname) 

I am trying to get a line colored to the cluster number to be represented as a color on top of a distance v time graph. Any suggestions would be greatly appreciated thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source