1

I want to plot my code as a sequential plot using matplotlib with the summer colormap. In my current code, I've plotted it as a scatterplot, but I want to do this using a line plot.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib as mpl
from matplotlib.colors import  LinearSegmentedColormap

cs2_35_1_10_11 = pd.read_excel('CS2_35_1_10_11.xlsx', sheet_name='Channel_1-008')

cs2_35_1_10_11 = cs2_35_1_10_11.drop(['Data_Point','Date_Time','Charge_Energy(Wh)','Charge_Energy(Wh)','dV/dt(V/s)','AC_Impedance(Ohm)','Is_FC_Data','ACI_Phase_Angle(Deg)','Unnamed: 17'],axis=1)

charge_cc = cs2_35_1_10_11[(cs2_35_1_10_11['Step_Index']==2)]

charge_cc['Cycle_Index'].unique()
for i, cycle in enumerate(charge_cc['Cycle_Index'].unique()):
    cycle = charge_cc['Cycle_Index']
    x = charge_cc[charge_cc['Cycle_Index']==cycle]['Step_Time(s)']
    y = charge_cc[charge_cc['Cycle_Index']==cycle]['Voltage(V)']
    plt.scatter(x, y, c=cycle, cmap='summer')
plt.xlabel('Time(s)')
plt.ylabel('Voltage(v)')
plt.colorbar(label="Cycle Index")
plt.grid()
plt.show()

enter image description here

Data, similar to that used to create this plot, can be found here.

By changing plt.scatter(x, y, c=cycle, cmap='summer') to plt.plot(x, y, c='summer') I have this error:

'summer' is not a valid value for color " even when I add "summer = LinearSegmentedColormap.from_list('summer', ['#ccff33','#004b23'], N=50)
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Leila Amani
  • 81
  • 1
  • 5
  • 1
    This question is not reproducible without **cs2_35_1_10_11**. This question needs a [SSCCE](http://sscce.org/). Please see [How to provide a reproducible dataframe](https://stackoverflow.com/q/52413246/7758804), then **[edit] your question**, and paste the clipboard into a code block. Always provide a [mre] **with code, data, errors, current output, and expected output, as [formatted text](https://stackoverflow.com/help/formatting)**. If relevant, plot images are okay. If you don't include a mre, it is likely the question will be downvoted, closed, and deleted. – Trenton McKinney Jun 19 '23 at 21:31
  • 1
    What's stopping you from using `plt.plot`? – Trenton McKinney Jun 19 '23 at 21:33
  • Please add `cs2_35_1_10_11` to the question, as per the first comment – Trenton McKinney Jun 19 '23 at 21:42
  • no unfortunately it didn't solve my problem – Leila Amani Jun 19 '23 at 21:50
  • 2
    The data you posted does not match the code. However, `import seaborn as sns` and `g = sns.relplot(kind='line', data=charge_cc, x='Step_Time(s)', y='Voltage(V)', hue='Cycle_Index', height=7, palette='summer')` will probably do what you want. – Trenton McKinney Jun 19 '23 at 22:14
  • To use the code as is you can use `cmap = mpl.colormaps.get_cmap('summer').resampled(len(charge_cc['Cycle_Index'].unique()))` and `colors = cmap(np.arange(0, cmap.N))` which will be your list of colors to iterate through and pass to `c=`. – Trenton McKinney Jun 19 '23 at 22:32
  • See [code and plot](https://i.stack.imgur.com/occqF.png) using the sample data provided. Resolved as per the information in the duplicates. – Trenton McKinney Jun 19 '23 at 22:39
  • thank you so much, your seaborn code worked ,it just needs some edits in for loop – Leila Amani Jun 19 '23 at 22:46
  • You're welcome. The seaborn code shouldn't need a loop. – Trenton McKinney Jun 19 '23 at 22:47
  • 1
    thanks a lot ,my problem has solved completely – Leila Amani Jun 19 '23 at 22:54

0 Answers0