pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.71k stars 17.92k forks source link

Allow to set hatches in 'area' plots #12354

Open wavexx opened 8 years ago

wavexx commented 8 years ago

I'd like to be able to set the hatch styles (using a cycler) in plot(kind='area'). This would allow me to produce black&white plots correctly with more than two categories.

jreback commented 8 years ago

@TomAugspurger ?

TomAugspurger commented 8 years ago

@wavexx can you show an example of the matplotlib code needed to get the hatching on an area plot?

wavexx commented 8 years ago

I didn't check how area plots are made internally. It varies a bit depending on which call you're using, but most filling functions in mpl support the hatch or hatches kwarg.

http://matplotlib.org/examples/pylab_examples/contourf_hatching.html

It's also supported by fill_between (hatch).

https://stackoverflow.com/questions/18386106/matplotlib-hatched-fill-between-without-edges

I tried to set a cycler('hatch') as my default axes.prop_cycle, but it tries to set the hatch style for the lines as well (and this obviously fails).

dschien commented 7 years ago

@wavexx I am right that it currently is not possible at all to set hatches to area plots - not just using a cycler?

I am thinking that is the case because I tried the following without success:

ax = pd.plot(kind='area')
hatches=['.', '/', '\\', None, '\\\\', '*']
for i, a in enumerate(ax.collections):
    hatch = hatches[i % len(hatches)]
    a.set_hatch(hatch)

Thanks for any info

wavexx commented 7 years ago

As I wrote, you have to be careful to apply the style only to areas, but otherwise it will work:

pd.DataFrame([1,2,3]).plot(kind='area').collections[0].set_hatch('.')

Unfortunately this method is not scalable to anything but the simplest plot. For black&white plots I still have to use MPL directly

dschien commented 7 years ago

Ok. Thanks for the confirmation. Will try

dschien commented 7 years ago

@wavexx Sorry to be a pain but your one liner does not get hatches in jupyter notebook on OSX python 3.6.1 with the below packages.

Any ideas?

alabaster==0.7.10
appdirs==1.4.3
appnope==0.1.0
appscript==1.0.1
asn1crypto==0.22.0
attrs==17.2.0
Automat==0.6.0
awscli==1.11.87
Babel==2.4.0
bleach==2.0.0
botocore==1.5.57
certifi==2017.4.17
cffi==1.10.0
chardet==3.0.3
click==6.7
colorama==0.3.9
constantly==15.1.0
cryptography==1.9
cssselect==1.0.1
cycler==0.10.0
decorator==4.0.11
docutils==0.13.1
entrypoints==0.2.2
et-xmlfile==1.0.1
-e git+https://github.com/dschien/PyExcelModelingHelper.git@9c2a47db71b4a9b6fb1cad6a4d624f44a2f58c1f#egg=excel_modelling_helper
Flask==0.12.2
Flask-SocketIO==2.8.6
google-api-python-client==1.6.2
html5lib==0.999999999
httplib2==0.10.3
idna==2.5
imagesize==0.7.1
incremental==17.5.0
ipykernel==4.6.1
ipynb==0.4
ipython==6.0.0
ipython-genutils==0.2.0
ipywidgets==6.0.0
itsdangerous==0.24
jdcal==1.3
jedi==0.10.2
Jinja2==2.9.6
jmespath==0.9.3
jsonschema==2.6.0
jupyter==1.0.0
jupyter-client==5.0.1
jupyter-cms==0.7.0
jupyter-console==5.1.0
jupyter-core==4.3.0
lxml==3.7.3
MarkupSafe==1.0
matplotlib==2.0.2
mistune==0.7.4
mpmath==0.19
nbconvert==5.2.1
nbformat==4.3.0
networkx==1.11
nose==1.3.7
notebook==5.0.0
numpy==1.12.1
oauth2client==4.1.0
olefile==0.44
onedrivesdk==1.1.8
openpyxl==2.4.8
packaging==16.8
pandas==0.20.1
pandoc==1.0.0b2
pandocfilters==1.4.1
parsel==1.2.0
patsy==0.4.1
pbr==3.0.1
pexpect==4.2.1
pickleshare==0.7.4
Pillow==4.1.1
ply==3.10
prompt-toolkit==1.0.14
psutil==5.2.2
ptyprocess==0.5.1
py4j==0.10.4
pyasn1==0.2.3
pyasn1-modules==0.0.8
pycparser==2.17
PyDispatcher==2.0.5
pydot-ng==1.0.0
pydotplus==2.0.2
Pygments==2.2.0
pygraphviz==1.3.1
pyOpenSSL==17.0.0
pypandoc==1.4
pyparsing==2.2.0
PyQt5==5.8.2
pyspark==2.1.0+hadoop2.7
python-dateutil==2.6.0
python-engineio==1.5.4
python-socketio==1.7.5
pytz==2017.2
PyYAML==3.12
pyzmq==16.0.2
qtconsole==4.3.0
queuelib==1.4.2
requests==2.17.3
rsa==3.4.2
s3transfer==0.1.10
scipy==0.19.0
Scrapy==1.4.0
service-identity==17.0.0
simplegeneric==0.8.1
simplejson==3.10.0
sip==4.19.2
six==1.10.0
snowballstemmer==1.2.1
Sphinx==1.6.2
sphinxcontrib-websupport==1.0.1
statsmodels==0.8.0
stevedore==1.22.0
sympy==1.0
tabulate==0.7.7
terminado==0.6
testpath==0.3.1
tornado==4.5.1
traitlets==4.3.2
Twisted==17.1.0
tzlocal==1.4
uritemplate==3.0.0
urllib3==1.21.1
virtualenv==15.1.0
virtualenv-clone==0.2.6
virtualenvwrapper==4.7.2
w3lib==1.17.0
wcwidth==0.1.7
webencodings==0.5.1
Werkzeug==0.12.2
Whoosh==2.7.4
widgetsnbextension==2.0.0
xarray==0.9.5
xlrd==1.0.0
xlwings==0.10.4
xlwt==1.2.0
zope.interface==4.4.1
wavexx commented 7 years ago

