68

半透明の 'x' マーカー (20% アルファ) を使用したプロットに取り組んでいます。凡例でマーカーを 100% の不透明度で表示するにはどうすればよいですか?

import matplotlib.pyplot as plt
plt.plot_date( x = xaxis, y = yaxis, marker = 'x', color=[1, 0, 0, .2], label='Data Series' )
plt.legend(loc=3, mode="expand", numpoints=1, scatterpoints=1 )
4

8 に答える 8

9

宇宙の答えに続いて、凡例の「偽の」行をプロット上で非表示にするために、NaNを使用できますが、それらは引き続き凡例エントリの生成に機能します:

import numpy as np
import matplotlib.pyplot as plt
# Plot data with alpha=0.2
plt.plot((0,1), (0,1), marker = 'x', color=[1, 0, 0, .2])
# Plot non-displayed NaN line for legend, leave alpha at default of 1.0
legend_line_1 = plt.plot( np.NaN, np.NaN, marker = 'x', color=[1, 0, 0], label='Data Series' )
plt.legend()
于 2014-12-03T22:52:55.543 に答える
3

凡例に特定のものを入れたい場合は、適切なテキストを使用して凡例に配置するオブジェクトを定義する方が簡単です。例えば:

import matplotlib.pyplot as plt
import pylab

plt.plot_date( x = xaxis, y = yaxis, marker = 'x', color=[1, 0, 0, .2], label='Data Series' )
line1 = pylab.Line2D(range(1),range(1),color='white',marker='x',markersize=10, markerfacecolor="red",alpha=1.0)
line2 = pylab.Line2D(range(10),range(10),marker="_",linewidth=3.0,color="dodgerblue",alpha=1.0)
plt.legend((line1,line2),('Text','Other Text'),numpoints=1,loc=1)

ここで、line1 は短い白い線 (本質的には見えない) を定義し、マーカー 'x' は赤で完全に不透明です。例として、line2 では、マーカーが表示されない長い青い線が表示されます。この「線」を作成することで、凡例内のプロパティをより簡単に制御できます。

于 2012-10-12T01:15:05.760 に答える
1

この関数は多くの凡例オブジェクトで機能することがわかりました.set_alpha()が、残念ながら、多くの凡例オブジェクトにはいくつかの部分 ( の出力などerrorbar()) があり、.set_alpha()呼び出しはそのうちの 1 つにしか影響しません。

ハンドルと の一部を使用.get_legend_handles_labels()してループすることはできます.set_alpha()が、残念ながら、copy.deepcopy()ハンドルのリストでは機能しないようで、プロット自体が影響を受けます。私が見つけた最善の回避策は、元のアルファ.set_alpha()を希望どおりに保存し、凡例を作成してから、プロットのアルファを元の値にリセットすることでした。ディープコピーできればhandles(アルファ値を保存したりリセットしたりする必要はありません)、はるかにきれいになりますが、python2.7 ではこれを行うことができませんでした (おそらく、これは凡例にあるオブジェクトによって異なります)。

f,ax=plt.subplots(1)

ax.plot(  ...  )

def legend_alpha(ax,newalpha=1.0):
    #sets alpha of legends to some value
    #this would be easier if deepcopy worked on handles, but it doesn't 
    handles,labels=ax.get_legend_handles_labels()
    alphass=[None]*len(handles) #make a list to hold lists of saved alpha values
    for k,handle in enumerate(handles): #loop through the legend entries
        alphas=[None]*len(handle) #make a list to hold the alphas of the pieces of this legend entry
        for i,h in enumerate(handle): #loop through the pieces of this legend entry (there could be a line and a marker, for example)
            try: #if handle was a simple list of parts, then this will work
                alphas[i]=h.get_alpha()
                h.set_alpha(newalpha)
            except: #if handle was a list of parts which themselves were made up of smaller subcomponents, then we must go one level deeper still.
                #this was needed for the output of errorbar() and may not be needed for simpler plot objects
                alph=[None]*len(h) 
                for j,hh in enumerate(h):
                    alph[j]=hh.get_alpha() #read the alpha values of the sub-components of the piece of this legend entry
                    hh.set_alpha(newalpha)
                alphas[i]=alph #save the list of alpha values for the subcomponents of this piece of this legend entry
        alphass[k]=alphas #save the list of alpha values for the pieces of this legend entry
    leg=ax.legend(handles,labels) #create the legend while handles has updated alpha values
    for k,handle in enumerate(handles): #loop through legend items to restore origina alphas on the plot
        for i,h in enumerate(handle): #loop through pieces of this legend item to restore alpha values on the plot
            try: 
                h.set_alpha(alphass[k][i])
            except:
                for j,hh in enumerate(h): #loop through sub-components of this piece of this legend item to restore alpha values
                    hh.set_alpha(alphass[k][i][j])
    return leg

leg=legend_alpha(ax)
leg.draggable()
于 2016-10-12T17:21:08.523 に答える