producing a series calculation of a dataframe to provide a percentage of NaN's to the total amount of rows as shown:
data = df.isnull().sum()/len(df)*100
RecordID 0.000000
ContactID 0.000000
EmailAddress 0.000000
ExternalID 100.000000
Date 0.000000
Name 0.000000
Owner 67.471362
Priority 0.000000
Status 0.000000
Subject 0.000000
Description 0.000000
Type 0.000000
dtype: float64
What I'm keen to do is represent this as a heatmap in seaborn sns.heatmap(data), drawing the readers attention those with 100 and 67%, unfortunately I'm getting this error
IndexError: Inconsistent shape between the condition and the input
(got (12, 1) and (12,))
Full traceback:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-17-05db696a3a9b> in <module>()
----> 1 sns.heatmap(data)
~\AppData\Local\Programs\Python\Python36-32\lib\site-packages\seaborn\matrix.py in heatmap(data, vmin, vmax, cmap, center, robust, annot, fmt, annot_kws, linewidths, linecolor, cbar, cbar_kws, cbar_ax, square, xticklabels, yticklabels, mask, ax, **kwargs)
515 plotter = _HeatMapper(data, vmin, vmax, cmap, center, robust, annot, fmt,
516 annot_kws, cbar, cbar_kws, xticklabels,
--> 517 yticklabels, mask)
518
519 # Add the pcolormesh kwargs here
~\AppData\Local\Programs\Python\Python36-32\lib\site-packages\seaborn\matrix.py in __init__(self, data, vmin, vmax, cmap, center, robust, annot, fmt, annot_kws, cbar, cbar_kws, xticklabels, yticklabels, mask)
114 mask = _matrix_mask(data, mask)
115
--> 116 plot_data = np.ma.masked_where(np.asarray(mask), plot_data)
117
118 # Get good names for the rows and columns
~\AppData\Local\Programs\Python\Python36-32\lib\site-packages\numpy\ma\core.py in masked_where(condition, a, copy)
1934 if cshape and cshape != ashape:
1935 raise IndexError("Inconsistent shape between the condition and the input"
-> 1936 " (got %s and %s)" % (cshape, ashape))
1937 if hasattr(a, '_mask'):
1938 cond = mask_or(cond, a._mask)
IndexError: Inconsistent shape between the condition and the input (got (12, 1) and (12,))
My research if hitting a lot of walls around numpy broadcasting rules, or a bug from 3 years ago - none of which are super helpful.
Thanks as always.
Your data variable is an instance of pd.Series which is inherently 1D. However,
sns.heatmap expects a 2D input. A quick fix is for example the following:
sns.heatmap(data.to_frame())
Related
I need to learn how to use Healpy and so I was trying to reproduce the results of the basic tutorial. I use Anaconda on Ubuntu 22.04 and I think I have all the pre-requisites (I have Python 3.9.13, Numpy, Matplotlib, Astropy, python3-dev and python-dev-is-python3 installed).
I have tried many variations of what is shown in the tutorial notebook, including a literal copy+paste of the code, I've tried to do this in Ipython on terminal, on a jupyter notebook, on Spyder, I've tried to include the %matplotlib inline (after importing matplotlib) in all of these options (I've tried not to include in all of them too), and in all situations I end up with the exact same error message (full error message in the end of the post):
ValueError: Passing a Normalize instance simultaneously with vmin/vmax
is not supported. Please pass vmin/vmax directly to the norm when
creating it.
Everything works except for the plot. I've tried setting min and max in the hp.mollview() command according to the documentation, but it didn't work too. It seems like a bug to me, so I thought about creating an issue ticket on github, but honestly the tutorial is very updated and I don't think this kind of bug would go unnoticed, so I'm thinking I missed some minor detail and I hope someone in here can help me identify what it is. In the meantime, I'll probably try to learn some other version of Healpix.
Here is the full error message when I run the code in a jupyter notebook (by the way, sorry if my question is not very well organized, this is my first post):
--------------------------------------------------------------------------- ValueError Traceback (most recent call
last) Cell In [5], line 2
1 m = np.arange(NPIX)
----> 2 hp.mollview(m, title="Mollview image RING")
3 hp.graticule()
File ~/anaconda3/lib/python3.9/site-packages/healpy/visufunc.py:250,
in mollview(map, fig, rot, coord, unit, xsize, title, nest, min, max,
flip, remove_dip, remove_mono, gal_cut, format, format2, cbar, cmap,
badcolor, bgcolor, notext, norm, hold, reuse_axes, margins, sub,
nlocs, return_projected_map)
246 elif remove_mono:
247 map = pixelfunc.remove_monopole(
248 map, gal_cut=gal_cut, nest=nest, copy=True, verbose=True
249 )
--> 250 img = ax.projmap(
251 map,
252 nest=nest,
253 xsize=xsize,
254 coord=coord,
255 vmin=min,
256 vmax=max,
257 cmap=cmap,
258 badcolor=badcolor,
259 bgcolor=bgcolor,
260 norm=norm,
261 )
262 if cbar:
263 im = ax.get_images()[0]
File ~/anaconda3/lib/python3.9/site-packages/healpy/projaxes.py:736,
in HpxMollweideAxes.projmap(self, map, nest, **kwds)
734 nside = pixelfunc.npix2nside(pixelfunc.get_map_size(map))
735 f = lambda x, y, z: pixelfunc.vec2pix(nside, x, y, z, nest=nest)
--> 736 return super(HpxMollweideAxes, self).projmap(map, f, **kwds)
File ~/anaconda3/lib/python3.9/site-packages/healpy/projaxes.py:726,
in MollweideAxes.projmap(self, map, vec2pix_func, xsize, **kwds)
724 def projmap(self, map, vec2pix_func, xsize=800, **kwds):
725 self.proj.set_proj_plane_info(xsize=xsize)
--> 726 img = super(MollweideAxes, self).projmap(map, vec2pix_func, **kwds)
727 self.set_xlim(-2.01, 2.01)
728 self.set_ylim(-1.01, 1.01)
File ~/anaconda3/lib/python3.9/site-packages/healpy/projaxes.py:202,
in SphericalProjAxes.projmap(self, map, vec2pix_func, vmin, vmax,
badval, badcolor, bgcolor, cmap, norm, rot, coord, **kwds)
200 ext = self.proj.get_extent()
201 img = np.ma.masked_values(img, badval)
--> 202 aximg = self.imshow(
203 img,
204 extent=ext,
205 cmap=cm,
206 norm=nn,
207 interpolation="nearest",
208 origin="lower",
209 vmin=vmin,
210 vmax=vmax,
211 **kwds
212 )
213 xmin, xmax, ymin, ymax = self.proj.get_extent()
214 self.set_xlim(xmin, xmax)
File
~/anaconda3/lib/python3.9/site-packages/matplotlib/_api/deprecation.py:454,
in make_keyword_only..wrapper(*args, **kwargs)
448 if len(args) > name_idx:
449 warn_deprecated(
450 since, message="Passing the %(name)s %(obj_type)s "
451 "positionally is deprecated since Matplotlib %(since)s; the "
452 "parameter will become keyword-only %(removal)s.",
453 name=name, obj_type=f"parameter of {func.name}()")
--> 454 return func(*args, **kwargs)
File
~/anaconda3/lib/python3.9/site-packages/matplotlib/init.py:1423,
in _preprocess_data..inner(ax, data, *args, **kwargs) 1420
#functools.wraps(func) 1421 def inner(ax, *args, data=None,
**kwargs): 1422 if data is None:
-> 1423 return func(ax, *map(sanitize_sequence, args), **kwargs) 1425 bound = new_sig.bind(ax, *args, **kwargs) 1426 auto_label = (bound.arguments.get(label_namer) 1427
or bound.kwargs.get(label_namer))
File
~/anaconda3/lib/python3.9/site-packages/matplotlib/axes/_axes.py:5577,
in Axes.imshow(self, X, cmap, norm, aspect, interpolation, alpha,
vmin, vmax, origin, extent, interpolation_stage, filternorm,
filterrad, resample, url, **kwargs) 5574 if im.get_clip_path() is
None: 5575 # image does not already have clipping set, clip to
axes patch 5576 im.set_clip_path(self.patch)
-> 5577 im._scale_norm(norm, vmin, vmax) 5578 im.set_url(url) 5580 # update ax.dataLim, and, if autoscaling, set viewLim 5581 #
to tightly fit the image, regardless of dataLim.
File ~/anaconda3/lib/python3.9/site-packages/matplotlib/cm.py:405, in
ScalarMappable._scale_norm(self, norm, vmin, vmax)
403 self.set_clim(vmin, vmax)
404 if isinstance(norm, colors.Normalize):
--> 405 raise ValueError(
406 "Passing a Normalize instance simultaneously with "
407 "vmin/vmax is not supported. Please pass vmin/vmax "
408 "directly to the norm when creating it.")
410 # always resolve the autoscaling so we have concrete limits
411 # rather than deferring to draw time.
412 self.autoscale_None()
ValueError: Passing a Normalize instance simultaneously with vmin/vmax
is not supported. Please pass vmin/vmax directly to the norm when
creating it.
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.
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()
I have a latitude array with the shape (1111,) and am attempting to use matplotlib pcolormesh, but I'm getting an error since my array is not 2D so I am getting the error not enough values to unpack (expected 2, got 1). Is there a way I can add 1111 zeros to the second dimension of my latitude array? Below is the code I have that is causing the error.
import matplotlib.cm as cm
cmap = cm.get_cmap('BrBG')
cs = plt.pcolormesh(longitude.values, latitude.values, dens, cmap = cmap)
plt.title('Satellite Trajectory')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.savefig('Satellite Trajectory', dpi=200, bbox_inches='tight', pad_inches=0.025)
cb = plt.colorbar(cs, orientation = 'vertical')
cb.set_label(r'Density')
These are the first few lines of my Pandas latitude array:
0 50.224832
1 50.536422
2 50.847827
3 51.159044
4 51.470068
5 51.780895
6 52.091521
7 52.401941
8 52.712151
9 53.022145
10 53.331919
I have the same issue with the longitude array too. Here are some longitude values for reference.
0 108.873007
1 108.989510
2 109.107829
3 109.228010
4 109.350097
5 109.474136
6 109.600176
7 109.728265
8 109.858455
9 109.990798
10 110.125348
The other array shown is dens which is a density array and has the shape (5, 91, 181). Here are a few values for reference:
[6.042968853864891e-12, 6.042894605467602e-12, 6.042777396826408e-12, 6.042616263531836e-12, 6.042410211830538e-12, 6.042158216350682e-12, 6.0361190688090634e-12, 6.038107492458882e-12, 6.039984972063208e-12, 6.041748879958635e-12, 6.030375732644546e-12, 6.027898597657696e-12, 6.0251851962303345e-12, 6.0390021800772395e-12, 6.035096323493865e-12, 6.030879347062723e-12, 6.026343416350273e-12, 6.021480432118012e-12, 6.01628202402901e-12, 6.042274874237314e-12, 6.040409269411221e-1
I'm just stuck how to execute the pcolormesh without getting the following error:
ValueError Traceback (most recent call last)
<ipython-input-54-685815191229> in <module>
7
8
----> 9 cs = plt.pcolormesh(longitude.values, latitude.values, dens, cmap = cmap)
10
11 plt.title('Satellite Trajectory')
~\Anaconda3\lib\site-packages\matplotlib\pyplot.py in pcolormesh(alpha, norm, cmap, vmin, vmax, shading, antialiased, data, *args, **kwargs)
2771 *args, alpha=alpha, norm=norm, cmap=cmap, vmin=vmin,
2772 vmax=vmax, shading=shading, antialiased=antialiased,
-> 2773 **({"data": data} if data is not None else {}), **kwargs)
2774 sci(__ret)
2775 return __ret
~\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
1808 "the Matplotlib list!)" % (label_namer, func.__name__),
1809 RuntimeWarning, stacklevel=2)
-> 1810 return func(ax, *args, **kwargs)
1811
1812 inner.__doc__ = _add_data_doc(inner.__doc__,
~\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in pcolormesh(self, alpha, norm, cmap, vmin, vmax, shading, antialiased, *args, **kwargs)
5980 allmatch = (shading == 'gouraud')
5981
-> 5982 X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch)
5983 Ny, Nx = X.shape
5984 X = X.ravel()
~\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in _pcolorargs(funcname, allmatch, *args)
5547 if isinstance(Y, np.ma.core.MaskedArray):
5548 Y = Y.data
-> 5549 numRows, numCols = C.shape
5550 else:
5551 raise TypeError(
ValueError: not enough values to unpack (expected 2, got 1)
I'm assuming it is because of the longitde and latitude array shape, so I'm asking for help filling the second dimension so I have an array (1111,1111) rather than (1111,).
If you have another recommendation I would love help. I am new to Python.
Use a for loop or a list comprehension in that case.
latitude = [50.224832, 50.536422, 50.847827, 51.159044, 51.470068]
longitude = [108.873007, 108.989510, 109.107829, 109.228010, 109.350097]
density = [.15,.25,.35,.45,.55]
output = [(latitude[i], longitude[i], density[i]) for i in range(len(latitude))]
print(output)
[(50.224832, 108.873007, 0.15), (50.536422, 108.98951, 0.25), (50.847827, 109.107829, 0.35), (51.159044, 109.22801, 0.45), (51.470068, 109.350097, 0.55)]
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'