10

I need to plot two histograms in the same figure and there is overlapping. I use command like

plt.hist(data1,bins=40,normed=True,histtype='step')
plt.hist(data2,bins=40,normed=True,histtype='step')

To distinguish these two different histograms (need to present them in black and white), I want to make one of them appear in dashed line instead of solid line, so I tried

plt.hist(data1,bins=40,normed=True,histtype='step',ls='--')

which gave me the following error message:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.py", line 1475, in __call__
    return self.func(*args)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.py", line 534, in callit
    func(*args)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/backends/backend_tkagg.py", line 363, in idle_draw
    self.draw()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/backends/backend_tkagg.py", line 348, in draw
    FigureCanvasAgg.draw(self)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/backends/backend_agg.py", line 451, in draw
    self.figure.draw(self.renderer)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/artist.py", line 56, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/figure.py", line 1035, in draw
    func(*args)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/artist.py", line 56, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/axes.py", line 2088, in draw
    a.draw(renderer)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/artist.py", line 56, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/patches.py", line 401, in draw
    gc.set_linestyle(self._linestyle)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/backend_bases.py", line 962, in set_linestyle
    raise ValueError('Unrecognized linestyle: %s' % str(style))
ValueError: Unrecognized linestyle: --

My question is, how can I change the line style (solid/dashed and color)? Or is there an alternative way to plot these two histograms with desired line styles?

wdg
  • 1,687
  • 5
  • 17
  • 29

2 Answers2

16

Have you imported all the libraries you need? Also, sometimes not all linestyles are available to all plot types. There are linestyles that work for plots that do not work on vectors (even though they look like they should), for example. When the symbol name does not work '--' it is a good idea to try the named version 'dashed'.

You can provide a tuple of linestyles (or colors, widths, etc.) in the plot argument much like how it is done for linewidths on this example from the matplotlib docs (Ctrl+F for linewidths)

Using your plot command, it should look like:

plt.hist(data1,bins=40,normed=True,histtype='step',linestyle=('solid','dashed'))

There is a color argument you can specify just like how linestyle was done. When the lines are plotted, pyplot looks at the first item in each tuple you provide. So if you wanted a solid black line and a dashed yellow line it would look like

plt.hist(data1,bins=40,normed=True,histtype='step',linestyle=('solid','dashed'),color=('black','k'))

So 'solid' should pair with 'black' and 'dashed' should pair with 'k'. This should work for any other line properties you want to use.

marisano
  • 571
  • 6
  • 10
gfritz
  • 559
  • 9
  • 16
  • 1
    setting `linestyle=('dashed')` does work. Since you mentioned about the color, how can I set the color in this way? I tried `linestyle=('dahsed','black')` and `linestyle=('dashed','k')` but they didn't work. – wdg Jan 23 '14 at 14:29
  • Tried to comment it down here, decided to update answer instead. – gfritz Jan 23 '14 at 15:05
  • I think you can structure your answer better (like writing separate commands for `data1` and `data2`), but I accepted your answer anyway. – wdg Jan 23 '14 at 15:36
2

The edgecolor parameter worked on mine.

plot.hist (bins = 10, xlim = [0.0,1], ylim = [0.0,70], color = 'blue', edgecolor = 'black')
joelostblom
  • 43,590
  • 17
  • 150
  • 159