1

I have a color coded plot. Here is a part of the code:

fig = plt.figure(figsize=(10,10))
color_scheme = plt.get_cmap('cool')
gs = gridspec.GridSpec(1, 1)
ax1 = plt.subplot(gs[0])
gs.update(left=0.15,bottom=0.15,right=0.80,top=0.95)
cax = fig.add_axes([0.80, 0.15, 0.03, 0.80])

im = ax1.scatter(x, y, c=z, edgecolors='black', marker='.', s=40, lw=1, cmap=color_scheme, vmin=0, vmax=10)
cb = fig.colorbar(im, cax=cax)
for t in cb.ax.get_yticklabels(): t.set_fontsize(12)

The problem is that I want to connect the dots with a line, and it doesn't work to use marker='-' and it also doesn't work if I use ax1.plt. How can I do this?
What I actually need is to fit a line to some points and color it the same color as the points (the points I fit to will all have same color)

2 Answers 2

2

Instead of using

ax1.scatter(x, y, ...)

use

ax1.plot(x, y, 'o-', ...) # three dots meaning you can configure markers, linestyle, etc.

This works bacause of 'o-' argument, which is a line plot with markers at every data point.

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

3 Comments

Does this really work? I am not sure if the standard plot can handle colormaps.
As I say plt.plot doesn't handle colormaps
You are right about the colormaps. But how you expect the lines connecting the dots to be colored? If you want a single-colored line, you could plot it using plot and then overlay the colormapped markers using a subsequent call to scatter. Now if you want each line segment to be colored, then you would have to plot each segment independently, or use matplotlib shapes for that (not fast).
1

Plot the same x and y-data separately with a standard ax.plot behind your scatter plot.

ax1.plot(x, y, '-')
im = ax1.scatter(x, y, c=z, edgecolors='black', marker='.', s=40, lw=1, cmap=color_scheme, vmin=0, vmax=10)

This should give you your cmapped scatter plot with the lines behind the scatter-points.

3 Comments

But if I do that, will the line has the same color distribution as the plot? I think it will be just blue
The default is ofcourse blue, and as you point out plot does not handle cmaps afaik. However I thought the main goal was to just connect the points?
What I actually need is to fit a line to some points and color it the same color as the points (the points I fit to will all have same color).

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.