機械学習-Pythonの基本2、Matplotlibチュートリアル

2、Matplotlibチュートリアル

1はじめに

Matplotlibは、Python2Dプロットの分野でおそらく最も広く使用されているパッケージです。これにより、ユーザーはデータを簡単にグラフ化し、さまざまな出力形式を提供できます。ここでは、matplotlibの一般的な使用法について説明します。

pylabは、matplotlibのオブジェクト指向描画ライブラリのインターフェイスです。その構文はMatlabと非常によく似ています。つまり、そのメインの描画コマンドとMatlabの対応するコマンドには同様のパラメーターがあります。

2.Pythonで使用する

mac osXシステムの場合、2行目を追加する必要がありますそうしないと、エラーが報告されます。
RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
Pythonのバージョンは3.0に設定する必要があります

https://blog.csdn.net/joey_ro/article/details/109101392

中国語フォントを導入する必要があります。そうしないと、エラーが報告されます

Matplotlibはデフォルトで中国語をサポートしていません。次の簡単な方法で解決できます。

ここでは、AdobeとGoogleが立ち上げたオープンソースフォントであるSiyuanHeidiを使用します。

公式ウェブサイト:https://source.typekit.com/source-han-serif/cn/

GitHubアドレス:https://github.com/adobe-fonts/source-han-sans/tree/release/OTF/SimplifiedChinese

ネットワークディスクからダウンロードすることもできます:https://pan.baidu.com/s/14cRhgYvvYotVIFkRVd71fQ抽出コード:e15r

SourceHanSansSC-Bold.otfなどのOTFフォントをダウンロードして、現在実行中のコードファイルにファイルを配置できます。

SourceHanSansSC-Bold.otfファイルは、現在実行中のコードファイルに配置されます。

# fname 为 你下载的字体库路径,注意 SourceHanSansSC-Bold.otf 字体的路径
zhfont1 = matplotlib.font_manager.FontProperties(fname="SourceHanSansSC-Bold.otf")

3.いくつかの重要な機能

1)サブプロットはグラフ上に複数の関数を表示します
# 计算正弦和余弦曲线上的点的 x 和 y 坐标
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
# 建立 subplot 网格,高为 2,宽为 1
# 激活第一个 subplot
plt.subplot(2, 1, 1)
# 绘制第一个图像
plt.plot(x, y_sin)
plt.title('正弦函数', fontproperties=zhfont1)
# 将第二个 subplot 激活,并绘制第二个图像
plt.subplot(2, 1, 2)
plt.plot(x, y_cos)
plt.title('余弦函数', fontproperties=zhfont1)
# 展示图像
plt.show()

2)棒グラフ
# bar函数生成条形图
x = [5, 8, 10]
y = [12, 16, 6]
x2 = [6, 9, 11]
y2 = [6, 15, 7]
plt.bar(x, y, align='center')
plt.bar(x2, y2, color='g', align='center')
plt.title('Bar graph')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
3)度数分布図

ビンは限界です

data = np.array(
    [1, 233, 45, 545, 543, 5, 34534, 5, 345, 34, 5, 34, 53, 45, 34, 5, 34, 5, 345, 3, 45, 34, 5, 34, 534, 5, 34])
plt.hist(data, bins=[0, 20, 40, 100])
plt.show()

おすすめ

転載: blog.csdn.net/joey_ro/article/details/109102230