Search code examples
pythonmatplotlibradar-chart

Matplotlib hatch does not work after update to version 2.1.2


I updated matplotlib from version 2.0.0 to 2.1.2 and my hatches do not display anymore. For convenience I take the same example I posted in my previous question (Axis label hidden by axis in plot?)

If I run this code in my python environment (python 3.5.2) with the older version of matloblib it displays the hatch, after the upgrade of matplotlib it does not. How can I still display the hatch?

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

Solution

  • To get a hatched area in matplotlib with no background, you may set fill=False.

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

    Complete example:

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

    enter image description here