0

I would like to plot certain slices of my Pandas Dataframe for each rows (based on row indexes) with different colors.

My data look like the following: img

I already tried with the help of this tutorial to find a way but I couldn't - probably due to a lack of skills.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.read_csv("D:\SOF10.csv" , header=None)
df.head()

#Slice interested data
C = df.iloc[:, 2::3]

#Plot Temp base on row index colorfully
C.apply(lambda x: plt.scatter(x.index, x, c='g'))
plt.show()

Following is my expected plot: img

I was also wondering if I could displace the mean of each row of the sliced data which contains 480 values somewhere in the plot or in the legend beside of plot! Is it feasible (like the following picture) to calculate the mean and displaced somewhere in the legend or by using small font size displace next to its own data in graph ?

Data sample: data

Mario
  • 1,631
  • 2
  • 21
  • 51
  • Try remove ` c='g'` in `C.apply(lambda x: plt.scatter(x.index, x, c='g'))`. – Quang Hoang Apr 08 '19 at 14:36
  • @QuangHoang Hi , I did already and I updated the picture but as it can be seen it's not what I want to. – Mario Apr 08 '19 at 16:46
  • That's because you didn't slice/handle the data correctly, not because of how you would plot it.`C = df.iloc[:, 2::3]`still keeps the index for the sliced columns. So all the series plotted shares the same indices. That's why you see all the dots interleaved, not separated. – Quang Hoang Apr 08 '19 at 16:57
  • @QuangHoang oh I see so what's your suggestion ? How can I fix it? Is there any remedy ? – Mario Apr 08 '19 at 16:59

2 Answers2

2

This gives the plot without legend

C = df.iloc[:,2::3].stack().reset_index()
C.columns = ['level_0', 'level_1', 'Temperature']

fig, ax = plt.subplots(1,1)
C.plot('level_0', 'Temperature', 
       ax=ax, kind='scatter', 
       c='level_0', colormap='tab20', 
       colorbar=False, legend=True)
ax.set_xlabel('Cycles')
plt.show()

Edit to reflect modified question:

  1. stack() transform your (sliced) dataframe to a series with index (row, col)
  2. reset_index() reset the double-level index above to level_0 (row), level_1 (col).
  3. set_xlabel sets the label of x-axis to what you want.

Edit 2: The following produces scatter with legend:

CC = df.iloc[:,2::3]

fig, ax = plt.subplots(1,1, figsize=(16,9))
labels = CC.mean(axis=1)

for i in CC.index:
    ax.scatter([i]*len(CC.columns[1:]), CC.iloc[i,1:], label=labels[i])

ax.legend()
ax.set_xlabel('Cycles')
ax.set_ylabel('Temperature')
plt.show()
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • The output didn't fulfill my expected result while I wanted to plot each row by different color! May i ask you check it out again the output picture? – Mario Apr 08 '19 at 18:27
  • Are you sure each row a color or each column a color? – Quang Hoang Apr 08 '19 at 18:28
  • surely each row sir ! – Mario Apr 08 '19 at 18:29
  • I also provided the data sample in the post you can plot it. The outcome of your answer was far from my expectation in spite of being colorful and x-axis wasn't base on **row-indexes** from 0 to 40 – Mario Apr 08 '19 at 18:32
  • I just updated the expected result picture hope it transfers my mindset better ! – Mario Apr 08 '19 at 18:48
  • 1
    I'm kinda lost as to what you want. You can try drop `.T` before `stack()` and try replace `'idx'` inside `plot` by `'level_0'`. I can't access google drive for now. – Quang Hoang Apr 08 '19 at 18:52
  • man it worked perfectly ! if you had an idea to improve colors plz and edit your answer since it's a bit difficult to differentiate between the colors due to they are so similar. I mean It would be nice if composition of colors are much distinguishable and different. I'm also curious about what are `idx`, `level_0`,`level_1`,`val` shortly. if you leave some comments in your answer, it would be great. I will accept it as the answer of this question surely – Mario Apr 08 '19 at 19:02
  • Is there any way to displace average of each row with its own color beside of plot ? – Mario Apr 08 '19 at 19:11
  • another issue when i get plot description under x-axis is "level_0" while I need to edit to my desired description which is "Cycles" ! and Y axis should be "Temperature" instead of val. I hope when you access google drive and plot you understand me better. – Mario Apr 08 '19 at 19:17
  • Thanks for update it was what I want but do u know how I could change the color to `mapcolor='prism`? I also could improve the legend position by `ax = plt.gca() plt.legend(bbox_to_anchor=(1.1, 1.1), bbox_transform=ax.transAxes)` and it looks nice! I also have another similar question regarding this issue to levelize the plot I would like to ask separately [here](https://stackoverflow.com/questions/55270346/how-can-fit-the-data-on-temperature-thermal-profile?) with this(current) post dataset and dataframe and data slice. i would appreciate that if you have a look on that. – Mario Apr 09 '19 at 17:28
0

This may be an approximate answer. scatter(c=, cmap= can be used for desired coloring.

import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import itertools

df = pd.DataFrame({'a':[34,22,1,34]})
fig, subplot_axes = plt.subplots(1, 1, figsize=(20, 10))  # width, height

colors = ['red','green','blue','purple']
cmap=matplotlib.colors.ListedColormap(colors)

for col in df.columns:
    subplot_axes.scatter(df.index, df[col].values, c=df.index, cmap=cmap, alpha=.9)
Rich Andrews
  • 1,590
  • 8
  • 12
  • This is coloring columnwise. The question wants the markers to be colored based on their ordinal position in the column (index). – Rich Andrews Apr 08 '19 at 17:34
  • Hi Thanks for response question is about either accessing desired data or plotting them based on row index **row-wise** – Mario Apr 08 '19 at 17:51
  • Updated to use colors from df.index. @Quan Hoang provides a more succinct answer. – Rich Andrews Apr 08 '19 at 17:56
  • the output of your answer is totally different from my expected result since it displays colorful columns! – Mario Apr 08 '19 at 18:28
  • I just updated the expected result picture hope it transfers my mindset better ! – Mario Apr 08 '19 at 18:49