Plotting an array of functions in different colors

In summary, when plotting a list of functions in a literal array, they will automatically be plotted in different colors. However, if the list of functions is obtained from another user-defined function, they will be plotted in a single color. To apply different colors to an array of functions constructed in a function, the functions must be surrounded by Evaluate[ ] when plotting. This issue is not directly addressed in existing discussions and can be solved by using Evaluate[ ].
  • #1
Swamp Thing
Insights Author
908
572
TL;DR Summary
A user-defined Mathematica function returns a list (array) of functions. How to plot them in different colors?
If we plot a list of functions in a literal array, they get plotted automatically in different colors, for example:
Plot [{x,x*x,-x},{x,0,10}]

But if we get the list of functions from another user-defined function, they get plotted in a single color:

fnY[x_]:={x, x*x, -x} ... OR ... fnY[x_] := List[x, x*x, -x]

Plot[fnY[x],{x,0,10}]

... even though fnY[x] evaluates to the same array as the literal array in the first example.

Why? How can we apply different colors to an array of functions constructed in a function?
 
Physics news on Phys.org
  • #3
Those discussions don't quite address the particular issue I had. After some more searching it turned out that it works if you put a Evaluate[ ] around the set of functions you want to plot.
 

1. How can I plot multiple functions in different colors on the same graph?

To plot an array of functions in different colors, you can use a plotting library such as Matplotlib or Plotly. These libraries allow you to specify the color for each function in the plot, either by specifying a different color for each function or by using a color map.

2. Can I customize the colors of each function in the plot?

Yes, most plotting libraries offer a variety of options for customizing the colors of each function in the plot. This can include specifying specific colors, using a color map, or even creating a custom color palette.

3. How do I add a legend to my plot to differentiate between the different functions?

To add a legend to your plot, you can use the "legend" function in your chosen plotting library. This function allows you to specify the labels for each function in the plot and will automatically add a legend to your graph.

4. Can I change the line style for each function in the plot?

Yes, most plotting libraries allow you to customize the line style for each function in the plot. This can include changing the line color, thickness, and type (e.g. solid, dashed, etc.).

5. Is it possible to plot both functions and data points on the same graph?

Yes, you can plot both functions and data points on the same graph by using the "plot" and "scatter" functions in your chosen plotting library. This allows you to visualize the relationship between the functions and the data points on the same plot.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
845
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Introductory Physics Homework Help
Replies
8
Views
577
  • Special and General Relativity
5
Replies
146
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
Back
Top