On Fri, Jun 30 2017, Daniel Schien wrote:

@wavexx Sorry to be a pain but your one liner does not get hatches in python 3.6.1

I'm still on MPL 2.0.0 (python 3.5) if that makes a difference. Do you get an error, or you just don't get hatches?

dschien commented 7 years ago

Indeed on 3.5.1 with MPL 2.0.0 I do get hatches. It turns out the problem is MPL. I also get hatches on python 3.6.1 with MPL 2.0.0. However, on 3.6.1 with MPL 2.0.2 no hatches. No error. Just no hatches.

wavexx commented 7 years ago

On Fri, Jun 30 2017, Daniel Schien wrote:

Indeed on 3.5.1 with MPL 2.0.0 I do get hatches. It turns out the problem is MPL. I also get hatches on python 3.6.1 with MPL 2.0.0. However, on 3.6.1 with MPL 2.0.2 no hatches. No error. Just no hatches.

It might be that the returned collection just has a different order and the hatches are set on something which is not the main area.

I'm pretty sure setting hatches directly as a kwarg in any MPL plot commands works fine also in 2.0.2. The thing here is that there's no correct way from the collection to get back to the series you're plotting.

dschien commented 7 years ago

Alright.

So setting hatches directly on the area plot fails. If I do: pd.DataFrame([1,2,3]).plot(kind='area', hatches=['.', '/', '\\']) I get: AttributeError: Unknown property hatches

wavexx commented 7 years ago

On Fri, Jun 30 2017, Daniel Schien wrote:

pd.DataFrame([1,2,3]).plot(kind='area', hatches=['.', '/', '\']) I get: AttributeError: Unknown property hatches

Indeed, this is what this report is about ;) We need to relay hatches and handle the cycler properly.

vwither commented 5 years ago

Hi, I have the same problem and it seems like it would make sense for pd.DataFrame.plot() to take the option keyed argument hatch = [] like it does for colors, marker, etc. It has been two years since this was first reported and I have seen several hacks o how to get around this on GitHub but is anyone working on this.