Esme_
Esme_

Reputation: 1520

Set matplotlib plot characteristics as a variable and reference for each plot

I am plotting a large number of plots and am using consistent marker styles, but more than one style. I am looking for a way to define the different markers I want to use once - and then just call the relevant marker for each plot. Consider

import matplotlib.pyplot as plt
import numpy as np

a = np.random.rand(50)
b = np.random.rand(50)
c = np.random.rand(50)
x = np.random.rand(50)

plt.plot(x,a, 'o', markeredgecolor = 'b', markerfacecolor = 'b')
plt.plot(x,b, 's', markeredgecolor = 'xkcd:amber', markerfacecolor = 'xkcd:amber')
plt.plot(x,c, '<', markeredgecolor = 'r', markerfacecolor = 'r')

I would like a way to have something like

marker1 = {'o', markeredgecolor = 'b', markerfacecolor = 'b'}
marker2 = {'s', markeredgecolor = 'xkcd:amber', markerfacecolor = 'xkcd:amber'}
marker3 = {'<', markeredgecolor = 'r', markerfacecolor = 'r'}
plt.plot(x,a,marker1)
plt.plot(x,b,marker2)
plt.plot(x,c,marker3)

I want to create a series of different markers (or line styles etc.) and call them by a variable name. I do not want to change the global settings. I have more characteristics than just those in the example.

I don't know what this is called, so searching for a solution has been rather unsuccessful - apologies if this has been asked and answered already.

Upvotes: 2

Views: 295

Answers (1)

ilyankou
ilyankou

Reputation: 1329

Put your marker options into a dictionary, and then unpack it, like that:

marker1 = {'marker': 'o', 'markeredgecolor': 'b', 'markerfacecolor': 'b'}
marker2 = {'marker': 's', 'markeredgecolor': 'xkcd:amber', 'markerfacecolor': 'xkcd:amber'}
marker3 = {'marker': '<', 'markeredgecolor': 'r', 'markerfacecolor': 'r'}

plt.plot(x, a, **marker1)
plt.plot(x, b, **marker2)
plt.plot(x, c, **marker3)

Upvotes: 1

Related Questions