私が線の線幅を変更すると、凡例で自動的に更新されることがわかります。 しかし、プロットに影響を与えずに凡例の線幅を変更したいだけです。ここでmatplotlib内の凡例行の線幅を増やす
32
A
答えて
44
はそれを行う方法の簡単な例です:
import numpy as np
import matplotlib.pyplot as plt
# make some data
x = np.linspace(0, 2*np.pi)
y1 = np.sin(x)
y2 = np.cos(x)
# plot sin(x) and cos(x)
p1 = plt.plot(x, y1, 'b-', linewidth=1.0)
p2 = plt.plot(x, y2, 'r-', linewidth=1.0)
# make a legend for both plots
leg = plt.legend([p1, p2], ['sin(x)', 'cos(x)'], loc=1)
# set the linewidth of each legend object
for legobj in leg.legendHandles:
legobj.set_linewidth(2.0)
plt.show()
3
あなたはプロット内のすべての行を変更したい場合は、あなた自身の伝説ハンドラを定義すると便利かもしれません:
import matplotlib.pyplot as plt
from matplotlib import legend_handler
from matplotlib.lines import Line2D
import numpy as np
class MyHandlerLine2D(legend_handler.HandlerLine2D):
def create_artists(self, legend, orig_handle,
xdescent, ydescent, width, height, fontsize,
trans):
xdata, xdata_marker = self.get_xdata(legend, xdescent, ydescent,
width, height, fontsize)
ydata = ((height-ydescent)/2.)*np.ones(xdata.shape, float)
legline = Line2D(xdata, ydata)
self.update_prop(legline, orig_handle, legend)
#legline.update_from(orig_handle)
#legend._set_artist_props(legline) # after update
#legline.set_clip_box(None)
#legline.set_clip_path(None)
legline.set_drawstyle('default')
legline.set_marker("")
legline.set_linewidth(10)
legline_marker = Line2D(xdata_marker, ydata[:len(xdata_marker)])
self.update_prop(legline_marker, orig_handle, legend)
#legline_marker.update_from(orig_handle)
#legend._set_artist_props(legline_marker)
#legline_marker.set_clip_box(None)
#legline_marker.set_clip_path(None)
legline_marker.set_linestyle('None')
if legend.markerscale != 1:
newsz = legline_marker.get_markersize()*legend.markerscale
legline_marker.set_markersize(newsz)
# we don't want to add this to the return list because
# the texts and handles are assumed to be in one-to-one
# correpondence.
legline._legmarker = legline_marker
return [legline, legline_marker]
plt.plot([0, 1], [0, 1], '-r', lw=1, label='Line')
plt.legend(handler_map={Line2D:MyHandlerLine2D()})
plt.show()
2
@Brendan Woodのメソッドはpyplot
によって提供されるAPIを使用します。 matplotlibでは、object oriented style using axes is preferedです。以下は、axes
メソッドを使用してこれを実現する方法です。生産プロットを以下に示し
import numpy as np
import matplotlib.pyplot as plt
# make some data
x = np.linspace(0, 2*np.pi)
y1 = np.sin(x)
y2 = np.cos(x)
fig, ax = plt.subplots()
ax.plot(x, y1, linewidth=1.0, label='sin(x)')
ax.plot(x, y2, linewidth=1.0, label='cos(x)')
leg = ax.legend()
for line in leg.get_lines():
line.set_linewidth(4.0)
plt.show()
1
は、伝説はラインそのものが含まれています。したがって、キャンバス内の線の線幅を変更すると、凡例内の線も変更されます(その逆も同様です)。
可能な解決策は、キャンバスからアーティストのコピーを使用し、コピーの線幅のみを変更することです。
import numpy as np
import matplotlib.pyplot as plt
import copy
x = np.linspace(0, 2*np.pi)
y1 = np.sin(x)
y2 = np.cos(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y1, c='b', label='y1',linewidth=1.0)
ax.plot(x, y2, c='r', label='y2')
# obtain the handles and labels from the figure
handles, labels = ax.get_legend_handles_labels()
# copy the handles
handles = [copy.copy(ha) for ha in handles ]
# set the linewidths to the copies
[ha.set_linewidth(7) for ha in handles ]
# put the copies into the legend
leg = plt.legend(handles=handles, labels=labels)
plt.savefig('leg_example')
plt.show()
異なるオプションがhandler_map
と更新機能を使用することです。これは何とか自動で行われ、ハンドラマップが自動的に凡例内の任意の線を7ポイント幅にすることを指定します。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.legend_handler import HandlerLine2D
x = np.linspace(0, 2*np.pi)
y1 = np.sin(x)
y2 = np.cos(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y1, c='b', label='y1',linewidth=1.0)
ax.plot(x, y2, c='r', label='y2')
linewidth=7
def update(handle, orig):
handle.update_from(orig)
handle.set_linewidth(7)
plt.legend(handler_map={plt.Line2D : HandlerLine2D(update_func=update)})
plt.show()
結果は上記と同じである。
関連する問題
- 1. matplotlib内の凡例フレームの線幅を指定する
- 2. ループ内のMatplotlibの凡例?
- 3. Matplotlib:凡例の水平線長
- 4. matplotlib - 別個のサブプロット内の凡例
- 5. gnuplotは - キー/のみ凡例にポイントのサイズを増やし
- 6. matplotlibの凡例ボックス内のマーカーのサイズ/アルファ値を変更
- 7. ggplot2:凡例の点線
- 8. matplotlibの凡例がダブルエラーバーを表示
- 9. Matplotlibの凡例のテキストの整列
- 10. ggplot2凡例の幅と凡例の文字列のサイズ
- 11. matplotlibでのラテックス引用凡例
- 12. divタグの幅を右から左に増やす/増やす
- 13. matplotlib csvタイトル行からライングラフへの凡例の追加
- 14. ドロップダウンアイテムの幅のみを増やす
- 15. ホバーでボタンの幅を数%増やす
- 16. Matplotlib:行の長さに線幅が追加される
- 17. 凡例をmatplotlibのスキャッタポイントの色に対応させます
- 18. matplotlib:凡例を他のサブプロットの上に表示します。
- 19. Matplotlib軸凡例のフォントサイズを設定するには?
- 20. matplotlib凡例ボックスの名前を変更するには?
- 21. Pythonのmatplotlibプロットに凡例を動的に追加する
- 22. Pythonのmatplotlibのサブプロットテキストフィールドのためのスペースを増やすには?
- 23. matplotlib 1凡例のエントリが多すぎます
- 24. 凡例内の行数を制御する
- 25. matplotlib:同じグラフの2つの異なる凡例
- 26. matplotlibのエラーバーの線幅を設定する方法は?
- 27. matplotlibのハッチングの線幅を操作する
- 28. Python Seaborn Matplotlibは凡例としてラインスタイルを設定します
- 29. matplotlib PDFを保存するときの線幅
- 30. パス内のタッチポイントの数を増やす
いいえ、私はこれを早く探していました。 ] – Alnitak
@Alnitak私はこの回答を別のオプションで更新しました。 – ImportanceOfBeingErnest