横方向の棒グラフ

この例では、単純な横棒グラフを展示します

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)プロパティを変更したい引数の設定項目を、修正、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

Pyplotkbrh()

  • BARH(Y、幅、高さ= 0.8、左=なし、*、ALIGN = '中央'、** kwargsから)
パラメーター 意味
Y(スカラーまたはベクトル) yは各バーの座標
幅(スカラーまたはベクトル) 各バーの幅
高さ 各バーの高さ
左(ベクトル) バーイル左座標
合わせ これは、バーとy軸目盛りの位置関係を示し、「中央」または「エッジ」であってもよいです
# 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()

おすすめ

転載: www.cnblogs.com/wndf6122/p/11701485.html