更新到 2.1.2 版后 Matplotlib 孵化不起作用

Matplotlib hatch does not work after update to version 2.1.2

我将 matplotlib 从版本 2.0.0 更新到 2.1.2,我的影线不再显示。 为了方便起见,我采用了我在上一个问题 (Axis label hidden by axis in plot?)

中发布的相同示例

如果我 运行 在我的 python 环境 (python 3.5.2) 中使用旧版本的 matloblib 中的这段代码,它会显示阴影线,在升级 matplotlib 后它会显示不是。我怎样才能仍然显示影线?

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

data = random.sample(range(100), 5)
data[0] = 100
data[3] = 50
index = ['industry', 'residential', 'agriculture', 'transport', 'other']
df1 = pd.DataFrame(data, index=index, columns=['data'])
df2 = pd.DataFrame(np.array(data)/2, index=index, columns=['data'])

fig = plt.figure()

ax = fig.add_subplot(111, projection="polar")

ax.grid(True)
ax.yaxis.grid(color='r')  
ax.xaxis.grid(color='#dddddd')  

for spine in ax.spines.values():
    spine.set_edgecolor('None')

theta = np.arange(len(df1))/float(len(df1))*2.*np.pi

l1, = ax.plot(theta, df1["data"], color="gold", marker="o", label=None, zorder=1)  # , zorder = -3)
l2, = ax.plot(theta, df2["data"], color='tomato', marker="o", label=None, zorder=1.1)  #, zorder =-2)

def _closeline(line):
    x, y = line.get_data()
    x = np.concatenate((x, [x[0]]))
    y = np.concatenate((y, [y[0]]))
    line.set_data(x, y)
[_closeline(l) for l in [l1, l2]]

mpl.rcParams['hatch.color'] = 'red'
ax.fill(theta, df1["data"], edgecolor="gold", alpha=1, color = 'None', zorder=1)
ax.fill(theta, df2["data"], edgecolor='tomato', hatch='///', color = 'None', zorder=2)

ax.set_rlabel_position(216)
ax.set_xticks(theta)
ax.set_xticklabels(df2.index, fontsize=12)#, zorder=1)
for it in np.arange(len(theta)):   
    txt = ax.text(theta[it], 100*1.1, index[it], va = 'center', ha = 'center', fontsize = 12)

ax.set_xticklabels('')
legend = plt.legend(handles=[l1,l2], labels =['first','second'], loc='lower right')

plt.title("data [unit]", fontsize = 16, y = 1.2)

plt.show()

要在 matplotlib 中获得没有背景的阴影区域,您可以设置 fill=False

ax.fill(..., hatch='///', edgecolor="gold", fill=False)

完整示例:

import matplotlib.pyplot as plt
import numpy as np

a = np.array([[0, 0], [1, 0.2], [1, 1.2], [0, 1.]])

fig, ax = plt.subplots()

ax.fill(a[:,0],a[:,1], hatch='///', edgecolor="gold", alpha=1, fill=False)

plt.show()