matplotlib通过函数设置绘图属性

时间:2014-04-01 14:07:45

标签: matplotlib

前段时间我问过如何通过将绘图轴实例作为参数的函数来设置线条样式 (matplotlib set all plots linewidth in a figure at once)。

import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10))
ax.plot(2 * range(10))

solution suggested:
def bulk_lw_edjuster(ax, lw = 5)
    for ln in ax.lines:
    ln.set_linewidth(lw)

如上图所示,建议在函数中使用ax.lines,但现在我想知道如何设置其他属性,如标记属性,颜色......

1 个答案:

答案 0 :(得分:2)

您可以找到有关设置Lines2D here.属性的信息。标记大小和标记颜色可以与线宽类似地设置:

import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10))
ax.plot(2 * range(10))

#solution suggested:
def bulk_lw_edjuster(ax, lw = 5, markersize  = 5, markerfacecolor  = 'r'):
    for ln in ax.lines:
        ln.set_linewidth(lw)
        ln.set_markersize(markersize) # set marker size
        ln.set_markerfacecolor(markerfacecolor) # set marker color

# Change the plot properties.        
bulk_lw_edjuster(ax, lw =10, markersize = 10, markerfacecolor = 'm')
plt.show()