更新到版本2.1.2后,Matplotlib填充不起作用

时间:2018-01-26 16:47:03

标签: python matplotlib radar-chart

我将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()

1 个答案:

答案 0 :(得分:1)

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

fill=False

完整示例:

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

enter image description here