Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 22634

3D scatter plot with date axis

$
0
0

I'm having problems with plotting 3D data where one of the dimensions is a date. I am wondering how I can solve it because any solution I can come up with looks too complicated for such a common case problem.

My solution is to drop the date dimension and replace it with an integer. I'd like to map this integer back to the dates. Any ideas what the best way to do this?

The code below works, but please adjust N to a smaller number if you want.

import pandas as pdimport matplotlib.pyplot as pltimport numpy as npN = 1000bm = pd.DataFrame(    index=pd.bdate_range(start='2012-01-01', periods=N, freq='B'),     data={x: np.random.randn(N) for x in range(1, 11)})# Simulate some zerosbm = pd.DataFrame(index=bm.index, columns=bm.columns, data=np.where(np.abs(bm.values) < 0.02, 0, bm.values))# Set zeros to Nan so that I don't plot thembm = bm.replace({0: np.nan})# unstack dataframeflat_bm = bm.reset_index(drop=True).unstack()  # DROP DATES AND REPLACE WITH INTx = flat_bm.index.get_level_values(0)y = flat_bm.index.get_level_values(1)z = flat_bm.values# Set up plotfig = plt.figure(figsize = (15,10))ax = plt.axes(projection ='3d')# plottingax.scatter(x, y, z, '.', c=flat_bm.values, cmap='Reds')

At the end, the plot has an integer instead of date labels.


Viewing all articles
Browse latest Browse all 22634

Trending Articles