Reputation: 21
I have a DataFrame that contains the history of weight values for N items. The sum of the weights for all items is 1. The date is when the weights were set, and they stay the same until they are set again.
Here is the Dataframe.
item2 item3 item4 item1
2018-03-26 0.333331 0.333331 0.000008 0.333331
2018-03-29 0.097470 0.249645 0.250353 0.402532
2018-04-01 0.329531 0.080273 0.356974 0.233221
2018-04-04 0.279514 0.048366 0.288980 0.383140
2018-04-07 0.289597 0.082032 0.201930 0.426442
2018-04-10 0.258850 0.107432 0.280754 0.352964
2018-04-13 0.030390 0.172697 0.486147 0.310766
2018-04-16 0.019628 0.095833 0.433445 0.451094
2018-04-19 0.054276 0.270346 0.351813 0.323565
2018-04-22 0.075713 0.089441 0.385883 0.448963
2018-04-25 0.109152 0.071784 0.536841 0.282223
2018-04-28 0.172047 0.108976 0.360786 0.358192
2018-05-01 0.059381 0.072429 0.434918 0.433272
I want to visualize how the weights change over time. I came up with this.
#weight_history is the DataFrame
tot = np.zeros(len(weight_history))
for weight in weight_history:
plt.step(weight_history[weight].index, weight_history[weight].values+tot,where='post',label=weight)
tot += weight_history[weight].values
plt.set_ylabel("Weights")
plt.legend()
Which produces this:
This is very close to what I want, but it is hard to visualize how much weight each item has.
How can I fill in the area under each line?
So filled blue from 0 to the blue line, filled orange from the blue line to the orange line, filled green from the orange line to the green line, and filled red from the green line to the red line.
Is there an alternative way to do this?
Upvotes: 1
Views: 504
Reputation: 21
I figured it out, fill_between has a step option.
tot = np.zeros(len(weight_history))
for weight in weight_history:
axes[1].fill_between(x=weight_history.index, y1=weight_history[weight].values+tot,y2=tot,step='post',label=weight)
tot += weight_history[weight].values
axes[1].set_ylabel("Weights")
axes[1].legend()
Upvotes: 1
Reputation: 8162
Try something like this
plt.fill_between(x, item2, item3, color='blue')
plt.show()
Upvotes: 0