2017-08-23 18 views
-1

私はインターネットからいくつかのコードを探し、3番目の列の値に基づいてプロットする次の関数を見つけます。結果= 0カラーイエロー、結果= 1、カラー=パープル。 xlabel、ylabel、タイトルや凡例は表示されません。ax.set_title、ax.set_xlabel、ax.set_ylabel、ax.legendを追加しても表示されません。x、yタイトル、凡例の異なる色をプロットした散布図

できる「ランダム歩行者」画像」 『ブルーライン』現在の平均人口1、 『黄色い線』のようなIプロットの凡例は、男性人口2 結果= 1、黄色 結果= 0、紫

def plot_scatter(df=tmp, xcol='freq', ycol='type', catcol='result'): 
    fig, ax = plt.subplots() 
    categories = np.unique(df[catcol]) 
    colors = np.linspace(0, 1, len(categories)) 
    colordict = dict(zip(categories, colors)) 

    df["Color"] = df[catcol].apply(lambda x: colordict[x]) 
    ax.set_title('random walkers empirical $\mu$ and $\pm \sigma$ interval') 
    ax.legend(loc='best') 
    ax.set_xlabel(xcol) 
    ax.set_ylabel(ycol) 
    ax.scatter(df[xcol], df[ycol], c=df.Color) 
    return fig 

fig = plot_scatter(tmp) 
fig.show() 
を提示する方法

plot result

enter image description here

+0

:だからscatterへの通話は次のようになります。最小限の実例を書いて、あなたが望む行動を明確にすることができますか? – innisfree

+0

2番目のイメージのような伝説のボックスを持っていたい、紫色の現在の結果を0、黄色の現在の結果を1とする – jacobcan118

+0

投稿したコードの問題は何ですか?図1を生成するのにこのコードを使用しましたか? – Samufi

答えて

1

改訂答え:

ので、実際に問題を解決していません残念ながら、私の前の回答(以下保存)

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.patches as mpatches 

fig, ax = plt.subplots() 

# this part should get replaced with your dataframe-based calculations 
x = np.linspace(0, 1, 100) 
y = np.random.randint(0, 2, 100) 
colordict = {0: 'yellow', 1: 'purple'} 
color = list(colordict[val] for val in y) 

ax.scatter(x, y, c = color, label = color) 

ax.set_title('random walkers empirical $\mu$ and $\pm \sigma$ interval') 
ax.set_xlabel('freq') 
ax.set_ylabel('type') 

# custom patches for the legend 
yellow_patch = mpatches.Patch(color='yellow', label='population 1, result = 0') 
purple_patch = mpatches.Patch(color='purple', label='population 2, result = 1') 
ax.legend(handles=[yellow_patch, purple_patch]) 

plt.show() 

:ここで私はあなたがやりたいと思うものをしたサンプルコードを示しますが、データフレームなしで、私はあなたのデータフレームを持っていないので、リンクした例を理解できませんでした。あなたがしたいことは、this exampleのようなカスタムラベルを作ることです。

コメントのコードに関しては、の後にlegendの後に呼び出す必要があることに注意してください。あなたの軸ラベルとタイトルがなぜ表示されないのか分かりません。


以下は、残念なことに実際にこの問題を解決するものではありません。

プロット内の要素にラベルが付いていないため、凡例が表示されません。あなたのプロットを複製するのに十分なコードを提供していませんでしたが、は、 label = df.Colorscatterに渡して正しい効果を得ることができるはずであることを示しています。私はあなたがここに欲しいものを理解していない

ax.scatter(df[xcol], df[ycol], c=df.Color, label = df.Color) 

+0

私はラベルを散布に追加しましたが、まだ伝説を見ることはできませんax.set_title( '{} vs {}')フォーマット(xcol、ycol) ax.set_xlabel xcol) ax.set_ylabel(ycol) ax.plot() ax.legend(loc = 'best') ax.scatter(df [xcol]、df [ycol]、c = df.Color、label = df .Color、edgecolors = 'none') – jacobcan118

+0

@ jacobcan118私は私の答えを改訂し、実際の例を含めました。 –

+0

それは私のために働く。 – jacobcan118

関連する問題