简体   繁体   中英

How can I tell matplotlib.pyplot to make a scatter graph using values from Column A when Column D is True?

        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

I've got this dataframe and how can I tell matplotlib.pyplot to make a scatter graph using values from min15 when min_record_2015 is true and max15 when max_record is true (and M_D should be on the X axis )?

I tried making an index list with the needed value to use later in plt.scatter but I get the full index list.

broken_min=(foo["min_record_2015"]==True).index.tolist()

Thanks in advance!

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()

Try this:

(df.assign(y=np.where(df['min_record_2015'], df['min15'], df['max15']))
   .plot.scatter('M_D', 'y', grid=True))

Result:

在此处输入图片说明

Since you want your y-values to be based on a conditional, you can create variable to hold the data you want to plot. Suppose your data was stored in a pandas DataFrame df :

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

I don't know matplotlib well, but filtering rows by column is simply:

dataframe[dataframe['Column_D'] == True]

So plotting should be something like

plot.scatter(x='Column A', data=dataframe[dataframe["min_record_2015"]==True])

==EDIT==

I just saw, that you wanted to plot from two columns depending on value of the third one. Then it should be something like:

fig, ax = plt.subplots()
plt.scatter(x='Column_A', y='Column_C', data=dataframe[dataframe["Column_E"] == True], ax=ax)
plt.scatter(x='Column_B', y='Column_C', data=dataframe[dataframe["Column_F"] == True], ax=ax)
plt.show()

`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM