Plotting data in Mathematica certain colours depending on

Click For Summary
SUMMARY

This discussion focuses on creating a scatter plot in Mathematica that visually distinguishes data points based on a boolean value from a CSV file. The user aims to plot points in blue for "TRUE" values and red for "FALSE" values. The solution involves using the Cases function to separate the data into two lists, truedata and falsedata, and then utilizing ListPlot to display them with the specified colors. Additionally, users are advised to manage PlotRange explicitly to ensure all data points are visible.

PREREQUISITES
  • Familiarity with Mathematica syntax and functions
  • Understanding of data manipulation using Cases in Mathematica
  • Knowledge of plotting functions, specifically ListPlot
  • Basic understanding of CSV data import in Mathematica
NEXT STEPS
  • Explore advanced plotting techniques in Mathematica, such as Graphics and GraphicsComplex
  • Learn about customizing plot aesthetics in Mathematica, including PlotStyle and ColorFunction
  • Investigate handling large datasets in Mathematica for performance optimization
  • Study the implications of PlotRange in multi-dataset plots to ensure comprehensive data visualization
USEFUL FOR

This discussion is beneficial for data analysts, statisticians, and researchers using Mathematica for data visualization, particularly those needing to differentiate data points based on categorical values.

stripes
Messages
262
Reaction score
0

Homework Statement



I have a list of data in .csv format that looks like the following:

10 5 TRUE
10.1 5.1 TRUE
10.2 5.2 FALSE
10.3 5.3 FALSE
10.4 5.4 FALSE
10.5 5.5 TRUE
10.6 5.6 TRUE
...
There are thousands of entries.

I want to make a scatter plot in Mathematica where it plots the points {10, 5}, {10.1, 5.1}, etc., but I want the ones with TRUE to be blue, and FALSE to be red.

Homework Equations



--

The Attempt at a Solution



I've tried using a while loop then an if statement to do this, but it seems to be an infinite loop. What I did was:

homeruns = column3 (so this is now a list with TRUE FALSE TRUE FALSE etc.)
angles = column2
initialV = column1

Code:
ListPlot[Transpose[{angles, initialV}], i=0; while [i<length[homeruns], If [ truefalse[[i]], PlotStyle -> {Blue}, PlotStyle -> {Red} ] i++; ]]

But this is just stupid. How could I go about making a plot where certain sections of the plot will be one colorr vs another colour, depending on that point's truth value?
 
Physics news on Phys.org
In[1]:= data = Import["data.csv"]

Out[1]= {{10, 5, "TRUE"}, {10.1, 5.1, "TRUE"}, {10.2, 5.5, "FALSE"}, {10.3, 5.6, "FALSE"}, {10.4, 5.7, "FALSE"}, {10.5, 5.5, "TRUE"}, {10.6, 5.6, "TRUE"}}

In[2]:= truedata = Cases[data, {x_, y_, "TRUE"} :> {x, y}]

Out[2]= {{10, 5}, {10.1, 5.1}, {10.5, 5.5}, {10.6, 5.6}}

In[3]:= falsedata = Cases[data, {x_, y_, "FALSE"} :> {x, y}]

Out[3]= {{10.2, 5.5}, {10.3, 5.6}, {10.4, 5.7}}

In[4]:= ListPlot[{truedata, falsedata}, Joined -> True]

Out[4]= ...PlotSnipped...

And then you can begin exploring graphics primitives to change colors of individual plots, points, etc

But watch out for combining several plots when some versions Mathematica will take the PlotRange from the first data set and ignore the range of subsequent data sets. You can overcome that by giving an explicit PlotRange large enough to show all your data
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K