How to use LineCollection with set of strings in segments array - python

I am trying to plot a line with three different colors based on other conditions:
I have a dataframe x_week where the column ['Year-Week'] contains a string of year and week in the form '%Y-w%U'
The column x_week['#ops'] are float numbers
The limits where I want to change color of the line are stored in a dictionary named week that also contains strings in the format '%Y-w%U'
I am using LineCollection, the problem is that it requieres that the string element of the array segment being float, I have already tried date2num but I want to mantain the format '%Y-w%U' for the x-axis
(I already look into here multicolored line with strings linecolllection )
x = x_week['Year-Week']
y = x_week['ops']
# select how to color
color = []
for i in range(3):
color.append('#%06X' % randint(0, 0xFFFFFF))
cmap = ListedColormap(color)
norm = BoundaryNorm([min(x_week['Year-Week']),week[1],week[2],week[3],max(x_week['Year-Week'])], cmap.N)
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
# make line collection
lc = LineCollection(segments, cmap = cmap, norm = norm)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-279-6f4c3a13d43e> in <module>
18
19 # make line collection
---> 20 lc = LineCollection(segments, cmap = cmap, norm = norm)
~\Anaconda3\lib\site-packages\matplotlib\collections.py in __init__(self, segments, linewidths, colors, antialiaseds, linestyles, offsets, transOffset, norm, cmap, pickradius, zorder, facecolors, **kwargs)
1331 **kwargs)
1332
-> 1333 self.set_segments(segments)
1334
1335 def set_segments(self, segments):
~\Anaconda3\lib\site-packages\matplotlib\collections.py in set_segments(self, segments)
1340 for seg in segments:
1341 if not isinstance(seg, np.ma.MaskedArray):
-> 1342 seg = np.asarray(seg, float)
1343 _segments.append(seg)
1344
~\Anaconda3\lib\site-packages\numpy\core\numeric.py in asarray(a, dtype, order)
536
537 """
--> 538 return array(a, dtype, copy=False, order=order)
539
540
ValueError: could not convert string to float: '2019-w27'

Related

ValueError: RGBA values should be within 0-1 range when plotting scatter plot

I am attempting to generate a scatter plot to show data before and after the PCA transform, similar to this tutorial.
To do this, I am running the following code:
fig, axes = plt.subplots(1,2)
axes[0].scatter(X.iloc[:,0], X.iloc[:,1], c=y)
axes[0].set_xlabel('x1')
axes[0].set_ylabel('x2')
axes[0].set_title('Before PCA')
axes[1].scatter(X_new[:,0], X_new[:,1], c=y)
axes[1].set_xlabel('PC1')
axes[1].set_ylabel('PC2')
axes[1].set_title('After PCA')
plt.show()
Which is causing this error to appear:
ValueError: RGBA values should be within 0-1 range
X is the preprocessed matrix of features, which contains 196 samples and 59 features. Whereas y is the dependent variable and contains two classes [0, 1].
Here is the full error message:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-109-2c4f74ddce3f> in <module>
1 fig, axes = plt.subplots(1,2)
----> 2 axes[0].scatter(X.iloc[:,0], X.iloc[:,1], c=y)
3 axes[0].set_xlabel('x1')
4 axes[0].set_ylabel('x2')
5 axes[0].set_title('Before PCA')
~/anaconda3/lib/python3.7/site-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs)
1597 def inner(ax, *args, data=None, **kwargs):
1598 if data is None:
-> 1599 return func(ax, *map(sanitize_sequence, args), **kwargs)
1600
1601 bound = new_sig.bind(ax, *args, **kwargs)
~/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, plotnonfinite, **kwargs)
4495 offsets=offsets,
4496 transOffset=kwargs.pop('transform', self.transData),
-> 4497 alpha=alpha
4498 )
4499 collection.set_transform(mtransforms.IdentityTransform())
~/anaconda3/lib/python3.7/site-packages/matplotlib/collections.py in __init__(self, paths, sizes, **kwargs)
881 """
882
--> 883 Collection.__init__(self, **kwargs)
884 self.set_paths(paths)
885 self.set_sizes(sizes)
~/anaconda3/lib/python3.7/site-packages/matplotlib/collections.py in __init__(self, edgecolors, facecolors, linewidths, linestyles, capstyle, joinstyle, antialiaseds, offsets, transOffset, norm, cmap, pickradius, hatch, urls, offset_position, zorder, **kwargs)
125
126 self._hatch_color = mcolors.to_rgba(mpl.rcParams['hatch.color'])
--> 127 self.set_facecolor(facecolors)
128 self.set_edgecolor(edgecolors)
129 self.set_linewidth(linewidths)
~/anaconda3/lib/python3.7/site-packages/matplotlib/collections.py in set_facecolor(self, c)
676 """
677 self._original_facecolor = c
--> 678 self._set_facecolor(c)
679
680 def get_facecolor(self):
~/anaconda3/lib/python3.7/site-packages/matplotlib/collections.py in _set_facecolor(self, c)
659 except AttributeError:
660 pass
--> 661 self._facecolors = mcolors.to_rgba_array(c, self._alpha)
662 self.stale = True
663
~/anaconda3/lib/python3.7/site-packages/matplotlib/colors.py in to_rgba_array(c, alpha)
277 result[mask] = 0
278 if np.any((result < 0) | (result > 1)):
--> 279 raise ValueError("RGBA values should be within 0-1 range")
280 return result
281 # Handle single values.
ValueError: RGBA values should be within 0-1 range
I am unsure what is causing this error and would appreciate help in figuring this out. Thanks!
The c= parameter of ax.scatter can be given in several ways:
A scalar or sequence of n numbers to be mapped to colors using cmap and norm. So a single number, or a list-like 1D sequence of numbers.
A 2D array in which the rows are RGB or RGBA. E.g. something like [[1,0,0], [0,0,1]]. All these values need to be between 0 and 1. Moreover, there should be either 3 (for RGB) or 4 (for RGBA) values per entry.
A sequence of colors of length n. E.g. ["red", "#B789C0", "turquoise"]
A single color format string. E.g. "cornflowerblue".
Now, when an array of numbers is given, to be able to distinguish between the first and the second case, matplotlib just looks at the array dimension. If it is 1D, matplotlib assumes the first case. For 2D, it assumes the second case. Note that also an Nx1 or an 1xN array is considered 2D. You can use np.squeeze() to "squeeze out" the dummy second dimension.

How to plot a windrose when the wind direction is a categorical value

From Dataset Australia Rainfall, I'm trying to predict RainTomorrow. Here is my code given below :
Downloading dataset directly from Kaggle using opendatasets library
import opendatasets as od
dataset_url = 'https://www.kaggle.com/jsphyg/weather-dataset-rattle-package'
od.download(dataset_url)
Importing necessary libraries
import os
import pandas as pd
import numpy as np
import plotly.express as px
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
sns.set_style('darkgrid')
matplotlib.rcParams['font.size'] = 14
matplotlib.rcParams['figure.figsize'] = (10,6)
matplotlib.rcParams['figure.facecolor'] = '#00000000'
Loading Dataset
data_dir = './weather-dataset-rattle-package'
os.listdir(data_dir)
train_csv = data_dir + '/weatherAUS.csv'
raw_df = pd.read_csv(train_csv)
Explore WindGustDir variable
print('WindGustDir contains', len(raw_df['WindGustDir'].unique()), 'labels')
raw_df['WindGustDir'].unique()
raw_df.WindGustDir.value_counts()
pd.get_dummies(raw_df.WindGustDir, drop_first=True, dummy_na=True).head()
pd.get_dummies(raw_df.WindGustDir, drop_first=True, dummy_na=True).sum(axis=0)
Plotting Windrose
from windrose import WindroseAxes
ax = WindroseAxes.from_ax()
ax.bar(raw_df.WindGustDir, raw_df.Rainfall, normed=True, opening=0.8,
edgecolor='white')
ax.set_legend()
I am unable to figure out which columns should use with WindGustDir or if their is any other option of compare RainTomorrow and WindGustDir .
Error Message
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
e:\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py in _wrapfunc(obj, method, *args, **kwds)
57 try:
---> 58 return bound(*args, **kwds)
59 except TypeError:
TypeError: '<' not supported between instances of 'float' and 'str'
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-253-1a1f0fa6bf7a> in <module>
1 ax = WindroseAxes.from_ax()
----> 2 ax.bar(direction=df.WindGustDir, var=df.Rainfall, normed=True, opening=0.8, edgecolor='white')
3 ax.set_legend()
e:\Anaconda3\lib\site-packages\windrose\windrose.py in bar(self, direction, var, **kwargs)
547 """
548
--> 549 bins, nbins, nsector, colors, angles, kwargs = self._init_plot(
550 direction, var, **kwargs
551 )
e:\Anaconda3\lib\site-packages\windrose\windrose.py in _init_plot(self, direction, var, **kwargs)
359
360 # Set the global information dictionnary
--> 361 self._info["dir"], self._info["bins"], self._info["table"] = histogram(
362 direction, var, bins, nsector, normed, blowto
363 )
e:\Anaconda3\lib\site-packages\windrose\windrose.py in histogram(direction, var, bins, nsector, normed, blowto)
746 direction[direction >= 360.] = direction[direction >= 360.] - 360
747
--> 748 table = histogram2d(x=var, y=direction, bins=[var_bins, dir_bins], normed=False)[0]
749 # add the last value to the first to have the table of North winds
750 table[:, 0] = table[:, 0] + table[:, -1]
<__array_function__ internals> in histogram2d(*args, **kwargs)
e:\Anaconda3\lib\site-packages\numpy\lib\twodim_base.py in histogram2d(x, y, bins, range, normed, weights, density)
742 xedges = yedges = asarray(bins)
743 bins = [xedges, yedges]
--> 744 hist, edges = histogramdd([x, y], bins, range, normed, weights, density)
745 return hist, edges[0], edges[1]
746
<__array_function__ internals> in histogramdd(*args, **kwargs)
e:\Anaconda3\lib\site-packages\numpy\lib\histograms.py in histogramdd(sample, bins, range, normed, weights, density)
1071
1072 # Compute the bin number each sample falls into.
-> 1073 Ncount = tuple(
1074 # avoid np.digitize to work around gh-11022
1075 np.searchsorted(edges[i], sample[:, i], side='right')
e:\Anaconda3\lib\site-packages\numpy\lib\histograms.py in <genexpr>(.0)
1073 Ncount = tuple(
1074 # avoid np.digitize to work around gh-11022
-> 1075 np.searchsorted(edges[i], sample[:, i], side='right')
1076 for i in _range(D)
1077 )
<__array_function__ internals> in searchsorted(*args, **kwargs)
e:\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py in searchsorted(a, v, side, sorter)
1346
1347 """
-> 1348 return _wrapfunc(a, 'searchsorted', v, side=side, sorter=sorter)
1349
1350
e:\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py in _wrapfunc(obj, method, *args, **kwds)
65 # Call _wrapit from within the except clause to ensure a potential
66 # exception has a traceback chain.
---> 67 return _wrapit(obj, method, *args, **kwds)
68
69
e:\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py in _wrapit(obj, method, *args, **kwds)
42 except AttributeError:
43 wrap = None
---> 44 result = getattr(asarray(obj), method)(*args, **kwds)
45 if wrap:
46 if not isinstance(result, mu.ndarray):
TypeError: '<' not supported between instances of 'float' and 'str'
It seems that the direction parameter must be numeric.
Create a dict where each key is a each direction in 'WindGustDir' and the corresponding value is a float in degrees.
.map the dict to df.WindGustDir and plot
Alternatively, create and plot a new column
df.insert(loc=8, column='WindGustDirDeg', value=df.WindGustDir.map(wind_dir_deg))
import pandas as pd
from windrose import WindroseAxes
import numpy as np
# load the downloaded data and dropna
df = pd.read_csv('weatherAUS/weatherAUS.csv').dropna(subset=['WindGustDir'])
# create a dict for WindGustDir to numeric values
wind_dir = ['E', 'ENE', 'NE', 'NNE', 'N', 'NNE', 'NW', 'WNW', 'W', 'WSW', 'SW', 'SSW', 'S', 'SSE', 'SE', 'ESE']
degrees = np.arange(0, 360, 22.5)
wind_dir_deg = dict((zip(wind_dir, degrees)))
# plot and map WindGustDir to the dict
ax = WindroseAxes.from_ax()
ax.bar(direction=df.WindGustDir.map(wind_dir_deg), var=df.Rainfall, normed=True, opening=0.8, edgecolor='white')
ax.set_legend()

hist2D masked array - ValueError: cannot convert float NaN to integer

Something about the way I have masked zero values in my array is causing an error when doing his2d. I try:
x = ma.masked_array([0.17478785046728973, 0.21854713940370668, 0.7354304690743048,
1.1052309124767226, 0.3450562200956937, 0])
x[5] = ma.masked_all(1)
y = ma.masked_array([0.17478785046728973, 0.21854713940370668, 0.7354304690743048,
1.1052309124767226, 0.3450562200956937, 0])
plt.hist2d(x, y, bins = 10)
plt.show()
I think it doesn't like something to do with the decimals because this is the error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-82-38bb844ac43e> in <module>()
9 1.1052309124767226, 0.3450562200956937, 0])
10
---> 11 plt.hist2d(c, d, bins = 10)
12 plt.show()
/Users/eebp/anaconda/envs/Python2/lib/python2.7/site-packages/matplotlib/pyplot.pyc in hist2d(x, y, bins, range, normed, weights, cmin, cmax, hold, data, **kwargs)
2983 ret = ax.hist2d(x, y, bins=bins, range=range, normed=normed,
2984 weights=weights, cmin=cmin, cmax=cmax, data=data,
-> 2985 **kwargs)
2986 finally:
2987 ax.hold(washold)
/Users/eebp/anaconda/envs/Python2/lib/python2.7/site-packages/matplotlib/__init__.pyc in inner(ax, *args, **kwargs)
1817 warnings.warn(msg % (label_namer, func.__name__),
1818 RuntimeWarning, stacklevel=2)
-> 1819 return func(ax, *args, **kwargs)
1820 pre_doc = inner.__doc__
1821 if pre_doc is None:
/Users/eebp/anaconda/envs/Python2/lib/python2.7/site-packages/matplotlib/axes/_axes.pyc in hist2d(self, x, y, bins, range, normed, weights, cmin, cmax, **kwargs)
6300 range = __builtins__["range"]
6301 h, xedges, yedges = np.histogram2d(x, y, bins=bins, range=bin_range,
-> 6302 normed=normed, weights=weights)
6303
6304 if cmin is not None:
/Users/eebp/anaconda/envs/Python2/lib/python2.7/site-packages/numpy/lib/twodim_base.pyc in histogram2d(x, y, bins, range, normed, weights)
712 xedges = yedges = asarray(bins, float)
713 bins = [xedges, yedges]
--> 714 hist, edges = histogramdd([x, y], bins, range, normed, weights)
715 return hist, edges[0], edges[1]
716
/Users/eebp/anaconda/envs/Python2/lib/python2.7/site-packages/numpy/lib/function_base.pyc in histogramdd(sample, bins, range, normed, weights)
800 mindiff = dedges[i].min()
801 if not np.isinf(mindiff):
--> 802 decimal = int(-log10(mindiff)) + 6
803 # Find which points are on the rightmost edge.
804 not_smaller_than_edge = (sample[:, i] >= edges[i][-1])
ValueError: cannot convert float NaN to integer
if I plot the array without my masked value against itself: plt.hist2d(y, y, bins = 10) then it works. Am I using the wrong method to mask? I was originally using that syntax to mask a 2D slice of a 3D array, so I am wondering if I am creating an array within an array unnecessarily.

How to pass date array to pcolor plot?

I have matrix-data where one axis relates to dates. However, I'm having problems passing this data on as an axis to pcolor. My dummy data is as follows:
In [219]: X = [datetime.date.today() + datetime.timedelta(days=i) for i in range(4)]
In [220]: Y = arange(5)
In [221]: Z = arange(4*5).reshape(4, 5)
The naive attemp pcolor(Y, X, Z) fails because pcolor does not like to get a list object:
In [222]: pcolor(Y, X, Z)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-222-1ece18b4bc13> in <module>()
----> 1 pcolor(Y, X, Z)
/export/data/home/gholl/venv/gerrit/lib/python3.4/site-packages/matplotlib/pyplot.py in pcolor(*args, **kwargs)
2926 ax.hold(hold)
2927 try:
-> 2928 ret = ax.pcolor(*args, **kwargs)
2929 draw_if_interactive()
2930 finally:
/export/data/home/gholl/venv/gerrit/lib/python3.4/site-packages/matplotlib/axes.py in pcolor(self, *args, **kwargs)
7545 shading = kwargs.pop('shading', 'flat')
7546
-> 7547 X, Y, C = self._pcolorargs('pcolor', *args, allmatch=False)
7548 Ny, Nx = X.shape
7549
/export/data/home/gholl/venv/gerrit/lib/python3.4/site-packages/matplotlib/axes.py in _pcolorargs(funcname, *args, **kw)
7357
7358 Nx = X.shape[-1]
-> 7359 Ny = Y.shape[0]
7360 if len(X.shape) != 2 or X.shape[0] == 1:
7361 x = X.reshape(1, Nx)
AttributeError: 'list' object has no attribute 'shape'
Converting it to an array containing datetime.date fails with TypeError: float() argument must be a string or a number, not 'datetime.date':
In [223]: pcolor(Y, numpy.array(X), Z)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-223-a00423a6d479> in <module>()
----> 1 pcolor(Y, numpy.array(X), Z)
/export/data/home/gholl/venv/gerrit/lib/python3.4/site-packages/matplotlib/pyplot.py in pcolor(*args, **kwargs)
2926 ax.hold(hold)
2927 try:
-> 2928 ret = ax.pcolor(*args, **kwargs)
2929 draw_if_interactive()
2930 finally:
/export/data/home/gholl/venv/gerrit/lib/python3.4/site-packages/matplotlib/axes.py in pcolor(self, *args, **kwargs)
7606 kwargs['antialiaseds'] = False
7607
-> 7608 collection = mcoll.PolyCollection(verts, **kwargs)
7609
7610 collection.set_alpha(alpha)
/export/data/home/gholl/venv/gerrit/lib/python3.4/site-packages/matplotlib/collections.py in __init__(self, verts, sizes, closed, **kwargs)
743 Collection.__init__(self, **kwargs)
744 self._sizes = sizes
--> 745 self.set_verts(verts, closed)
746
747 def set_verts(self, verts, closed=True):
/export/data/home/gholl/venv/gerrit/lib/python3.4/site-packages/matplotlib/collections.py in set_verts(self, verts, closed)
763 codes[0] = mpath.Path.MOVETO
764 codes[-1] = mpath.Path.CLOSEPOLY
--> 765 self._paths.append(mpath.Path(xy, codes))
766 else:
767 self._paths.append(mpath.Path(xy))
/export/data/home/gholl/venv/gerrit/lib/python3.4/site-packages/matplotlib/path.py in __init__(self, vertices, codes, _interpolation_steps, closed, readonly)
131 vertices = vertices.astype(np.float_).filled(np.nan)
132 else:
--> 133 vertices = np.asarray(vertices, np.float_)
134
135 if codes is not None:
/export/data/home/gholl/venv/gerrit/lib/python3.4/site-packages/numpy/core/numeric.py in asarray(a, dtype, order)
460
461 """
--> 462 return array(a, dtype, copy=False, order=order)
463
464 def asanyarray(a, dtype=None, order=None):
TypeError: float() argument must be a string or a number, not 'datetime.date'
Finally, converting it to a proper numpy.datetime64 object does not resolve the situation either, failing with Invalid type promotion:
In [224]: pcolor(Y, numpy.array(X, dtype="datetime64[D]"), Z)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-224-0ac06cfafa35> in <module>()
----> 1 pcolor(Y, numpy.array(X, dtype="datetime64[D]"), Z)
/export/data/home/gholl/venv/gerrit/lib/python3.4/site-packages/matplotlib/pyplot.py in pcolor(*args, **kwargs)
2926 ax.hold(hold)
2927 try:
-> 2928 ret = ax.pcolor(*args, **kwargs)
2929 draw_if_interactive()
2930 finally:
/export/data/home/gholl/venv/gerrit/lib/python3.4/site-packages/matplotlib/axes.py in pcolor(self, *args, **kwargs)
7577 X4[:, newaxis], Y4[:, newaxis],
7578 X1[:, newaxis], Y1[:, newaxis]),
-> 7579 axis=1)
7580 verts = xy.reshape((npoly, 5, 2))
7581
TypeError: invalid type promotion
What would be the correct way to proceed here? In the
Note that the answer to the question plotting date data with pcolor uses scatter, not pcolor, and therefore does not help in my situation.
Matplotlib uses simple floats to represent datetimes. Hence you have to convert them first and then tell the axis that is has to format the labels as dates. Matplotlib provides the function date2num for that:
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
import numpy as np
# Your original data (with adapted sizes)
x = [datetime.date.today() + datetime.timedelta(days=i) for i in range(4)]
y = np.arange(5)
z = np.arange(3*4).reshape(3, 4).T
# Convert to numbers
x = mdates.date2num(x)
# Create the figure
fig, ax = plt.subplots(1,1)
plt.pcolor(x,y,z)
# Setup the DateFormatter for the x axis
date_format = mdates.DateFormatter('%D')
ax.xaxis.set_major_formatter(date_format)
# Rotates the labels to fit
fig.autofmt_xdate()
plt.show()
Some other remarks:
For pcolor the x and y vectors represent the corner points of the tiles. So they need to be 1 element longer than the data.
The documentation provides a nice overview of how to handle dates in matplotlib.
Result:

Python, Key Error: 1

The goal of my code is to sort through the data and select only the Visual band or "Vis." band data. From that I eliminated all values that were upper and lower limits to clean up the graph. Finally I wanted to remove all the data that was not a part of the outbursts or decays. My filtering of Vis. band and the upper/lower limit data seems to work fine, but when I try to remove data that had a small slope it shows key error:1, I don't have enough reputation to post an image so I included a link to the plot. The plot shows data after filtering the vis band and upper/lower limits.
def timeplot():
import pandas as pd
import matplotlib.pyplot as plt
import jdcal as jd
import math
#Getting input from user as to start and end dates for the data
(miny,minm,mind) = input("Enter the start date for data in the format (yyyy,mm,dd) ex. (2000,01,01):")
(maxy,maxm,maxd) = input("Enter the end date for data in the format (yyyy,mm,dd) ex. (2000,01,01):")
#Calculating modified julian dates from the gregorian date input
(x,Amin)=jd.gcal2jd(miny,minm,mind)
(y,Amax)=jd.gcal2jd(maxy,maxm,maxd)
#Creating a table with the numbers corresponding to their month
Month = {1:'Jan', 2:'Feb', 3:'Mar', 4:'Apr', 5:'May', 6:'Jun', 7:'Jul', 8:'Aug', 9:'Sep', 10:'Oct', 11:'Nov', 12:'Dec'}
#Read in data file
pd.set_option('html', False)
pd.set_option('max_columns', 30)
pd.set_option('max_rows', 2000)
data1 = pd.read_csv("50yrdata.csv")
data1['ulflag']=1
#Deal with any bad columns
data1_limit = data1.JD * 0
ii=0
for mag in data1.Magnitude:
if mag[0] == '<':
data1.ulflag[ii]=0
data1.Magnitude[ii] = mag[1:]
data1_limit[ii] = 1
if mag[0] == '>':
data1.ulflag[ii]=0
data1.Magnitude[ii] = mag[1:]
data1_limit[ii] = -1
ii +=1
#The data set has Vis, V, I, R, B, TG, TB, TR, CV bands
#Selecting data only in the visual band with no upper or lower limits in
#magnitude
#Converting Julian Date to Modified Julian Date
data1.JD=data1.JD-2400000.5
data1.index=data1.ulflag
data1=data1.ix[1,['JD','Magnitude','Band']]
data1.index=data1.Band
tdata=data1.ix['Vis.',['JD','Magnitude']]
#Changing all of the values from Magnitude from string to float
tdata=tdata.astype(float)
#Adding on columns to make computations easier
tdata['sflag']=0
tdata['slope']=0.000
tdata['aslope']=0.000
tdata['A']=0.000
tdata['B']=0.000
#Finding max and min values of our MJD,
Max=Amax
Min=Amin
#We split the data into N graphs where N is the number of years the data spans
N=(int((Max-Min)/365))
#Finding slope of the curve
#Attempt to filter the data using
#1. A positive slope greater than a certain threshold = outburst
#2. A negtaive slope smaller than a certain threshold = decay
#3. The absolute value of the slope is smaller than a certain threshold = quiescence
length=len(tdata.JD)-1
tdata.A[length]=0
tdata.B[length]=1
for i in range(length):
tdata.A[i] = tdata.Magnitude[i+1]-tdata.Magnitude[i]
for i in range(length):
tdata.B[i] = tdata.JD[i+1]-tdata.JD[i]
for i in range(length+1):
tdata.slope[i] = tdata.A[i]/tdata.B[i]
tdata.aslope=abs(tdata.slope)
for i in range(length):
if tdata.aslope[i] > 1:
tdata.sflag = 1
if tdata.aslope[i] < 1:
tdata.sflag = 0
i += 1
#filtering out all the data that has a slope less than a certain threshold
tdata.index = tdata.sflag
tdata=tdata.astype(float)
tdata=tdata.ix[1,['JD','Magnitude']]
#Plot selected data
fig ,axs = plt.subplots(N,1)
fig.subplots_adjust(hspace = .5)
#Due to data set being so large, make multiple sub plots instead of one large plot
#Magnitude axis needs to be flipped to see when the star has outbursts
#When setting the limits of our subplots, we extend them by a small value in
#order to make the data easier to read. The large value being added and subtracted
#of 365 causes the graph to cover approximately one year in data.
axs = axs.ravel()
for i in range(N):
axs[i].scatter(tdata.JD, tdata.Magnitude)
axs[i].invert_yaxis()
axs[i].set_xlim([Min+(365*(i-1))-5, Max+5-(365*(N-i))])
A=str(miny+i)
B=Month[minm]
C=str(mind)
axs[i].set_title('A Year of data starting from ' + A + ',' + B + ',' +C)
#Setting title and axis, I was unable to set a shared x and y axis title
#between the subplots, when I attempted to do this it would create another
#plot overlapping the 4 subplots making it difficult to see the values
fig.suptitle('SS Cyg Data', fontsize = 20)
fig.text(0.5, 0.04, 'Modified Julian Date', ha='center', va='center')
fig.text(0.04, 0.5, 'Magnitude', ha='center', va='center', rotation='vertical')
plt.show()
timeplot()
The full Traceback to the error is
KeyError Traceback (most recent call last)
C:\Users\Kenny\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.2.0.1610.win-x86_64\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
195 else:
196 filename = fname
--> 197 exec compile(scripttext, filename, 'exec') in glob, loc
198 else:
199 def execfile(fname, *where):
C:\Users\Kenny\Dropbox\499\timeplot.py in <module>()
136 plt.show()
137
--> 138 timeplot()
C:\Users\Kenny\Dropbox\499\timeplot.py in timeplot()
102 tdata.index = tdata.sflag
103 tdata=tdata.astype(float)
--> 104 tdata=tdata.ix[1,['JD','Magnitude']]
105
106 #Plot selected data
E:\Enthought\Canopy\User\lib\site-packages\pandas\core\indexing.pyc in __getitem__(self, key)
45 pass
46
---> 47 return self._getitem_tuple(key)
48 else:
49 return self._getitem_axis(key, axis=0)
E:\Enthought\Canopy\User\lib\site-packages\pandas\core\indexing.pyc in _getitem_tuple(self, tup)
251 def _getitem_tuple(self, tup):
252 try:
--> 253 return self._getitem_lowerdim(tup)
254 except IndexingError:
255 pass
E:\Enthought\Canopy\User\lib\site-packages\pandas\core\indexing.pyc in _getitem_lowerdim(self, tup)
361 for i, key in enumerate(tup):
362 if _is_label_like(key) or isinstance(key, tuple):
--> 363 section = self._getitem_axis(key, axis=i)
364
365 # we have yielded a scalar ?
E:\Enthought\Canopy\User\lib\site-packages\pandas\core\indexing.pyc in _getitem_axis(self, key, axis)
411 return self._get_loc(key, axis=axis)
412
--> 413 return self._get_label(key, axis=axis)
414
415 def _getitem_iterable(self, key, axis=0):
E:\Enthought\Canopy\User\lib\site-packages\pandas\core\indexing.pyc in _get_label(self, label, axis)
59 return self.obj._xs(label, axis=axis, copy=False)
60 except Exception:
---> 61 return self.obj._xs(label, axis=axis, copy=True)
62
63 def _get_loc(self, key, axis=0):
E:\Enthought\Canopy\User\lib\site-packages\pandas\core\frame.pyc in xs(self, key, axis, level, copy)
2369 loc, new_index = self.index.get_loc_level(key)
2370 else:
-> 2371 loc = self.index.get_loc(key)
2372
2373 if isinstance(loc, np.ndarray):
E:\Enthought\Canopy\User\lib\site-packages\pandas\core\index.pyc in get_loc(self, key)
714 loc : int if unique index, possibly slice or mask if not
715 """
--> 716 return self._engine.get_loc(key)
717
718 def get_value(self, series, key):
E:\Enthought\Canopy\User\lib\site-packages\pandas\index.pyd in pandas.index.IndexEngine.get_loc (pandas\index.c:3542)()
E:\Enthought\Canopy\User\lib\site-packages\pandas\index.pyd in pandas.index.IndexEngine.get_loc (pandas\index.c:3373)()
E:\Enthought\Canopy\User\lib\site-packages\pandas\index.pyd in pandas.index.IndexEngine._get_loc_duplicates (pandas\index.c:3709)()
KeyError: 1

Categories

Resources