python - 在 matplotlib 中以编程方式设置线条属性

标签 python matplotlib attributes

我有一个用于标准化绘图的模块,并且想向它传递一个包含图中每条线的线属性的元组。理想情况下,它会像下面的代码片段一样工作......为了简单起见,我将元组抽象为简单的字符串。这会生成错误,因为它不知道如何解析我的字符串。

import matplotlib.pyplot as plt
x=range(10)
y=range(10)
myStyle = "'b-',linewidth=2.0"
plt.figure(1)
plt.plot(x,y,myStyle)

ValueError: Unrecognized character ' in format string

可以用其他方式实现吗?或者..是否有替代解决方案(类似于 Matlab),我可以为该行分配一个句柄并在这样的循环中访问其行属性?

myStyl = (["color=b","linestyle='-'","linewidth=1.5"],  )
lh = plt.plot(x,y)
for ii in range(len(myStyle[0]))
    plt.setp(lh,myStyle[0][ii])          #<----what goes here 

最佳答案

例如,您可以使用eval:

import matplotlib.pyplot as plt
x=range(10)
y=range(10)
myStyle = "'b-',linewidth=2.0"
plt.figure(1)
eval("plt.plot(x,y,"+myStyle+")")
plt.show()

但是要小心这一点。检查类似 this 的问题详细了解此选项。

关于python - 在 matplotlib 中以编程方式设置线条属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36664593/

相关文章:

python - 将包含列表的 DataFrame 导出到 Excel

python - 如何在 Django 表单选择字段中设置默认值?

python - 每当 matplotlib 中两条线相遇时,如何使用 Python 打印某些内容?

python - 单击图表时显示的勾选标签?

python - Pandas:按时间分组,然后绘制每组的密度

c# - 关于 NUnit TestFixtureTeardown 属性

python - 如何停止将元素放入列表中

python - HTTP 响应 Python 中缺少 cookie

c++ - 覆盖非虚函数

attributes - Chef 属性与数据袋