Python-data-science-python-chart-styling

提供:Dev Guides
移動先:案内検索

Python-チャートスタイリング

Pythonで作成されたチャートは、チャート作成に使用されるライブラリの適切なメソッドを使用して、さらにスタイルを設定できます。 このレッスンでは、注釈、凡例、グラフ背景の実装について説明します。 前の章のコードを引き続き使用し、これらのスタイルをチャートに追加するように修正します。

注釈を追加する

多くの場合、チャートの特定の場所を強調表示して、チャートに注釈を付ける必要があります。 以下の例では、それらのポイントに注釈を追加することにより、チャートの値の急激な変化を示しています。

import numpy as np
from matplotlib import pyplot as plt

x = np.arange(0,10)
y = x ^ 2
z = x ^ 3
t = x ^ 4
# Labeling the Axes and Title
plt.title("Graph Drawing")
plt.xlabel("Time")
plt.ylabel("Distance")
plt.plot(x,y)

#Annotate
plt.annotate(xy=[2,1], s='Second Entry')
plt.annotate(xy=[4,6], s='Third Entry')

その*出力*は次のとおりです-

chartstyle1.png

凡例を追加する

複数の線をプロットするチャートが必要になる場合があります。 凡例の使用は、各行に関連付けられた意味を表します。 次のグラフには、適切な凡例が3行あります。

import numpy as np
from matplotlib import pyplot as plt

x = np.arange(0,10)
y = x ^ 2
z = x ^ 3
t = x ^ 4
# Labeling the Axes and Title
plt.title("Graph Drawing")
plt.xlabel("Time")
plt.ylabel("Distance")
plt.plot(x,y)

#Annotate
plt.annotate(xy=[2,1], s='Second Entry')
plt.annotate(xy=[4,6], s='Third Entry')
# Adding Legends
plt.plot(x,z)
plt.plot(x,t)
plt.legend(['Race1', 'Race2','Race3'], loc=4)

その*出力*は次のとおりです-

chartstyle2.png

チャートプレゼンテーションスタイル

スタイルパッケージのさまざまなメソッドを使用して、グラフの表示スタイルを変更できます。

import numpy as np
from matplotlib import pyplot as plt

x = np.arange(0,10)
y = x ^ 2
z = x ^ 3
t = x ^ 4
# Labeling the Axes and Title
plt.title("Graph Drawing")
plt.xlabel("Time")
plt.ylabel("Distance")
plt.plot(x,y)

#Annotate
plt.annotate(xy=[2,1], s='Second Entry')
plt.annotate(xy=[4,6], s='Third Entry')
# Adding Legends
plt.plot(x,z)
plt.plot(x,t)
plt.legend(['Race1', 'Race2','Race3'], loc=4)

#Style the background
plt.style.use('fast')
plt.plot(x,z)

その*出力*は次のとおりです-

chartstyle3.png