Reputation: 974
If I run the following code:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'start': [0, 3, 7], 'end': [1, 4, 8]})
fig, ax = plt.subplots()
for i in df.index:
ax.fill_betweenx([0, 1], df.loc[i, 'start'], df.loc[i, 'end'], color='red')
I get this output, as expected:
The problem with the code I've written is that, if I have a huge dataframe, then looping through all the rows takes a long time.
Is there a faster way of achieving the same output?
matplotlib or plotly solutions welcome.
Upvotes: 0
Views: 105
Reputation: 36775
If you use fill_between
instead of fill_betweenx
, you can do
boundaries = df[['start', 'end', 'end']].to_numpy().ravel()
ax.fill_between(
boundaries,
np.zeros(len(boundaries)),
np.ones(len(boundaries)),
where=np.tile([True, True, False], len(df)),
color='red'
)
Upvotes: 1