我想創建一個水平條形圖,其中每個條形圖都有一個自定義的條紋顏色方案(類似于下圖)。我熟悉創建一個普通的柱狀圖,但我不知道雙色方案如何運作。
嘗試的代碼:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame({"P1_P2"/span>: [0.20]。
"P1_P3": [0.16]。
"P2_P5"。[0.12]。
"P3_P5": [0.06]。
"P3_P6": [0.06]。
"P5_P6"。[0.06]})
df=df.T
fig, ax = plt. subplots(dpi=600, figsize=(4, 4), nrows=1, ncols=1, facecolor=None, edgecolor='black' /span>)
df.plot.barh(ax=ax, position=0.50, width=0. 3, color=(245/255, 153/255, 145/255, 1. 0)
ax.get_legend().remove()
plt.show()
uj5u.com熱心網友回復:
這可以通過使用facecolor和edgecolor引數在barh中實作。我看到您在subplots中使用了以下引數。用下面的解決方案重新創建和制作了該圖:
%matplotlib
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame({"P1_P2"/span>: [0.20]。
"P1_P3": [0.16]。
"P2_P5"。[0.12]。
"P3_P5": [0.06]。
"P3_P6": [0.06]。
"P5_P6"。[0.06]})
df=df.T
fig, ax = plt. subplots(dpi=600, figsize=(4, 4), nrows=1, ncols=1)
df.plot.barh(ax=ax, position=0.50, width=0.3, facecolor="red",
edgecolor="blue")
ax.get_legend().remove()
plt.show()
為了在顏色之間有條紋,可以使用引數hatch,如下代碼中提到的那樣:
%matplotlib
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame({"P1_P2"/span>: [0.20]。
"P1_P3": [0.16]。
"P2_P5"。[0.12]。
"P3_P5": [0.06]。
"P3_P6": [0.06]。
"P5_P6"。[0.06]})
df=df.T
fig, ax = plt. subplots(dpi=600, figsize=(4, 4), nrows=1, ncols=1)
df.plot.barh(ax=ax, position=0.50, width=0.3, facecolor="red",
edgecolor="blue", hatch=r'//')
ax.get_legend().remove()
plt.show()
該圖將看起來像這樣:
如果你想增加或減少線寬,可以用plt.rcParams["hatch.lineewidth"]
plt.rcParams["hatch.lineewidth"]=4
uj5u.com熱心網友回復:
你可以用不同的顏色來孵化每個條形。線條的寬度需要根據特定的繪圖來調整。
由于孵化的顏色與輪廓的顏色相同,可以創建一個只顯示輪廓的條形圖副本。
importmatplotlib.pyplot as plt
from matplotlib import rcParams
import pandas as pd
from copy import copy
df = pd.DataFrame([0.2 , 0.16, 0。 12, 0.06, 0.06, 0。 06], index=['P1_P2'/span>, 'P1_P3'/span>, 'P2_P5'/span>, 'P3_P5', 'P3_P6', 'P5_P6'] )
first_colors = ['dodgerblue'/span>, 'orange'/span>, 'green'/span>, '銀色', '銀色', 'dugerblue']
second_colors = ['crimson', 'crimson', 'dodgerblue', 'dodgerblue'/span>, 'green', 'gold']
圖,ax = plt. subplots(dpi=600, figsize=(4, 4), nrows=1, ncols=1, facecolor=None, edgecolor='black' /span>)
df.plot.barh(ax=ax, position=0.50, width=0.3, legend=False, lineewidth=0)
rcParams['hatch.lineewidth'] = 4.
for bar, main_color, hatch_color in zip(ax.contains[0], first_colors, second_colors) 。
rect = copy(bar)
rect.set_facecolor('none')
rect.set_edgecolor('black')
rect.set_linewidth(2)
ax.add_patch(rect)
bar.set_facecolor(main_color)
bar.set_edgecolor(hatch_color)
bar.set_hatch('//')
ax.invert_yaxis()
plt.tight_layout()
plt.show()
PS:一個更簡單的版本來獲得輪廓,可以畫兩次條形的輪廓,然后更新第一次畫的條形的顏色和陰影(第一個條形存盤在ax.containers[0])。
for _ in range(2):
df.plot.barh(ax=ax, position=0.50, width=0. 3, legend=False, fc='none'/span>, ec='black', lw=2)
rcParams['hatch.lineewidth'] =4
for bar, main_color, hatch_color in zip(ax.contains[0], first_colors, second_colors) 。
bar.set_linewidth(0)
bar.set_facecolor(main_color)
bar.set_edgecolor(hatch_color)
bar.set_hatch('//')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/312397.html
標籤:


