1

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()
esperluette
  • 1,262
  • 2
  • 10
  • 18
  • Can you clearly state what the desired outcome is, or how it did look in the previous version compared to how it looks now? There are some colors red, gold, "None" involved, but which part should be colororized how? – ImportanceOfBeingErnest Jan 27 '18 at 11:52
  • There should be a hatched filling inside the inner figure (which displays fine with the old version of matplolib, without changing my code) – esperluette Jan 27 '18 at 13:16
  • I understand that. But what I'm asking is how exactly do you want the patch to look like (there seems to be some contradiction between edgecolor and "hatch.color"). Just describe how it should look like independed of the fact that some code produced some other result in some previous version. – ImportanceOfBeingErnest Jan 27 '18 at 13:25
  • I will post a picture on Monday. But I should see slanted red lines in the centre figure.. Sort of like this /// – esperluette Jan 27 '18 at 15:37

1 Answers1

1

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

ImportanceOfBeingErnest
  • 251,038
  • 37
  • 461
  • 518
  • Cheers! it worked, I changed the second fill to `ax.fill(theta, df2["data"], hatch='///', alpha=1, edgecolor = 'tomato', fill = False, zorder=2)` – esperluette Jan 29 '18 at 08:55