MatPlotlib--Lines,bars and markers--Broken Barh破损条形图

本文介绍如何使用Matplotlib库中的broken_barh函数绘制破损条形图,并通过具体实例展示了如何设置条形颜色、坐标轴范围及标签等关键属性。

Broken Barh破损条形图

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.broken_barh([(110, 30), (150, 10)], (10, 9), facecolors='blue')
ax.broken_barh([(10, 50), (100, 20), (130, 10)], (20, 9),
               facecolors=('red', 'yellow', 'green'))
ax.set_ylim(5, 35)
ax.set_xlim(0, 200)
ax.set_xlabel('seconds since start')
ax.set_yticks([15, 25])
ax.set_yticklabels(['Bill', 'Jim'])
ax.grid(True)
ax.annotate('race interrupted', (61, 25),
            xytext=(0.8, 0.9), textcoords='axes fraction',
            arrowprops=dict(facecolor='black', shrink=0.05),
            fontsize=16,
            horizontalalignment='right', verticalalignment='top')

plt.show()

 

broken_barh:方括号里,小括号里,第一个数字是起点位置,第二个数字是长度。

非方括号的小括号:

facecolors:条形的颜色。

set_ylim:

set_xlim:x轴的长度?

set_xlabel:X轴的标签名称

set_yticks:

set_yticklabels:

grid

annotate

不存在:

Horizontal bar chart

等待:

Plotting categorical variables

Plotting the coherence

CSD Demo

Errorbar limit selection

Errorbar Subsample

EventCollection Demo

Eventplot Demo

Filled ploygon

Filling the area between

FillBetweenx Demo

Hatch-filled histograms

Bar chart with gradients

Discrete distribution as horizontal bar chart

Join styles and cap styles

Customizing dashed line styles

Linestyles

Marker filling-styles

Marker Reference

Markevery Demo

prop_cycle property markevery in reParams

Masked Demo

Multicolored lines

Nan Test

Psd Demo

Scatter Custom Symbol

Scatter Demo2

Scatter plot with histograms

Scatter Masked

Scatter plot with pie chart markers

Scatter Star Poly

Scatter Symbol

Scatter plots with legend

Simple Plot

Using span_where

Spectrum Representations

Stackplot Demo

Stem Plot

Step Demo

Creating a timeline with lines, dates and text

hlines and vlines

Cross- and Auto- Correlation Demo

