python绘制横向水平柱状条形图Bar,供大家参考,具体内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
import matplotlib import random import matplotlib.pyplot as plt # 中文乱码和坐标轴负号处理。 matplotlib.rc( 'font' , family = 'SimHei' , weight = 'bold' ) plt.rcParams[ 'axes.unicode_minus' ] = False #城市数据。 city_name = [ '北京' , '上海' , '广州' , '深圳' , '成都' ] #数组反转。 city_name.reverse() #装载随机数据。 data = [] for i in range ( len (city_name)): data.append(random.randint( 100 , 150 )) #绘图。 fig, ax = plt.subplots() b = ax.barh( range ( len (city_name)), data, color = '#6699CC' ) #为横向水平的柱图右侧添加数据标签。 for rect in b: w = rect.get_width() ax.text(w, rect.get_y() + rect.get_height() / 2 , '%d' % int (w), ha = 'left' , va = 'center' ) #设置Y轴纵坐标上的刻度线标签。 ax.set_yticks( range ( len (city_name))) ax.set_yticklabels(city_name) #不要X横坐标上的label标签。 plt.xticks(()) plt.title( '水平横向的柱状图' , loc = 'center' , fontsize = '25' , fontweight = 'bold' , color = 'red' ) plt.show() |
运行结果:
可以简单的绘制一个水平横向的柱状图:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import matplotlib.pyplot as plt import random import matplotlib matplotlib.rc( 'font' , family = 'SimHei' , weight = 'bold' ) city_name = [ '北京' , '上海' , '广州' , '深圳' , '成都' ] city_name.reverse() data = [] for i in range ( len (city_name)): data.append(random.randint( 100 , 200 )) colors = [ 'red' , 'yellow' , 'blue' , 'green' , 'gray' ] colors.reverse() plt.barh( range ( len (data)), data, tick_label = city_name, color = colors) #不要X横坐标标签。 #plt.xticks(()) plt.show() |
输出结果如图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/zhangphil/article/details/88390032