简体   繁体   中英

Add labels and vertical line on matplotlib (outside plot area)

I have a pandas dataframe similar to:

df = pd.DataFrame([['1/1/2020',0.1,0.3,'cat1','csv'],\
                  ['1/1/2020',0.4,0.7,'cat1','excel'],\
                  ['1/1/2020',1.7,2.3,'cat2','csv'],\
                  ['2/1/2020',0,0.3,'cat2','csv'],\
                  ['2/1/2020',0.4,0.5,'cat2','csv'],\
                  ['2/1/2020',0.5,0.7,'cat1','csv'],\
                  ['2/1/2020',1.1,1.3,'cat1','excel']],\
                     columns = ['date','start','end','cat','source'])

Each row of the df contains range of values to be colored and the color group (cat). I can plot a graph like the following with the following code: plt.figure(figsize=(10,3))

for i,r in df.iterrows():
    plt.barh(r['date'], width=r['end']-r['start'],
             left=r['start'], color='y' if r['cat']=='cat1' else 'g')

在此处输入图像描述

My issue is, I would like to add zoning details:

在此处输入图像描述

The answer of this question can be broken down into 2 parts:

1. Plot textbox outside the xy plot

props = dict(boxstyle='round', facecolor='wheat', alpha=1)
plt.text(0, .9, "textstr", fontsize=14, transform=plt.gcf().transFigure,verticalalignment='top', bbox=props)

The values of 0 and 0.9 can be adjusted manually for your purpose.

2. draw a vertical line on the xy plot

plt.axvline(x=5,color='r', linewidth = 5)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM