我将 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()
关于python - 更新到版本 2.1.2 后,Matplotlib hatch 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48465795/