1

I have a data file containing three columns of data; x and y, which represent the coordinates of data points to be plotted; and z, which is an integer that is either 0 or 1. When z=0, I wish to plot the data with a solid line, and when z=1 I want to plot with a dashed line.

My reason for wanting to do this is that my data (x,y) represent the solutions to a differential equation, and z encodes the stability of those solutions - z=0 denotes a stable solution and z=1 denotes unstable. The conventional way to represent stability is with solid and dashed lines.

If I use matplotlib to plot (x,y) while ignoring z I get the following plot: enter image description here

for which I used the following code:

import numpy as np
import matplotlib
import matplotlib.pyplot as plt

data = np.genfromtxt('bif.txt')
x, y, z = np.hsplit(data, 3)

font = {'size'   : 18}
matplotlib.rc('font', **font)

plt.plot(x, y, linestyle='-',color='b', linewidth=3.0)

plt.ylabel('$||u||_{2}$', fontsize=24, rotation=0, labelpad = 26)
plt.xlabel('$h$', fontsize=24)
plt.tight_layout()

plt.show()

Now essentially, the above plot should have alternating branches of dashed and solid lines depending on the value of z. Is there a way to do this in Python?

Edit

Here is some sample data:

  0.39464808441470212        7.8834557350383436                         0
  0.39463345228324243        7.8863501873675874                         0
  0.39462029602635296        7.8892429033300591                         0
  0.39460867186795834        7.8921339011986227                         0
  0.39459862088291187        7.8950232017888187                         0
  0.39459016704381161        7.8979108278326740                         0
  0.39458331611262704        7.9007968032693112                         0
  0.39457805541136221        7.9036811524879083                         0
  0.39457435446228745        7.9065638995593925                         0
  0.39457216644773424        7.9094450674925740                         0
  0.39457143040382697        7.9123246775481046                         0
  0.39457207403295502        7.9152027486399952                         1
  0.39457401699683936        7.9180792968496210                         1
  0.39457717453584740        7.9209543350720102                         1
  0.39458146125058674        7.9238278728080029                         1
  0.39458679487846998        7.9266999161098797                         1
  0.39459309990056007        7.9295704676815966                         1
  0.39460031082233094        7.9324395271284001                         1
  0.39460837498580414        7.9353070913444350                         1
  0.39461725478969767        7.9381731550211647                         1
  0.39462692921851011        7.9410377112539612                         1
  0.39463739461045982        7.9439007522198146                         1
  0.39464866462720694        7.9467622698954790                         1
3
  • can you include sample data? Commented Sep 4, 2015 at 11:47
  • Sure, I'll edit my post to include some Commented Sep 4, 2015 at 11:52
  • That's just a small sample - I have about 8000 data points in total Commented Sep 4, 2015 at 11:54

1 Answer 1

4

itertools.groupby is a handy function, although I don't know how much using it can affect perfomance.

For example (written from memory):

from itertools import groupby
...
for g_z, group in groupby(zip(zip(x, y), z), lambda p: p[1]):
    g_x, g_y = [], []
    for i in group:
        g_x.append(i[0][0])
        g_y.append(i[0][1])
    if g_z:
        plt.plot(g_x, g_y, linestyle='-')
    else:
        plt.plot(g_x, g_y, linestyle='--')
...
Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately I get an error using this: ValueError: too many values to unpack
Sorry, I confused how groupby works.. does it work now?
Yes it does! Thanks, you really helped me out here.

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.