Pandas dataframe process in fiddle

I have data that I read from an Excel spreadsheet. The data have a series of observations for each of the six scenarios, from S1 to S6. When I read the data in my dataframe df, it looks like this:

Scenario LMP 0 S1 -21.454544 1 S1 -20.778094 2 S1 -20.027689 3 S1 -19.747170 4 S1 -20.814405 5 S1 -21.955406 6 S1 -23.018960 ... 12258 S6 -34.089906 12259 S6 -34.222814 12260 S6 -26.712010 12261 S6 -24.555973 12262 S6 -23.062616 12263 S6 -20.488411 

I want to create a violin poster with a different violin for each of the six scenarios. I am new to Pandas and dataframes, and despite a lot of research / testing over the last day, I can’t find an elegant way to pass some links to my framework (to break it down into different series for each scenario), which will work in the axes statement .violinplot (). For example, I tried the following, which does not work. I get "ValueError: I can not copy a sequence with size 1752 onto the axis of an array with size 2" in my axes.violinplot statement.

 import pandas as pd import numpy as np import matplotlib.pyplot as plt # load data into a dataframe df = pd.read_excel('Modeling analysis charts.xlsx', sheetname='lmps', parse_cols=[7,12], skiprows=0, header=1) fontsize = 10 fig, axes = plt.subplots() axes.violinplot(dataset = [[df.loc[df.Scenario == 'S1']], [df.loc[df.Scenario == 'S2']], [df.loc[df.Scenario == 'S3']], [df.loc[df.Scenario == 'S4']], [df.loc[df.Scenario == 'S5']], [df.loc[df.Scenario == 'S6']] ] ) axes.set_title('Day Ahead Market') axes.yaxis.grid(True) axes.set_xlabel('Scenario') axes.set_ylabel('LMP ($/MWh)') plt.show() 
+5
source share
2 answers

You need to be careful how to create a dataset to build. In the code from the question, you have a list of lists of one data frame. However, you just need a list of one-column frames.

Thus, you will also need to take only the β€œLMP” column from the filtered data frames, otherwise the script will not know which column will be displayed.

Here is a working example that is close to the source code:

 import pandas as pd import numpy as np import matplotlib.pyplot as plt x = np.random.poisson(lam =3, size=100) y = np.random.choice(["S{}".format(i+1) for i in range(6)], size=len(x)) df = pd.DataFrame({"Scenario":y, "LMP":x}) fig, axes = plt.subplots() axes.violinplot(dataset = [df[df.Scenario == 'S1']["LMP"], df[df.Scenario == 'S2']["LMP"], df[df.Scenario == 'S3']["LMP"], df[df.Scenario == 'S4']["LMP"], df[df.Scenario == 'S5']["LMP"], df[df.Scenario == 'S6']["LMP"] ] ) axes.set_title('Day Ahead Market') axes.yaxis.grid(True) axes.set_xlabel('Scenario') axes.set_ylabel('LMP ($/MWh)') plt.show() 

enter image description here

+5
source

You can use marine. In this case, import the marine vessel, and then use the violin plot to visualize the scenarios.

 import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns # load data into a dataframe df = pd.read_excel('Modeling analysis charts.xlsx', sheetname='lmps', parse_cols=[7,12], skiprows=0, header=1) fontsize = 10 fig, axes = plt.subplots() # plot violin. 'Scenario' is according to x axis, # 'LMP' is y axis, data is your dataframe. ax - is axes instance sns.violinplot('Scenario','LMP', data=df, ax = axes) axes.set_title('Day Ahead Market') axes.yaxis.grid(True) axes.set_xlabel('Scenario') axes.set_ylabel('LMP ($/MWh)') plt.show() 

enter image description here

+6
source

Source: https://habr.com/ru/post/1266586/


All Articles