Issue
I have very long table, here is an example:
and what do I want is to plot according to image number. given as 1,2,3 and scatter x y (given in column 2 and 3) coordinates on the plot. and color should be according to Type like Green for P and red for F and so on.
Plus I want them to plot separately not in the graph and save them like image.1 image2 and image 3.
Please suggest a possible solution.
I did grouping and now I'm stuck here.
I did until now is following:
for index, group in data.groupby(['image number']):
group.plot(x='x',y='y', label=index, kind='scatter')
Please help me to add colors in the plot according to MP column.
Solution
So from what I understand, your dataframe
looks something like this:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame([
{"number": 1, "x": 2391, "y": 1994, "MP": "PP"},
{"number": 1, "x": 530, "y": 2707, "MP": "PP"},
{"number": 1, "x": 467, "y": 3208, "MP": "PP"},
{"number": 2, "x": 952, "y": 656, "MP": "PE"},
{"number": 2, "x": 3678, "y": 1409, "MP": "PE"},
{"number": 3, "x": 2950, "y": 177, "MP": "PMMA"},
{"number": 3, "x": 1039, "y": 306, "MP": "f"},
{"number": 3, "x": 2726, "y": 1827, "MP": "PMMA"},
{"number": 3, "x": 3578, "y": 2129, "MP": "PMMA"},
{"number": 3, "x": 3708, "y": 3624, "MP": "f"},
# rest of dataframe entries...
])
If you want to make a scatter plot of these using matplotlib
, you can just iterate over the dataframe and use plt.scatter
to plot individual particles like so:
scheme = {
"PP": "m", # PP is magenta
"PE": "g", # PE is green
"PMMA": "c", # PMMA is cyan
"f": "r", # f is red
# put more colours here...
}
def plot_single(df, num, scheme):
for i in range(len(df.index)):
if df["number"][i] == num:
plt.scatter(x=df["x"][i], y=df["y"][i], c = scheme[df["MP"][i]] if df["MP"][i] in scheme else "k")
# V change these as needed V
plt.title("Particle "+str(num))
plt.xlabel("x")
plt.ylabel("y")
# ^ change these as needed ^
plt.show()
As an example, if you try plot_single(df, 1, scheme)
with the small snippet of the dataframe
, you will get this graph:
To further customize your scatter plot, you can add a legend, change the colour scheme, tinker with the scatter plot, etc.
EDIT:
if you want all the scatter plots on one graph, you can do it with a function like so:
def plot_all(df, scheme):
for i in range(len(df.index)):
plt.scatter(x=df["x"][i], y=df["y"][i], c = scheme[df["MP"][i]] if df["MP"][i] in scheme else "k")
plt.title("All Particles")
plt.xlabel("x")
plt.ylabel("y")
plt.show()
And you can call it by typing plot_all(df, scheme)
into your shell.
Answered By - Lucas Roy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.