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?
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.
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.
The edgecolor parameter worked on mine.
plot.hist (bins = 10, xlim = [0.0,1], ylim = [0.0,70], color = 'blue', edgecolor = 'black')
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With