I would like to draw and append sparklines to my dataframe. I have worked through a bunch of examples which ended up running into Base64 encoded output. I was hoping to use a simple approach, perhaps using matplotlib. I need to visualise linecharts for each row, I am unsure whether a plt figure can be appended to a dataframe cell? My guess is no.
My best attempt is below:
## Imports from scipy import stats import matplotlib.pyplot as plt %matplotlib inline import sparklines import base64 from io import BytesIO import matplotlib matplotlib.use("Agg") ## Create example dataset def sparkline(data, figsize=(4,0.25),**kwags): data = list(data) fig,ax = plt.subplots(1,1,figsize=figsize,**kwags) ax.plot(data) for k,v in ax.spines.items(): v.set_visible(False) ax.set_xticks([]) ax.set_yticks([]) plt.plot(len(data)-1, data[len(data)-1], 'r.') ax.fill_between(range(len(data)), data, len(data)*[min(data)], alpha=0.1) img = BytesIO() plt.savefig(img, transparent=True, bbox_inches='tight') img.seek(0) plt.close() return base64.b64encode(img.read()).decode("UTF-8") ## Create sparkline (output is Base64 sting - Should be linechart) df = pd.DataFrame(price, columns=['Close']) df = df.T df['Trend'] = df.apply(sparkline, axis=1) pd.set_option('display.max_colwidth', -1) df
Not sure why my script doesn’t decode the Base64 string to a linechart. Is there an easy way?
Current Output: Close iVBORw0KGgoAAAANSUhEUgAAAO0AAAAbCAYAAACQhgiLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAFEElEQVR4nO3afYxcVRnH8c9daBdKMVSpadSYaxRMiQKp+NJgG19AozdUNESiiSb8AdYYNEYhV3xLNIRrQkLUqCghVCNSUFMCvYoCajQR62vUCCnVeCMtGihqsCzQ7u74x3M33Taz0+7MbtuZnu8/mzvnzDPP3bm/8/zOcybrdDoSicTwMHa0E0gkEvMjiTaRGDKSaBOJISOJNpEYMpJoE4kh48SjncCRIC/rt+Bbut/v/3BFUxX3HtmsEon+OC5Ei8uxFHd2GTsfd+dlfUlTFVvz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mRf68C9citmz+587ydD54zPrXv/qO6p83L+lxxltmt2m4Qq9/5Yu9znViZ5+Ie8UAMwnbcP2CM44ELxSK5ur3ejV/jYSGQR1E7vK77ScIuLxdbkTscKPQMH8dHhOA+hOeLB/89ovKvE1352fxTiPTRed3ZkeFU4SwmcZVwPofDE4MeFS7InjYv6xX226rZrBLHJStEhb1P2JeDmRZf3PaBk0kcy6zFF4ULeFaIcZ15dtyHnIFFu1BHPk8KK3QwjwurdrOw0F9foM9LDCd34UfCOm8Ue8/DrVCjwuShp/Rm5LrHicSoM9K/iEokRpEk2kRiyEiiTSSGjCTaRGLISKJNJIaMJNpEYsj4P1RkeFLWS0WaAAAAAElFTkSuQmCC Name: Trend, dtype: object
should look like this:
Advertisement
Answer
So I worked this one out myself.
I defined the function to generate the sparkline:
def sparkline(data, figsize=(4,0.25),**kwags): data = list(data) fig,ax = plt.subplots(1,1,figsize=figsize,**kwags) ax.plot(data) for k,v in ax.spines.items(): v.set_visible(False) ax.set_xticks([]) ax.set_yticks([]) plt.plot(len(data)-1, data[len(data)-1], 'r.') ax.fill_between(range(len(data)), data, len(data)*[min(data)], alpha=0.1) img = BytesIO() plt.savefig(img, transparent=True, bbox_inches='tight') img.seek(0) # plt.show() plt.close() return base64.b64encode(img.read()).decode("utf-8")
With this function in memory, I was then able to generate the required PNG images and then save them to a directory. Working from excel, I imported the images.
d2 = pd.DataFrame(price, columns=['Close']) d2 = d2.T d2['Trend'] = d2.apply(sparkline, axis=1) data = str(list(d2['Trend'])) im = Image.open(BytesIO(base64.b64decode(data))) im.save('C:/Users/Toby/Desktop/IB/Training/Lines/'+str(t)+'.png', 'PNG')