How to fill an area with only hatching (no background color) in matplotlib 2.0

With the latest updates matplotlib fill_betweenand hatch( see links here ), the areas filled with the line font are no longer the same as before. That is, the area is filled with color, and hatches are black or the area is not filled with color, and hatches are not visible.

Here is a side-by-side comparison of graphs from the same code ( from this answer )

import matplotlib.pyplot as plt
import matplotlib as mpl
plt.plot([0,1],[0,1],ls="--",c="b")
plt.fill_between([0,1],[0,1], color="none", hatch="X", edgecolor="b", linewidth=0.0)
plt.show()

enter image description here

Is there any way to reproduce the 1.X chart in 2.X? I'm not quite familiar with the back-end, but mpl.rcParams['hatch.color'] = 'b'keyword options color, edgecolordon't help either.

Thanks in advance for this.

+4
source share
2 answers

matplotlib > 2.0.1

GitHub hatching , , . p >

, , facecolor color.

import matplotlib.pyplot as plt

plt.plot([0,1],[0,1],ls="--",c="b")
plt.fill_between([0,1],[0,1], facecolor="none", hatch="X", edgecolor="b", linewidth=0.0)
plt.show()

enter image description here

matplotlib 2.0.0

, :
matplotlib 2.0.0 plt.style.use('classic').

##Classic!
import matplotlib.pyplot as plt
plt.style.use('classic')
plt.rcParams['hatch.color'] = 'b'

plt.plot([0,1],[0,1],ls="--",c="b")
plt.fill_between([0,1],[0,1], color="none", hatch="X", edgecolor="b", linewidth=0.0)

plt.show()

, none, .

## New
import matplotlib.pyplot as plt
plt.rcParams['hatch.color'] = 'b'

plt.plot([0,1],[0,1],ls="--",c="b")
plt.fill_between([0,1],[0,1], hatch="X", linewidth=0.0, alpha=0.0)

plt.show()

plt.rcParams['hatch.color'] = 'b'.

, matplotlib 2.0.0.
matplotlib, ,

API .

, github, API- ( 2.0.1).

+7

matplotlib 2.0.1 .

( ) - color = None, alpha = 0 fill_between. , , , . >

QuLogic, , facecolor = 'none', .

from matplotlib import pyplot as plt
import numpy as np

def plt_hist(axis, data, hatch, label):
    counts, edges = np.histogram(data, bins=int(len(data)**.5))
    edges = np.repeat(edges, 2)
    hist = np.hstack((0, np.repeat(counts, 2), 0))

    outline, = ax.plot(edges,hist,linewidth=1.3)        
    axis.fill_between(edges,hist,0,
                edgecolor=outline.get_color(), hatch = hatch, label=label, 
                facecolor = 'none')  ## < removes facecolor
    axis.set_ylim(0, None, auto = True)

h1 = '//'
d1 = np.random.rand(130)
lab1 = 'Rand1'
h2 = '\\\\'
d2 = np.random.rand(200)
lab2 = 'Rand2'

fig, ax = plt.subplots(1)

plt_hist(ax,d1,h1,lab1)
plt_hist(ax,d2,h2,lab2)
ax.legend()

example plot

+2

Source: https://habr.com/ru/post/1668222/


All Articles