matplotlib 堆叠条形图,条形从图表顶部和底部出现,并在中心收敛

gkn4icbw  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(100)

我正在使用matplotlib和pandas绘制与材料及其带隙相关的数据,我想以类似于下图的格式显示它。
x1c 0d1x的数据
到目前为止我的代码:

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
mpl.style.use('seaborn-white')

matls = ["Si", "Ge", "SiC", "GaN"]
VB = pd.DataFrame({'materials': matls, 'value': [-5.1, -4.6, -6.37, -7.57]})
CB = pd.DataFrame({'materials': matls, 'value': [-4.05, -4.0, -3.17, -4.1]})

fig, ax1 = plt.subplots()
ax1.set_ylabel("Potential relative to NHE (V)", color = 'C2' )
ax1.tick_params(axis='y', labelcolor='C2')
ax1.set_xlabel("Material")
ax1.bar(CB['materials'], CB['value'], bottom = 2., color = 'blue')

ax2 = ax1.twinx()
ax1.set_yticks(np.linspace(-4, 2, 13))
ax1.set_ylim(-4, 2)
ax2.set_yticks(np.linspace(-8.85, -2.85, 13))
ax2.set_ylim(-8.85, -2.85)
ax2.set_ylabel("Energy Relative to Vacuum (eV)", color = 'C5')
ax2.bar(VB['materials'], VB['value'], bottom = -8., color = 'green')
ax2.tick_params(axis='y', labelcolor='C5')
plt.show()

字符串



正如您所看到的,这些条只是从底部设置的任何值开始对齐,它们实际上并不反映数组中的真实的值。柱的高度由数组决定,因此Si的绿色柱应从-5.1开始并覆盖底部,蓝色柱应从-4.05开始并上升到图表的顶部。两条杠都错了。
编辑:
如果我更改这些值以匹配所使用的比例尺,则会得到以下结果。

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
mpl.style.use('seaborn-white')

matls = ["Si", "Ge", "SiC", "GaN"]
VB = pd.DataFrame({'materials': matls, 'value': [-5.1, -4.6, -6.37, -7.57]})
CB = pd.DataFrame({'materials': matls, 'value': [0.8, 0.85, 1.68, 0.75]})

fig, ax1 = plt.subplots()
ax1.set_ylabel("Potential relative to NHE (V)", color = 'C2' )
ax1.tick_params(axis='y', labelcolor='C2')
ax1.set_xlabel("Material")
ax1.bar(CB['materials'], CB['value'], bottom = 0., color = 'blue')

ax2 = ax1.twinx()
ax1.set_yticks(np.linspace(-4, 2, 13))
ax1.set_ylim(-4, 2)
ax2.set_yticks(np.linspace(-8.85, -2.85, 13))
ax2.set_ylim(-8.85, -2.85)
ax2.set_ylabel("Energy Relative to Vacuum (eV)", color = 'C5')
ax2.bar(VB['materials'], VB['value'], bottom = -8., color = 'green')
ax2.tick_params(axis='y', labelcolor='C5')
plt.show()


oxf4rvwz

oxf4rvwz1#

数据框的"value"列表示每个数据集的不同内容。对于蓝色数据集,它表示底部点,而对于绿色数据集,它表示顶部点。我们可以做的是为蓝色条设置一个顶部值,为绿色条设置一个底部值,然后根据这些值计算一个高度,得到所需的结果。

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

plt.close("all")

matls = ["Si", "Ge", "SiC", "GaN"]
VB = pd.DataFrame({"materials": matls, "value": [-5.1, -4.6, -6.37, -7.57]})
CB = pd.DataFrame({"materials": matls, "value": [-4.05, -4.0, -3.17, -4.1]})

VB_bottom = round(np.min(VB.value)-1)
CB_top = round(np.max(CB.value)+1)

VB["heights"] = VB_bottom - VB.value
CB["heights"] = CB_top - CB.value

fig, ax1 = plt.subplots()

ax1.set_ylabel("Potential relative to NHE (V)", color="blue")
ax1.tick_params(axis="y", labelcolor="blue")
ax1.set_xlabel("Material")
ax2 = ax1.twinx()
ax2.set_ylabel("Energy Relative to Vacuum (eV)", color="green")
ax2.tick_params(axis="y", labelcolor="green")
for ax in [ax1, ax2]:
    ax.set_ylim(VB_bottom, CB_top)

ax1.bar(CB.materials, CB.heights, bottom=CB.value, color="blue")
ax2.bar(VB.materials, VB.heights, bottom=VB.value, color="green")

fig.show()

字符串


的数据

相关问题