Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

histogram graph line style in matplotlib

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?

like image 770
wdg Avatar asked Jan 23 '14 13:01

wdg


People also ask

How do I make my Matplotlib histogram look better?

We can achieve this by increasing the number of bins, which is essentially the number of classes the histogram divides the data into. More bins will make the histogram smoother.

How do I insert a horizontal line in Matplotlib?

The axhline() function in pyplot module of matplotlib library is used to add a horizontal line across the axis. Parameters: y: Position on Y axis to plot the line, It accepts integers. xmin and xmax: scalar, optional, default: 0/1.


2 Answers

The edgecolor parameter worked on mine.

plot.hist (bins = 10, xlim = [0.0,1], ylim = [0.0,70], color = 'blue', edgecolor = 'black')
like image 39
Ernandes Junior Avatar answered Oct 14 '22 13:10

Ernandes Junior


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.

like image 152
gfritz Avatar answered Oct 14 '22 14:10

gfritz