Home > Software design >  Plotting step function with empirical data cumulative x-axis
Plotting step function with empirical data cumulative x-axis

Time:09-29

I have a dummy dataset, df:

  Demand    WTP
0    13.0  111.3
1   443.9  152.9
2   419.6   98.2
3   295.9  625.5
4   150.2  210.4

I would like to plot this data as a step function in which the "WTP" are y-values and "Demand" are x-values.

The step curve should start with from the row with the lowest value in "WTP", and then increase gradually with the corresponding x-values from "Demand". However, I can't get the x-values to be cumulative, and instead, my plot becomes this:

enter image description here

I'm trying to get something that looks like this:

enter image description here

  • but instead of a proportion along the y-axis, I want the actual values from my dataset:

This is my code:

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

Demand_quantity = pd.Series([13, 443.9, 419.6, 295.9, 150.2])
Demand_WTP = [111.3, 152.9, 98.2, 625.5, 210.4]

demand_data = {'Demand':Demand_quantity, 'WTP':Demand_WTP}
Demand = pd.DataFrame(demand_data)
Demand.sort_values(by = 'WTP', axis = 0, inplace = True)
print(Demand)

# sns.ecdfplot(data = Demand_WTP, x = Demand_quantity, stat = 'count')

plt.step(Demand['Demand'], Demand['WTP'], label='pre (default)')

plt.legend(title='Parameter where:')
plt.title('plt.step(where=...)')
plt.show()

CodePudding user response:

You can try:

import matplotlib.pyplot as plt
import pandas as pd

df=pd.DataFrame({"Demand":[13, 443.9, 419.6, 295.9, 150.2],"WTP":[111.3, 152.9, 98.2, 625.5, 210.4]})

df=df.sort_values(by=["Demand"])
plt.step(df.Demand,df.WTP)

But I am not really sure about what you want to do. If the x-values are the df.Demand, than the dataframe should be sorted according to this column.

If you want to cumulate the x-values, than try to use numpy.cumsum:

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

df=pd.DataFrame({"Demand":[13, 443.9, 419.6, 295.9, 150.2],"WTP":[111.3, 152.9, 98.2, 625.5, 210.4]})

df=df.sort_values(by=["WTP"])
plt.step(np.cumsum(df.Demand),df.WTP)
  • Related