Python
 Computer >> コンピューター >  >> プログラミング >> Python

Pythonで複数のグラフを組み合わせる方法


はじめに

Matplotlibを使用すると、同じグラフに複数のプロットを追加できます。このチュートリアルでは、2つの異なる軸で同じプロットにデータを表示する方法を示します。

その方法..

1. pythonコマンドプロンプトを開き、pip install matplotlibを起動して、matplotlibをインストールします。

import matplotlib.pyplot as plt

2.表示するデータを準備します。

import matplotlib.pyplot as plt

# data prep (I made up data no accuracy in these stats)
mobile = ['Iphone','Galaxy','Pixel']

# Data for the mobile units sold for 4 Quaters in Million
units_sold = (('2016',12,8,6),
('2017',14,10,7),
('2018',16,12,8),
('2019',18,14,10),
('2020',20,16,5),)

3.データを各企業のモバイルユニットのアレイに分割します。

# data prep - splitting the data
Years, IPhone_Sales, Galaxy_Sales, Pixel_Sales = zip(*units_sold)

# set the position
Position = list(range(len(units_sold)))

# set the width
Width = 0.2

4.最初のサブプロットを作成します。

plt.subplot(2, 1, 1)
<matplotlib.axes._subplots.AxesSubplot at 0x214185d4e50>

Pythonで複数のグラフを組み合わせる方法

5.IPhone_Salesに関する情報を含む棒グラフを作成します。

Iphone = plt.bar(Position, IPhone_Sales,color='green')
plt.ylabel('IPhone Sales')
plt.xticks(Position, Years)


([<matplotlib.axis.XTick at 0x214186115e0>,
<matplotlib.axis.XTick at 0x21418611580>,
<matplotlib.axis.XTick at 0x2141861fc40>,
<matplotlib.axis.XTick at 0x21418654e20>,
<matplotlib.axis.XTick at 0x2141865f370>],
[Text(0, 0, '2016'),
Text(0, 0, '2017'),
Text(0, 0, '2018'),
Text(0, 0, '2019'),
Text(0, 0, '2020')])

Pythonで複数のグラフを組み合わせる方法

6.次に、別のy軸を作成して、SamsungGalaxyの売上に関する情報を追加します。

plt.twinx()
Galaxy = plt.plot(Position, Galaxy_Sales, 'o-', color='blue')
plt.ylabel('Galaxy Sales')
plt.xticks(Position, Years)


([<matplotlib.axis.XTick at 0x214186b4c40>,
<matplotlib.axis.XTick at 0x214186b4c10>,
<matplotlib.axis.XTick at 0x21418682ac0>,
<matplotlib.axis.XTick at 0x214186dd8e0>,
<matplotlib.axis.XTick at 0x214186dddf0>],
[Text(0, 0, '2016'),
Text(0, 0, '2017'),
Text(0, 0, '2018'),
Text(0, 0, '2019'),
Text(0, 0, '2020')])

Pythonで複数のグラフを組み合わせる方法

7.最終的なGooglePixelの売上をプロットします。

plt.subplot(2, 1, 2)
plt.plot(Position, Pixel_Sales, color='yellow')
plt.gca().set_ylim(ymin=0)
plt.xticks(Position, Years)
>


([<matplotlib.axis.XTick at 0x2141870f9a0>,
<matplotlib.axis.XTick at 0x2141870f580>,
<matplotlib.axis.XTick at 0x2141870a730>,
<matplotlib.axis.XTick at 0x2141873c9d0>,
<matplotlib.axis.XTick at 0x2141873cee0>],
[Text(0, 0, '2016'),
Text(0, 0, '2017'),
Text(0, 0, '2018'),
Text(0, 0, '2019'),
Text(0, 0, '2020')])


Pythonで複数のグラフを組み合わせる方法

plt.show()

8.まとめてグラフを保存します。

import matplotlib.pyplot as plt

# data prep (I made up data no accuracy in these stats)
mobile = ['Iphone','Galaxy','Pixel']

# Data for the mobile units sold for 4 Quaters in Million
units_sold = (('2016',12,8,6),
('2017',14,10,7),
('2018',16,12,8),
('2019',18,14,10),
('2020',20,16,5),)
# data prep - splitting the data
Years, IPhone_Sales, Galaxy_Sales, Pixel_Sales = zip(*units_sold)

# set the position
Position = list(range(len(units_sold)))

# set the width
Width = 0.2

plt.subplot(2, 1, 1)
Iphone = plt.bar(Position, IPhone_Sales,color='green')
plt.ylabel('IPhone Sales')
plt.xticks(Position, Years)

plt.twinx()
Galaxy = plt.plot(Position, Galaxy_Sales, 'o-', color='blue')
plt.ylabel('Galaxy Sales')
plt.xticks(Position, Years)

plt.subplot(2, 1, 2)
plt.plot(Position, Pixel_Sales, color='yellow')
plt.ylabel('Pixel Sales')
plt.gca().set_ylim(ymin=0)
plt.xticks(Position, Years)

# plt.show()
plt.savefig('CombiningGraphs.png', dpi=72)


Pythonで複数のグラフを組み合わせる方法


  1. Pythonでログヒストグラムを作成するにはどうすればよいですか?

    ログヒストグラムを作成するには、 log =Trueを使用できます。 hist()の引数で メソッド。 ステップ 番号のリストを作成します。 density =Trueでヒストグラムをプロットします 。 図を表示するには、 show()を使用します メソッド。 例 import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = Tr

  2. Pythonで時系列をプロットする方法は?

    matplotlibを使用してPythonで時系列をプロットするには、次の手順を実行できます- numpyを使用してxポイントとyポイントを作成します。 plot()を使用して、作成されたxポイントとyポイントをプロットします。 メソッド。 図を表示するには、 show()を使用します メソッド。 例 import matplotlib.pyplot as plt import datetime import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams[