首页 > 其他 > 详细

横向柱状图

时间:2019-10-19 01:13:52      阅读:107      评论:0      收藏:0      [点我收藏+]

This example showcases a simple horizontal bar chart

import matplotlib.pyplot as plt
import numpy as np

# Fixing random state for reproducibility
np.random.seed(19680801)


plt.rcdefaults()
fig, ax = plt.subplots()

# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))

ax.barh(y_pos, performance, xerr=error, align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.invert_yaxis()  # labels read top-to-bottom
ax.set_xlabel('Performance')
ax.set_title('How fast do you want to go today?')

plt.show()

技术分享图片


np.random.seed()

使用seed(value)之后调用np.random.randn()np.random.rand()可以产生固定的随机值

# 使用seed设置随机种子
np.random.seed(19680801)
print(np.random.randn(2,5), end='\n\n')

# 不设置随机种子
print(np.random.randn(2,5), end='\n\n')

# 设置相同的随机种子
np.random.seed(19680801)
print(np.random.randn(2,5))
[[ 1.04930431  0.86609917  0.5511346   1.72807779 -0.64928812]
 [-0.47841958  1.07788511  0.96881033 -0.80934479 -1.2373891 ]]

[[ 1.86011654 -0.30831924  0.78297055  0.02152345  0.71566379]
 [ 1.40146651 -1.12750212  1.65581822  1.40096148  0.20199007]]

[[ 1.04930431  0.86609917  0.5511346   1.72807779 -0.64928812]
 [-0.47841958  1.07788511  0.96881033 -0.80934479 -1.2373891 ]]

plt.rcdefaults()

  • matplotlib配置信息是从配置文件中读取的。在配置文件中可以为matplotlib几乎所有的属性指定永久有效的默认值
  • 查看配置:你可以通过 matplotlib.rcParams字典访问所有已经加载的配置项
  • 可以通过 matplotlib.rcParams字典修改所有已经加载的配置项
  • 可以通过matplotlib.rc(*args,**kwargs)来修改配置项,其中args是你要修改的属性,kwargs是属性的关键字属性
  • 可以调用matplotlib.rcdefaults()将所有配置重置为标准设置
import matplotlib
print(matplotlib.rcParams['animation.embed_limit'])

matplotlib.rcParams['animation.embed_limit'] = 10
print(matplotlib.rcParams['animation.embed_limit'])

matplotlib.rc('animation', embed_limit=15)
print(matplotlib.rcParams['animation.embed_limit'])

matplotlib.rcdefaults()
print(matplotlib.rcParams['animation.embed_limit'])
20.0
10.0
15.0
20.0

pyplot.barh()

  • barh(y, width, height=0.8, left=None, *, align=‘center‘, **kwargs)
Parameters Meaning
y(标量或向量) 每个bar的y坐标
width(标量或向量) 每个bar的宽度
height 每个bar的高度
left(向量) bar左侧基坐标
align 可以是‘center‘或‘edge‘,表示bar和y轴ticks之间的位置关系
# Fixing random state for reproducibility
np.random.seed(19680801)

# plt.rcdefaults()
fig, ax = plt.subplots()

# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))

#使用height和left控制bar的宽度和左侧坐标,color设置bar的颜色,align设置bar和ytick的对齐方式
ax.barh(y_pos, performance, [0.5, 0.8, 0.3, 0.6, 0.2], [2, 3, 5, 4, 1], 
        xerr=error, align='edge', color="#84D096")
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.invert_yaxis()  # labels read top-to-bottom
ax.set_xlabel('Performance')
ax.set_title('How fast do you want to go today?')

plt.show()

技术分享图片

横向柱状图

原文:https://www.cnblogs.com/wndf6122/p/11701485.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!