1

So I've been going round in circles on this for the last day, and hoping someone can put me out of my misery.

I have a function f that depends on x and y values and when plotting f against y, gives the following figure.

Now each of the lines are for a value of x [0,1] and I feel there must be a way to colour/contour the plot such that it can easily be identified which line corresponds to what value of x. I've tried numerous searches, but have not found anything that helps in this instance.

The code to reproduce the data that gives my figure is as below. Any help would be great, as I feel I'm missing something glaringly obvious here.

import numpy as np
import matplotlib.pyplot as plt

y = np.linspace(0,100)
x = np.linspace(0,1,11)

f = np.zeros(( len(y), len(x) ))


for j in range(len(x)):
    i=0
    for r in y:
        f[i,j] = (1000 + (-r * 2)) + (1000*(1-x[j]))
        i += 1

 plt.plot(f, y)
3

3 Answers 3

0

I gave a few possibilities in my comment:

Or plotting in 3D, or annotations, or colormapping the lines, or if you have only a very limited number of x values use a different linestyle for each.

Beside that you can create a new axis specifically for x. In the following snippet adapted from your code I put the x value in the top horizontal axis:

import numpy as np
import matplotlib.pyplot as plt

y = np.linspace(0,100)
x = np.linspace(0,1,11)

f = np.zeros(( len(y), len(x) ))


for j in range(len(x)):
    i=0
    for r in y:
        f[i,j] = (1000 + (-r * 2)) + (1000*(1-x[j]))
        i += 1

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twiny()
ax2.set_xticks(x)
ax2.set_xticklabels(["%.3f" % xi for xi in x])
ax1.plot(f, y)

The result is the following:

Double x axis

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the linked examples, they're quite helpful!
0

Since the lines correspond to more or less continuous values of x, I would color the lines according to a colormap. Then use a colorbar to show the mapping of x to colors.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cmx
import matplotlib.colors as colors

y = np.linspace(0,100)
x = np.linspace(0,1,11)

f = np.zeros(( len(y), len(x) ))


for j in range(len(x)):
    i=0
    for r in y:
        f[i,j] = (1000 + (-r * 2)) + (1000*(1-x[j]))
        i += 1


cn  = colors.Normalize(vmin=0, vmax=1)
scalar_map = cmx.ScalarMappable(norm=cn, cmap='jet')
# see plt.colormaps() for many more colormaps

for f_, x_ in zip(f.T, x):
    c = scalar_map.to_rgba(x_)
    plt.plot(f_, y, color=c)

scalar_map.set_array([])  # dunno why we need this. plt.colorbar fails otherwise.
plt.colorbar(scalar_map, label='x')

enter image description here

Comments

0
import numpy as np
import matplotlib.pyplot as plt

y = np.linspace(0, 100)
x = np.linspace(0, 1, 11)
f = np.zeros((len(y), len(x)))

for j in range(len(x)):
    i = 0
    for r in y:
        f[i, j] = (1000 + (-r * 2)) + (1000 * (1 - x[j]))
        i += 1

plt.plot(f, y)
labels = [f[xs, 0] for xs in x]
plt.legend(labels, loc='best')
plt.show()

Just fix the labels

1 Comment

Sorry I probably should have mentioned in the original post that I didn't want to use a legend, as I have another similar function that would be plotted together and the legend gets out of hand quickly. Thanks for the response though.

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.