M_D        min       max     min15  max15     min_record_2015  max_record_2015

0    693596.0  -4.762312  3.232487  -13.3    1.1            True   False
1    693597.0  -7.787081  1.154286  -12.2    3.9            True   True
2    693598.0  -9.556938 -0.870142   -6.7    3.9           False   True
3    693599.0  -7.292574  0.183099   -8.8    4.4            True   True
4    693600.0  -5.668780  1.768571  -15.5    2.8            True   True
5    693601.0  -5.867633  1.738278  -18.2    3.3            True   True

我得到了这个数据文件,如何告诉MMatPrLiByPrPoto使用Min 15的值,当MynReordOn2015是真的和Max 15,当Max记录是真的(MX D应该在X轴上)时,做一个散布图。
我试着用需要的值制作一个索引列表,稍后在plt.scatter中使用,但是我得到了完整的索引列表。
broken_min=(foo["min_record_2015"]==True).index.tolist()

提前谢谢!
edit:
foo=pd.merge(temp,temp15,how="outer", left_on="M_D",right_on="M_D")
foo['min_record_2015'] = foo['min15'] < foo['min']
foo['max_record_2015'] = foo['max15'] > foo['max']
foo=foo[(foo.min_record_2015 == True) | (foo.max_record_2015 == True)] #remove false|false rows
#broken_min=(foo["min_record_2015"]==True).index.tolist()
#broken_max=(foo["max_record_2015"]==True).tolist()

#ys=foo.apply(lambda x: x["min15"] if x["min_record_2015"] else x["max15"] , axis=1)


%matplotlib notebook
    #plt.close()
    plt.figure()
    plt.plot(temp_min["M_D"], temp_min["min"]/10, c="b", lw=0.5, label="record low 2005-2014")
    plt.plot(temp_max["M_D"], temp_max["max"]/10 ,c="r", lw=0.5, label="record high 2005-2014")

    #plt.scatter(foo["M_D"], ys, c="m", marker="o", s=5, label="record low broken in 2015")
    #plt.scatter(temp15["M_D"], temp15["max15"], c="k", marker="o", s=5, label="record high broken in 2015")
    (foo.assign(y=np.where(foo['min_record_2015'], foo['min15'], foo['max15'])).plot.scatter('M_D', 'y'))

    ax = plt.gca()
    ax.xaxis.set_major_formatter(dates.DateFormatter('%b-%d'))
    ax.xaxis.set_major_locator(dates.MonthLocator())
    loc, labels = plt.xticks()
    plt.setp(labels, rotation=45)

    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)


    plt.subplots_adjust(bottom=0.15)
    plt.legend(loc='best')
    plt.ylabel('Temperature (Deg C)')
    plt.title('Record Temperatures for each Day of the Year')

    plt.gca().fill_between(temp['M_D'],
                           temp['min'], temp['max'],
                           facecolor='blue',
                           alpha=0.20)
    #plt.show()

最佳答案

因为您希望y值基于条件,所以可以创建变量来保存要打印的数据。假设您的数据存储在pandas数据框中:

ys = df.apply(lambda x: x['min15'] if x['min_record_2015'] else x['max15'], axis=1)
plt.scatter(df['M_D'], ys)

关于python - 当列D为True时,如何告诉matplotlib.pyplot使用列A的值制作散点图?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47518403/

10-12 00:23
查看更多