user1850133
user1850133

Reputation: 2993

matplotlib set plot properties through function

Some time ago I asked how to set line style through a function taking the plot axes instance as parameter (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)

As shown above one suggested to use ax.lines in the function, but now I would like to know how to set other properties such as marker properties, colors ...

Upvotes: 0

Views: 480

Answers (1)

Molly
Molly

Reputation: 13610

You can find information about the setting the properties of Lines2D here. Marker size and marker color can be set similarly to the line width:

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()

Upvotes: 2

Related Questions