2

First, I defined a line through matplotlib.lines.Line2D:

import matplotlib.lines as mlines
newLine = mlines.Line2D([], [], color="blue", linestyle='-',linewidth=2)

(For simplicity) In the same script file, I would like to call these line settings whenever possible through: matplotlib.pyplot.plot without repeating the kwargs, such as:

import matplotlib.pyplot as plt
import numpy as np

dataXaxis=np.arange(0,10)
dataYaxis=np.arange(5,15)

plt.plot(dataXaxis, dataYaxis, <CALL newLine PROPERTIES>)

instead of:

plt.plot(dataXaxis, dataYaxis, color="blue", linestyle='-',linewidth=2)

Context:

I might be confused with the above functionalities. Yet I want to use predefined line style settings associated to a predefined concept. For instance, I want to use a particular line style whenever I plot 'input vs. time' relation, and another particular style to plot 'output vs. time' relation.

2 Answers 2

1

You could just define a dictionary with the keyword arguments that you want to use each time, e.g.,

newlinestyle = {'color': 'blue', 'linestyle': '-', 'linewidth': 2}

plt.plot(dataXaxis, dataYaxis, **newlinestyle)
Sign up to request clarification or add additional context in comments.

Comments

0

Based on Matt Pitkin's answer, I tried to generalize the approach as a MWE:

import numpy as np
import matplotlib.pyplot as plt

newlinestyles = {'dict1': {'color': 'blue', 'linestyle': '-', 'linewidth': 2},
                 'dict2': {'color': 'red' , 'linestyle': '-', 'linewidth': 2} }

dataXaxis=np.arange(0,10)
dataYaxis=np.arange(5,15)

plt.plot(dataXaxis, dataYaxis, **newlinestyles['dict1'])
plt.show()

plt.plot(dataXaxis, dataYaxis, **newlinestyles['dict2'])

plt.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.