Python matplotlib 中的直方图线条样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21309735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:33:31  来源:igfitidea点击:

histogram graph line style in matplotlib

pythonmatplotlibplothistogram

提问by wdg

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?

我的问题是,如何更改线条样式(实线/虚线和颜色)?或者是否有另一种方法来绘制这两个具有所需线条样式的直方图?

采纳答案by gfritz

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'.

您是否导入了所需的所有库?此外,有时并非所有线型都适用于所有绘图类型。例如,有些线型适用于对矢量不起作用的图(即使它们看起来应该如此)。当符号名称不起作用“--”时,最好尝试命名版本“dashed”。

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

您可以在 plot 参数中提供一个线型(或颜色、宽度等)元组,就像在 matplotlib 文档中的linewidths这个例子中所做的一样(Ctrl+F 表示线宽)

Using your plot command, it should look like:

使用您的绘图命令,它应该如下所示:

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

There is a colorargument you can specify just like how linestylewas 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

color您可以指定一个参数,就像如何linestyle完成一样。绘制线条时,pyplot 会查看您提供的每个元组中的第一项。所以如果你想要一条黑色实线和一条黄色虚线,它看起来像

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.

所以“solid”应该与“black”配对,“dashed”应该与“k”配对。这应该适用于您要使用的任何其他线属性。

回答by Ernandes Junior

The edgeColorparameter worked on mine.

edgeColor参数的工作对矿井。

plot.hist (bins = 10, xlim = [0.0,1], ylim = [0.0,70], color = 'blue', edgeColor = 'black')