如何在图表右侧添加子图 table
How to add subplot table to right of graph
我想合并 table 和水平条形图,这样您就可以在条形图中看到 table 中的注释。
我现在的样子是这样的:
我只想在我的代码中 Catalyst =
的图表右侧添加一列,以获得如下所示的内容(在演示文稿中可能更明显,这些行与条形图一致):
我假设这将通过调用子图来完成。我在将第二个子图设为 table 时遇到了一些麻烦,所以我对如何执行它有点困惑。这是我的淡化代码:
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"]
Catalyst = ["asdjfhskjdfhksdhf", "sdklfhslkijflk", "slkdjflskjf", "sikdjifliskjflissdofjlskdjf", "skdfjsolidjfhsoidfhjoslif","skdfhlskdhjfskldjflksjdflsk", "skidfhslikdjhfislkdjflskdfnlskdjflsk", "skdjfhlskdjlfksjdflksdjflskdjflskdjflskdjflksdjlkfnslkfnslkdfn"]
fig, ax1 = plt.subplots()
width = 0.75 # the width of the bars
ind = np.arange(len(y)) # the x locations for the groups
ax1.barh(ind, y, width, color="green", align='edge')
ax1.set_yticks(ind+width/2)
ax1.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')
for bar, disease in zip(ax1.patches, Disease[::-1]):
ax1.text(0.1, bar.get_y()+bar.get_height()/2, disease, color = 'white', ha = 'left', va = 'center')
plt.show()
不要在绘图中包含 table,而是在右侧创建第二个(双)y 轴并将该轴的 yticklabels 设置为催化剂列表:
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"]
Catalyst = ["Primary completion Oct 2021, Study completion Nov 2022",
"Primary completion Oct 2021, Study completion Sept 20, 2022",
"sdklfhslkijflk", "slkdjflskjf", "sikdjifliskj",
"skdfhlskdhjfskldjflksjdflsk", "skidfhslikdjhfislkdjflskdfnlskdjflsk",
"skdjfhlskdjlfksjdflksdjflskdjflskdjflskdjflksdjlkfnslkfnslkdfn",
"Approved on Feb 27, 2020"]
fig = plt.figure(figsize=(12,5))
ax1 = fig.add_subplot()
ax2 = ax1.twinx()
width = 0.75 # the width of the bars
ind = np.arange(len(y)) # the x locations for the groups
ax1.barh(ind, y, width, color="green", align='edge')
ax1.set_yticks(ind+width/2)
ax1.set_yticklabels(x, minor=False)
ax1.set_ylabel('Drug')
ax1.set_xmargin(1.5)
ax1.autoscale_view()
ax2.set_ylim(ax1.get_ylim())
ax2.set_yticks(np.arange(len(Catalyst)) + width/2)
ax2.set_yticklabels(Catalyst,horizontalalignment = "right")
ax1.spines['right'].set_color('None')
ax2.spines['right'].set_color('None')
plt.xticks(np.arange(5),('Pre-clinical','Phase I','Phase II','Phase III', 'Approved'))
fig.suptitle('BHVN')
plt.show()
要在图形的右侧添加一个table,请将ax.table() 的loc 属性设置到右侧。之后,根据左边的item指定cell的高度。接下来,调整 bbox 的位置,因为它们是重叠的。紧度作为注释添加。
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"]
Catalyst = ["Approved on Feb 27, 2020",
"sNDA submitted in Oct 2020, PDUFA goal date 2Q 2021",
"Primary and study completion expected Mar 1, 2021",
"Expected completion April 2021",
"Topline data 1Q2021, Study completion Jan 23, 2022",
"Phase III completion Mar 2023",
"Estimated completion Nov 30, 2021",
"Primary completion Oct 20, 2021, Study completion Sep 20, 2022",
"Primary completion Oct 2021, Study completion Nov 2022"]
tbl_cnt = [[c] for c in Catalyst]
fig, ax1 = plt.subplots()
width = 0.75 # the width of the bars
ind = np.arange(len(y)) # the x locations for the groups
ax1.barh(ind, y, width, color="green", align='edge')
ax1.set_yticks(ind+width/2)
ax1.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')
for bar, disease in zip(ax1.patches, Disease[::-1]):
ax1.text(0.1, bar.get_y()+bar.get_height()/2, disease, color = 'white', ha = 'left', va = 'center')
rtable = ax1.table(cellText=tbl_cnt, cellLoc='left', loc='right', bbox=[1.05,0,1.0,1], edges='horizontal')
ax1.text(1.3,0.9,'Catalyst', transform=fig.transFigure)
cellDict = rtable.get_celld()
for i in range(0,len(Catalyst)):
cellDict[(i,0)].set_height(0.11)
plt.show()
我想合并 table 和水平条形图,这样您就可以在条形图中看到 table 中的注释。
我现在的样子是这样的:
我只想在我的代码中 Catalyst =
的图表右侧添加一列,以获得如下所示的内容(在演示文稿中可能更明显,这些行与条形图一致):
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"]
Catalyst = ["asdjfhskjdfhksdhf", "sdklfhslkijflk", "slkdjflskjf", "sikdjifliskjflissdofjlskdjf", "skdfjsolidjfhsoidfhjoslif","skdfhlskdhjfskldjflksjdflsk", "skidfhslikdjhfislkdjflskdfnlskdjflsk", "skdjfhlskdjlfksjdflksdjflskdjflskdjflskdjflksdjlkfnslkfnslkdfn"]
fig, ax1 = plt.subplots()
width = 0.75 # the width of the bars
ind = np.arange(len(y)) # the x locations for the groups
ax1.barh(ind, y, width, color="green", align='edge')
ax1.set_yticks(ind+width/2)
ax1.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')
for bar, disease in zip(ax1.patches, Disease[::-1]):
ax1.text(0.1, bar.get_y()+bar.get_height()/2, disease, color = 'white', ha = 'left', va = 'center')
plt.show()
不要在绘图中包含 table,而是在右侧创建第二个(双)y 轴并将该轴的 yticklabels 设置为催化剂列表:
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"]
Catalyst = ["Primary completion Oct 2021, Study completion Nov 2022",
"Primary completion Oct 2021, Study completion Sept 20, 2022",
"sdklfhslkijflk", "slkdjflskjf", "sikdjifliskj",
"skdfhlskdhjfskldjflksjdflsk", "skidfhslikdjhfislkdjflskdfnlskdjflsk",
"skdjfhlskdjlfksjdflksdjflskdjflskdjflskdjflksdjlkfnslkfnslkdfn",
"Approved on Feb 27, 2020"]
fig = plt.figure(figsize=(12,5))
ax1 = fig.add_subplot()
ax2 = ax1.twinx()
width = 0.75 # the width of the bars
ind = np.arange(len(y)) # the x locations for the groups
ax1.barh(ind, y, width, color="green", align='edge')
ax1.set_yticks(ind+width/2)
ax1.set_yticklabels(x, minor=False)
ax1.set_ylabel('Drug')
ax1.set_xmargin(1.5)
ax1.autoscale_view()
ax2.set_ylim(ax1.get_ylim())
ax2.set_yticks(np.arange(len(Catalyst)) + width/2)
ax2.set_yticklabels(Catalyst,horizontalalignment = "right")
ax1.spines['right'].set_color('None')
ax2.spines['right'].set_color('None')
plt.xticks(np.arange(5),('Pre-clinical','Phase I','Phase II','Phase III', 'Approved'))
fig.suptitle('BHVN')
plt.show()
要在图形的右侧添加一个table,请将ax.table() 的loc 属性设置到右侧。之后,根据左边的item指定cell的高度。接下来,调整 bbox 的位置,因为它们是重叠的。紧度作为注释添加。
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"]
Catalyst = ["Approved on Feb 27, 2020",
"sNDA submitted in Oct 2020, PDUFA goal date 2Q 2021",
"Primary and study completion expected Mar 1, 2021",
"Expected completion April 2021",
"Topline data 1Q2021, Study completion Jan 23, 2022",
"Phase III completion Mar 2023",
"Estimated completion Nov 30, 2021",
"Primary completion Oct 20, 2021, Study completion Sep 20, 2022",
"Primary completion Oct 2021, Study completion Nov 2022"]
tbl_cnt = [[c] for c in Catalyst]
fig, ax1 = plt.subplots()
width = 0.75 # the width of the bars
ind = np.arange(len(y)) # the x locations for the groups
ax1.barh(ind, y, width, color="green", align='edge')
ax1.set_yticks(ind+width/2)
ax1.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')
for bar, disease in zip(ax1.patches, Disease[::-1]):
ax1.text(0.1, bar.get_y()+bar.get_height()/2, disease, color = 'white', ha = 'left', va = 'center')
rtable = ax1.table(cellText=tbl_cnt, cellLoc='left', loc='right', bbox=[1.05,0,1.0,1], edges='horizontal')
ax1.text(1.3,0.9,'Catalyst', transform=fig.transFigure)
cellDict = rtable.get_celld()
for i in range(0,len(Catalyst)):
cellDict[(i,0)].set_height(0.11)
plt.show()