I am trying to plot geochemical values and my data is in a dataframe with columsn such as "Pt"
, "Pd"
, "Cu"
etc,
I am trying to plot the Cu/Pd
ratio of a df
against the Pd
values, which I've done and is fine. However then I want to make smaller subplots of the data, refining them by a third variable, "Mg#"
which is a column in the same df.
How would I only plot values with Mg# >70
or Mg# 60-70 for example.
I understand there must be and IF or ELIF statement but I'm unsure how to order it.
This is my current code for the plot:
plt.scatter(East_Greenland['Pd'],
East_Greenland['Cu']/East_Greenland["Pd"],
color = "purple", marker = "s", s=25,
edgecolor = "black", label = "East Greenland (onshore)")
I am just unsure how to filter by the Mg# without creating a new column or df. Thanks!
I've tried looking at other elif and if problems but they are either for discrete values or I cannot follow where to put in my statements
I am trying to plot geochemical values and my data is in a dataframe with columsn such as "Pt"
, "Pd"
, "Cu"
etc,
I am trying to plot the Cu/Pd
ratio of a df
against the Pd
values, which I've done and is fine. However then I want to make smaller subplots of the data, refining them by a third variable, "Mg#"
which is a column in the same df.
How would I only plot values with Mg# >70
or Mg# 60-70 for example.
I understand there must be and IF or ELIF statement but I'm unsure how to order it.
This is my current code for the plot:
plt.scatter(East_Greenland['Pd'],
East_Greenland['Cu']/East_Greenland["Pd"],
color = "purple", marker = "s", s=25,
edgecolor = "black", label = "East Greenland (onshore)")
I am just unsure how to filter by the Mg# without creating a new column or df. Thanks!
I've tried looking at other elif and if problems but they are either for discrete values or I cannot follow where to put in my statements
Share Improve this question edited Mar 4 at 18:03 Christoph Rackwitz 15.6k5 gold badges39 silver badges51 bronze badges asked Jan 29 at 19:29 Anna MorrisonAnna Morrison 311 silver badge2 bronze badges 1 |1 Answer
Reset to default 0To filter by the Mg#
without creating a new column you can use boolean indexing. The created variables hold temporary filtered DataFrames, essential for plotting the subsets. They do not change the original dataframe.
Filtering and plotting for Mg# > 70
high_mg_data = East_Greenland[East_Greenland['Mg#'] > 70]
plt.scatter(high_mg_data['Pd'],
high_mg_data['Cu']/high_mg_data["Pd"],
color = "red", marker = "o", s=25,
edgecolor = "black", label = "Mg# > 70")
Filtering and plotting for 60 <= Mg# <= 70
mid_mg_data = East_Greenland[(East_Greenland['Mg#'] >= 60) & (East_Greenland['Mg#'] <= 70)]
plt.scatter(mid_mg_data['Pd'],
mid_mg_data['Cu']/mid_mg_data["Pd"],
color = "blue", marker = "^", s=25,
edgecolor = "black", label = "60 <= Mg# <= 70")
Note that in the context of filtering Pandas DataFrames using boolean indexing, you must use the &
(bitwise AND) operator, not the and
(logical AND) operator.
East_Greenland_70 = East_Greenland[East_Greenland["Mg#"] > 70]
. – jared Commented Jan 29 at 20:45