【发布时间】:2021-03-23 03:30:17
【问题描述】:
我想在 matplotlib 中使用由外观命令组成的列表条目,以便于编辑线条颜色和类型。但是当我运行以下代码时,出现 2 个错误:
- '类型对象有多个关键字参数'ax'的值'
Traceback (most recent call last):
File "./plotMeltFront.py", line 34, in <module>
data.plot(0, 1, ax=ax1, label=myLabel, *sets[i][2].split())
File "/home/ksalscheider/.local/lib/python3.6/site-packages/pandas/plotting/_core.py", line 872, in __call__
plot_backend.__name__, self._parent, args, kwargs
File "/home/ksalscheider/.local/lib/python3.6/site-packages/pandas/plotting/_core.py", line 859, in _get_call_args
kwargs = dict(arg_def, **pos_args, **kwargs)
TypeError: type object got multiple values for keyword argument 'ax'
- 删除 ax 参数时,错误提示“color="C0" 不是有效的绘图类型”
Traceback (most recent call last):
File "./plotMeltFront.py", line 34, in <module>
data.plot(0, 1, label=myLabel, *sets[i][2].split())
File "/home/ksalscheider/.local/lib/python3.6/site-packages/pandas/plotting/_core.py", line 882, in __call__
raise ValueError(f"{kind} is not a valid plot kind")
ValueError: color="C0" is not a valid plot kind
两个我都不懂。对于 1 我没有在列表中声明第二个 ax 值,为什么这样说?编写data.plot(0, 1, label=myLabel, 'color="C0"') 时也会出现错误2(注意' 周围的颜色)。这模仿了列表条目的外观,在打印列表时。有没有办法从拆分列表条目中删除 '?
我在链接 python script with example data 中包含了带有一些示例集的脚本
#!/usr/bin/python3
import os
import matplotlib.pyplot as plt
import pandas as pd
sets = []
iOld = -1
#--------USER EDIT--------#
#["set directory name", "label", 'set appearence']
sets.append(["set1", "Experiment", 'color="C0" linestyle=":"'])
sets.append(["set2", "Linear", 'color="C1" linestyle="--"'])
sets.append(["set3", "Erf", 'color="C2" linestyle="-."'])
print(sets[0][2].split())
#------USER EDIT END------'
fig = plt.figure()
ax1 = fig.add_subplot()
for i in range(len(sets)):
for j in os.listdir(sets[i][0]):
filepath = os.path.abspath(sets[i][0]) + "/" + j
data = pd.read_csv(filepath, delimiter=',', engine='python',
header=None, skipfooter=1, error_bad_lines=False)
if i != iOld:
myLabel = sets[i][1];
else:
myLabel = '_nolegend_'
data.plot(0, 1, ax=ax1, label=myLabel, *sets[i][2].split())
iOld = i
plt.show()
PS:“nolegend”条目会抛出很多警告,有没有办法禁用它们?
【问题讨论】:
-
请在问题中包含所有错误消息。
-
您的
sets[i][2]应该是字典,例如["set1", "Experiment", {'color':"C0", 'linestyle': ":"}],之后您可以通过双星号使用字典:data.plot(..., **sets[i][2]。另请参阅What does ** (double star/asterisk) and * (star/asterisk) do for parameters?。
标签: python list matplotlib arguments