ValueError Traceback (most recent call last) Cell In[129], line 7 3 coefficients = [-2.713712e-02,1.917585e-02,-1.619617e-02,-1.245247e-02,-1.196426e-02,-7.996985e-03,-7.859375e-03,6.864546e-03,6.558818e-03,-4.640525e-03, 4 -2.989272e-03,-2.384312e-03,-1.860149e-03,1.551288e-03,5.374756e-18] 6 fig, ax = plt.subplots(figsize=(10, 6)) ----> 7 ax.barh(features, coefficients, color='skyblue') 8 ax.set_xlabel('Coefficient Value') 9 ax.set_ylabel('Features') File D:\anaconda3\Lib\site-packages\matplotlib\axes\_axes.py:2812, in Axes.barh(self, y, width, height, left, align, data, **kwargs) 2693 r""" 2694 Make a horizontal bar plot. 2695 (...) 2809 :doc:`/gallery/lines_bars_and_markers/horizontal_barchart_distribution`. 2810 """ 2811 kwargs.setdefault('orientation', 'horizontal') -> 2812 patches = self.bar(x=left, height=height, width=width, bottom=y, 2813 align=align, data=data, **kwargs) 2814 return patches File D:\anaconda3\Lib\site-packages\matplotlib\__init__.py:1521, in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs) 1518 @functools.wraps(func) 1519 def inner(ax, *args, data=None, **kwargs): 1520 if data is None: -> 1521 return func( 1522 ax, 1523 *map(cbook.sanitize_sequence, args), 1524 **{k: cbook.sanitize_sequence(v) for k, v in kwargs.items()}) 1526 bound = new_sig.bind(ax, *args, **kwargs) 1527 auto_label = (bound.arguments.get(label_namer) 1528 or bound.kwargs.get(label_namer)) File D:\anaconda3\Lib\site-packages\matplotlib\axes\_axes.py:2572, in Axes.bar(self, x, height, width, bottom, align, **kwargs) 2569 if yerr is not None: 2570 yerr = self._convert_dx(yerr, y0, y, self.convert_yunits) -> 2572 x, height, width, y, linewidth, hatch = np.broadcast_arrays( 2573 # Make args iterable too. 2574 np.atleast_1d(x), height, width, y, linewidth, hatch) 2576 # Now that units have been converted, set the tick locations. 2577 if orientation == 'vertical': File D:\anaconda3\Lib\site-packages\numpy\lib\_stride_tricks_impl.py:558, in broadcast_arrays(subok, *args) 551 # nditer is not used here to avoid the limit of 32 arrays. 552 # Otherwise, something like the following one-liner would suffice: 553 # return np.nditer(args, flags=['multi_index', 'zerosize_ok'], 554 # order='C').itviews 556 args = [np.array(_m, copy=None, subok=subok) for _m in args] --> 558 shape = _broadcast_shape(*args) 560 result = [array if array.shape == shape 561 else _broadcast_to(array, shape, subok=subok, readonly=False) 562 for array in args] 563 return tuple(result) File D:\anaconda3\Lib\site-packages\numpy\lib\_stride_tricks_impl.py:433, in _broadcast_shape(*args) 428 """Returns the shape of the arrays that would result from broadcasting the 429 supplied arrays against each other. 430 """ 431 # use the old-iterator because np.nditer does not handle size 0 arrays 432 # consistently --> 433 b = np.broadcast(*args[:32]) 434 # unfortunately, it cannot handle 32 or more arguments directly 435 for pos in range(32, len(args), 31): 436 # ironically, np.broadcast does not properly handle np.broadcast 437 # objects (it treats them as scalars) 438 # use broadcasting to avoid allocating the full array ValueError: shape mismatch: objects cannot be broadcast to a single shape. Mismatch is between arg 2 with shape (15,) and arg 3 with shape (14,). D:\anaconda3\Lib\site-packages\IPython\core\events.py:82: UserWarning: Glyph 31070 (\N{CJK UNIFIED IDEOGRAPH-795E}) missing from font(s) DejaVu Sans. func(*args, **kwargs) D:\anaconda3\Lib\site-packages\IPython\core\events.py:82: UserWarning: Glyph 32463 (\N{CJK UNIFIED IDEOGRAPH-7ECF}) missing from font(s) DejaVu Sans. func(*args, **kwargs) D:\anaconda3\Lib\site-packages\IPython\core\events.py:82: UserWarning: Glyph 20256 (\N{CJK UNIFIED IDEOGRAPH-4F20}) missing from font(s) DejaVu Sans. func(*args, **kwargs) D:\anaconda3\Lib\site-packages\IPython\core\events.py:82: UserWarning: Glyph 23548 (\N{CJK UNIFIED IDEOGRAPH-5BFC}) missing from font(s) DejaVu Sans. func(*args, **kwargs) D:\anaconda3\Lib\site-packages\IPython\core\events.py:82: UserWarning: Glyph 24773 (\N{CJK UNIFIED IDEOGRAPH-60C5}) missing from font(s) DejaVu Sans. func(*args, **kwargs) D:\anaconda3\Lib\site-packages\IPython\core\events.py:82: UserWarning: Glyph 20917 (\N{CJK UNIFIED IDEOGRAPH-51B5}) missing from font(s) DejaVu Sans. func(*args, **kwargs) D:\anaconda3\Lib\site-packages\IPython\core\pylabtools.py:170: UserWarning: Glyph 31070 (\N{CJK UNIFIED IDEOGRAPH-795E}) missing from font(s) DejaVu Sans. fig.canvas.print_figure(bytes_io, **kw) D:\anaconda3\Lib\site-packages\IPython\core\pylabtools.py:170: UserWarning: Glyph 32463 (\N{CJK UNIFIED IDEOGRAPH-7ECF}) missing from font(s) DejaVu Sans. fig.canvas.print_figure(bytes_io, **kw) D:\anaconda3\Lib\site-packages\IPython\core\pylabtools.py:170: UserWarning: Glyph 20256 (\N{CJK UNIFIED IDEOGRAPH-4F20}) missing from font(s) DejaVu Sans. fig.canvas.print_figure(bytes_io, **kw) D:\anaconda3\Lib\site-packages\IPython\core\pylabtools.py:170: UserWarning: Glyph 23548 (\N{CJK UNIFIED IDEOGRAPH-5BFC}) missing from font(s) DejaVu Sans. fig.canvas.print_figure(bytes_io, **kw) D:\anaconda3\Lib\site-packages\IPython\core\pylabtools.py:170: UserWarning: Glyph 24773 (\N{CJK UNIFIED IDEOGRAPH-60C5}) missing from font(s) DejaVu Sans. fig.canvas.print_figure(bytes_io, **kw) D:\anaconda3\Lib\site-packages\IPython\core\pylabtools.py:170: UserWarning: Glyph 20917 (\N{CJK UNIFIED IDEOGRAPH-51B5}) missing from font(s) DejaVu Sans. fig.canvas.print_figure(bytes_io, **kw)
最新发布
08-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值