目前我的代码产生了以下效果:
![enter image description here](https://istack.dev59.com/1OpAc.webp)
我想在条形图中写入文字来达到以下效果:
![enter image description here](https://istack.dev59.com/Iiq84.webp)
目前这是我的基础代码:
我想将Disease
放入条形中
import matplotlib
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
x = [u'Rimegepant',u'Rimegepant',u'Zavegepant',u'Zavegepant',u'Troriluzole',u'Troriluzole',u'Troriluzole',u'Verdiperstat',u'Verdiperstat']
y = [4,3,3,3,2,3,3,3,3]
Disease = ['Acute Treatment of Migraine','Preventive Treatment of Migraine','Acute and Preventive Migraine','Lung Inflammation COVID-19',"Alzheimer's Disease", "OCD", "Spinocerebellar Ataxia", "Multiple System Atrophy", "Amyotrophic Lateral Sclerosis"]
fig, ax = plt.subplots()
width = 0.75 # the width of the bars
ind = np.arange(len(y)) # the x locations for the groups
ax.barh(ind, y, width, color="green", align='edge')
ax.set_yticks(ind+width/2)
ax.set_yticklabels(x, minor=False)
plt.xticks(np.arange(5),('Pre-clinical','Phase I','Phase II','Phase III', 'Approved'))
plt.margins(0,0.05)
plt.title('BHVN')
plt.ylabel('Drug')
plt.show()
for each_bar in bars: plt.text(each_bar.get_x() + each_bar.get_width() / 2.0, 20, your_text_in_every_iteration, color='red', ha='center', va='center', rotation='vertical')
在此之前,您需要像这样将绘图分配给一个变量:bars = plt.bar(xlist, ylist)
。 - Celuk