Make a multiline plot from .CSV file in matplotlib -


i've been trying weeks plot 3 sets of (x, y) data on same plot .csv file, , i'm getting nowhere. data excel file have converted .csv file , have used pandas read ipython per following code:

from pandas import dataframe, read_csv import pandas pd # define data location df = read_csv(location) df[['limmag1.3', 'exptime1.3', 'limmag2.0', 'exptime2.0', 'limmag2.5','exptime2.5']][:7] 

my data in following format:

type    mag1    time1   mag2    time2   mag3    time3  m0      8.87    41.11   8.41    41.11   8.16    65.78;  ...  m6     13.95  4392.03  14.41 10395.13  14.66 25988.32 

i'm trying plot time1 vs mag1, time2 vs mag2 , time3 vs mag3, on same plot, instead plots of time.. vs type, eg. code:

df['exptime1.3'].plot() 

i 'exptime1.3' (y-axis) plotted against m0 m6 (x-axis), when want 'exptime1.3' vs 'limmag1.3', x-labels m0 - m6.

  1. how 'exptime..' vs 'limmag..' plots, 3 sets of data on same plot?

  2. how m0 - m6 labels on x-axis 'limmag..' values (also on x-axis)?

since trying askewchan's solutions, did not return plots reasons unknown, i've found can plot of exptimevs limmagusing df['exptime1.3'].plot(),if change dataframe index (df.index) values of x axis (limmag1.3). however, appears mean have convert each desired x-axis dataframe index manually inputing values of desired x-axis make data index. have awful lot of data, , method slow, , can plot 1 set of data @ time, when need plot 3 series each dataset on 1 graph. there way around problem? or can offer reason, , solution, why i got no plots whatsoever solutions offered askewchan?\

in response nordev, have tried first version again, bu no plots produced, not empty figure. each time put in 1 of ax.plotcommands, output of type: [<matplotlib.lines.line2d @ 0xb5187b8>], when enter command plt.show()nothing happens. when enter plt.show()after loop in askewchan's second solution, error saying attributeerror: 'function' object has no attribute 'show'

i have done bit more fiddling original code , can plot of exptime1.3vs limmag1.3 code df['exptime1.3'][:7].plot(),by making index same x axis (limmag1.3), can't other 2 sets of data on same plot. appreciate further suggestions may have. i'm using ipython 0.11.0 via anaconda 1.5.0 (64bit) , spyder on windows 7 (64bit), python version 2.7.4.

if have understood correctly, both question previous 1 on same subject, following should basic solutions customize needs.

several subplots:

note solution output many subplots there spectral classes (m0, m1, ...) vertically on same figure. if wish save plot of each spectral class in separate figure, code needs modifications.

import pandas pd pandas import dataframe, read_csv import numpy np import matplotlib.pyplot plt  # here put code read csv-file dataframe df  plt.figure(figsize=(7,5)) # set size of figure, customize more subplots  in range(len(df)):     xs = np.array(df[df.columns[0::2]])[i] # use values odd numbered columns x-values     ys = np.array(df[df.columns[1::2]])[i] # use values numbered columns y-values     plt.subplot(len(df), 1, i+1)     plt.plot(xs, ys, marker='o') # plot circle markers line connecting points     j in range(len(xs)):         plt.annotate(df.columns[0::2][j][-3:] + '"', # annotate every plotted point last 3 characters of column-label                      xy = (xs[j],ys[j]),                      xytext = (0, 5),                      textcoords = 'offset points',                      va = 'bottom',                      ha = 'center',                      clip_on = true)     plt.title('spectral class ' + df.index[i])     plt.xlabel('limiting magnitude')     plt.ylabel('exposure time')     plt.grid(alpha=0.4)  plt.tight_layout() plt.show() 

enter image description here

all in same axes, grouped rows (m0, m1, ...)

here solution different spectral classes plotted in same axes legend identifying different classes. plt.yscale('log') optional, seeing how values span such great range, recommended.

import pandas pd pandas import dataframe, read_csv import numpy np import matplotlib.pyplot plt  # here put code read csv-file dataframe df  in range(len(df)):     xs = np.array(df[df.columns[0::2]])[i] # use values odd numbered columns x-values     ys = np.array(df[df.columns[1::2]])[i] # use values numbered columns y-values     plt.plot(xs, ys, marker='o', label=df.index[i])     j in range(len(xs)):         plt.annotate(df.columns[0::2][j][-3:] + '"', # annotate every plotted point last 3 characters of column-label                      xy = (xs[j],ys[j]),                      xytext = (0, 6),                      textcoords = 'offset points',                      va = 'bottom',                      ha = 'center',                      rotation = 90,                      clip_on = true)  plt.title('spectral classes') plt.xlabel('limiting magnitude') plt.ylabel('exposure time')  plt.grid(alpha=0.4)     plt.yscale('log') plt.legend(loc='best', title='spectral classes') plt.show() 

enter image description here

all in same axes, grouped columns (1.3", 2.0", 2.5")

a third solution shown below, data grouped series (columns 1.3", 2.0", 2.5") rather spectral class (m0, m1, ...). example similar @askewchan's solution. 1 difference y-axis here logarithmic axis, making lines pretty parallel.

import pandas pd pandas import dataframe, read_csv import numpy np import matplotlib.pyplot plt  # here put code read csv-file dataframe df  xs = np.array(df[df.columns[0::2]]) # use values odd numbered columns x-values ys = np.array(df[df.columns[1::2]]) # use values numbered columns y-values  in range(df.shape[1]/2):      plt.plot(xs[:,i], ys[:,i], marker='o', label=df.columns[0::2][i][-3:]+'"')      j in range(len(xs[:,i])):         plt.annotate(df.index[j], # annotate every plotted point spectral class                      xy = (xs[:,i][j],ys[:,i][j]),                      xytext = (0, -6),                      textcoords = 'offset points',                      va = 'top',                      ha = 'center',                      clip_on = true)  plt.title('spectral classes') plt.xlabel('limiting magnitude') plt.ylabel('exposure time')  plt.grid(alpha=0.4)     plt.yscale('log') plt.legend(loc='best', title='series') plt.show() 

enter image description here


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -

keyboard - C++ GetAsyncKeyState alternative -