Matplotlib根据索引在散点图中具有不同颜色的不同标记

时间:2020-01-24 14:51:55

标签: python pandas matplotlib

我有一个带有8列的DataFrame,我想分别散布图。每个点都会根据其索引获得颜色。 那没有问题。现在,我也想添加其他标记。我对标记器使用了相同的方法,并得到“系列”对象可变的错误,因此无法对其进行哈希处理。

有人知道解决此错误的方法吗?

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<div id="main">
<form enctype"multipart/formdata" id="form-id" class="hide" method="post" action="your-action">
  <label for="input-id" class="hide"></label>
  <input type="file" id="input-id" class="hide"/>
</form>

<button class="btn js-btn-upload">click me</button>
</div>

这是数据框的外观

df['colors'] = ""
df['marker'] = ''

for x in df.index:
    if 'SS' in x: #Stress
        df.loc[x, 'colors'] = 'orangered'
        df.loc[x, 'marker'] = 'o'
    elif 'PT' in x: #pyruvate and tca
        df.loc[x, 'colors'] = 'cyan'
        df.loc[x, 'marker'] = 'v'
    elif 'TE' in x: #terpenoids
        df.loc[x, 'colors'] = 'saddlebrown'
        df.loc[x, 'marker'] = '^'
    elif 'GS' in x: #glycolysis
        df.loc[x, 'colors'] = 'cyan'
        df.loc[x, 'marker'] = 'v'
    elif 'PP' in x: #pentose phosphat pathway
        df.loc[x, 'colors'] = 'steelblue'
        df.loc[x, 'marker'] = '8'
    elif 'GE' in x: #general
        df.loc[x, 'colors'] = 'k'
        df.loc[x, 'marker'] = 'o'
    elif 'TR' in x: #transport
        df.loc[x, 'colors'] = 'blue'
        df.loc[x, 'marker'] = '>'
    elif 'GM' in x: #cgsm aa
        df.loc[x, 'colors'] = '#0800f2'
        df.loc[x, 'marker'] = 'o'
    elif 'EX' in x: #export
        df.loc[x, 'colors'] = 'gray'
        df.loc[x, 'marker'] = '<'
    elif 'PR' in x: #photosynthesis
        df.loc[x, 'colors'] = 'green'
        df.loc[x, 'marker'] = 'X'
    elif 'PY' in x: #pyrimidine
        df.loc[x, 'colors'] = 'cyan'
        df.loc[x, 'marker'] = 'v'
    else:
        df.loc[x, 'colors'] = '#0b2863'
        df.loc[x, 'marker'] = 'o'





fig = plt.figure(figsize=(20,20))
ax = fig.add_subplot(111)

for x in key_reactions:

    ax.scatter(df['Biomass'], df[x.id], color=df['colors'], marker=df['marker'], alpha=0.5, s=150, edgecolors='k')#, alpha=2/len(key_reactions))

ax.plot([-2.5, 9], [-2.5, 9], 'k-', lw=2)

ax.set_xlim(-2.5, 9)
ax.set_ylim(-2.5, 9)

plt.xlabel('Flux with Biomass as objective \n [$mMol \cdot (gDW \cdot h)^{-1}$]',fontsize=26)
plt.ylabel('Flux with fuel as objective \n[$mMol \cdot (gDW \cdot h)^{-1}$]',fontsize=26)

custom_lines = [Line2D([0], [0], marker='o', color='orangered', lw=8),
               Line2D([0], [0], marker='v', color='cyan', lw=8),
               Line2D([0], [0], marker='^', color='saddlebrown', lw=8),
               Line2D([0], [0], marker='8', color='steelblue', lw=8),
               Line2D([0], [0], marker='o', color='k', lw=8),
               Line2D([0], [0], marker='>', color='blue', lw=8),
               Line2D([0], [0], marker='<', color='gray', lw=8),
               Line2D([0], [0], marker='X', color='darkgreen', lw=8)]


ax.legend(custom_lines, ['Stress', 'Pyruvate, glycolysis, pyrimidine and tca', 'Terpenoids', 'Pentose phosphat pathway', 'General', 'Transport', 'Export', 'Photosynthesis'])


fig.show()

谢谢!

0 个答案:

没有答案
相关问题