我有一些堆叠的条形图,看起来可能像这样
Matplotlib是否有可能(在合理范围内)?
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
test1 = np.array([51, 13.8, 15.5, np.NaN])
test2 = np.array([40.3, 30.4, 13.8, 15.5])
df = pd.DataFrame(dict(test1 = test1,
test2 = test2),
columns = ["test1","test2"]).T
ax = df.plot.barh(stacked = True, cmap = "coolwarm", edgecolor = "black", lw = 1, width = 0.8, figsize = (6,4))
plt.show()
首先,条形图的边缘为黑色,因为您已将设置edgecolor = "black"
为df.plot.barh
。删除此意味着没有边色。您需要将每个条的edgecolor设置为bar facecolor的
您可以通过矩形补丁(使用迭代获得做到这一点ax.patches
),并使用edgecolor设置为facecolorget_facecolor()
和set_edgecolor()
。
test1 = np.array([51, 13.8, 15.5, np.NaN])
test2 = np.array([40.3, 30.4, 13.8, 15.5])
df = pd.DataFrame(dict(test1 = test1,
test2 = test2),
columns = ["test1","test2"]).T
ax = df.plot.barh(stacked = True, cmap = "coolwarm", lw = 1.5, width = 0.8, figsize = (6,4))
for rect in ax.patches:
facecolor = list(rect.get_facecolor())
rect.set_edgecolor(facecolor)
facecolor[-1] = 0.5 # reduce alpha value of facecolor, but not of edgecolor
rect.set_facecolor(facecolor)
plt.show()
这使:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句