user25742608
user25742608

Reputation: 21

Tikzplotlib module throws attribute error: module 'webcolors' has no attribute 'CSS3_HEX_TO_NAMES'

I would like to convert matplotlib figures to PGFPlots/Tikz figures while using the module tikzplotlib:

import matplotlib.pyplot as plt
import numpy as np
import tikzplotlib

x = np.linspace(0, 2*np.pi, 51)
y = np.sin(x)

plt.plot(x, y)
plt.xlabel("x")
plt.ylabel("y")

tikzplotlib.save('test.tex')

When I run this code, I get an attribute error:

AttributeError: module 'webcolors' has no attribute 'CSS3_HEX_TO_NAMES'

The documentations of tikzplotlib and webcolors did not give me an idea to solve this.

I have found related questions(1, 2) to the described attribute error from the webcolors module without a solution for me.

Is someone able to reproduce this error or knows a solution for this?

I am working with python 3.9.5, webcolors 24.6.0 and tikzplotlib 0.10.1 in Visual Studio Code Version 1.90.2 on Windows 11.

Upvotes: 2

Views: 1317

Answers (2)

Quiggin
Quiggin

Reputation: 31

tikzplotlib is no longer regularly maintained, so it may be best to clone/fork it and update a local copy.

The offending line is for h, name in webcolors.CSS3_HEX_TO_NAMES.items(): in /src/tikzplotlib/_color.py. For webcolors 24.8.0, you can replace this line with

for name in webcolors.names('css3'):
    h = webcolors.name_to_hex(name)

Upvotes: 3

user25742608
user25742608

Reputation: 21

Solved:

The module webcolors had recent changes and the attributes for the mapping of color names/values is no longer publically accessible. Since tikzplotlib has not been updated to this changes, an older version (e.g. version 1.13) of webcolors must be used.

Upvotes: 0

Related Questions