PCA can't get color on scatterplot
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm doing a mini project on my own. I'm trying this thing with PCA. After i have plotted my graph, I can't seem to get the color out. These are the steps below for my code. Before this i have scaled and transformed the data. After scaling i did a PCA. These are the steps. First:
from sklearn.decomposition import PCA
pca= PCA(n_components= 2)
pca.fit(scaled_data)
x_pca= pca.transform(scaled_data)
principaldf= pd.DataFrame(data=x_pca,
columns=['principal component 1',
'principal component 2'])
After this i combined both dataframes and got this.
new_df= principaldf.join(df_features)
new_df.head()

Next i attempted to plot the graph with the code below:
color= ['r','g']
plt.scatter(x_pca[:, 0], x_pca[:, 1],
edgecolor='none', alpha=0.5, c= color)
plt.xlabel('component 1')
plt.ylabel('component 2')
I got this error
ValueError: 'c' argument has 2 elements, which is not acceptable for
use with 'x' with size 261, 'y' with size 261.

Can anyone please advise or help? Hope my question was clear enough. thanks!
python matplotlib pca
|
show 1 more comment
I'm doing a mini project on my own. I'm trying this thing with PCA. After i have plotted my graph, I can't seem to get the color out. These are the steps below for my code. Before this i have scaled and transformed the data. After scaling i did a PCA. These are the steps. First:
from sklearn.decomposition import PCA
pca= PCA(n_components= 2)
pca.fit(scaled_data)
x_pca= pca.transform(scaled_data)
principaldf= pd.DataFrame(data=x_pca,
columns=['principal component 1',
'principal component 2'])
After this i combined both dataframes and got this.
new_df= principaldf.join(df_features)
new_df.head()

Next i attempted to plot the graph with the code below:
color= ['r','g']
plt.scatter(x_pca[:, 0], x_pca[:, 1],
edgecolor='none', alpha=0.5, c= color)
plt.xlabel('component 1')
plt.ylabel('component 2')
I got this error
ValueError: 'c' argument has 2 elements, which is not acceptable for
use with 'x' with size 261, 'y' with size 261.

Can anyone please advise or help? Hope my question was clear enough. thanks!
python matplotlib pca
Please post the full trace as text, not image - idownvotedbecau.se/imageofanexception
– desertnaut
Nov 25 '18 at 10:55
1
what variable do you want to actually color your points by? ie which points are red and which are green?
– Simon
Nov 25 '18 at 11:05
hello @Simon, for Gender will like to get red and green. Thanks
– Jonathan
Nov 25 '18 at 11:22
@desertnaut whats a full trace? and isnt it in text format? lol. read the link u sent and got no clue what it even means
– Jonathan
Nov 25 '18 at 11:25
1
It means that your error message should be posted as text, and not as a screenshot image as you have done here...
– desertnaut
Nov 25 '18 at 12:11
|
show 1 more comment
I'm doing a mini project on my own. I'm trying this thing with PCA. After i have plotted my graph, I can't seem to get the color out. These are the steps below for my code. Before this i have scaled and transformed the data. After scaling i did a PCA. These are the steps. First:
from sklearn.decomposition import PCA
pca= PCA(n_components= 2)
pca.fit(scaled_data)
x_pca= pca.transform(scaled_data)
principaldf= pd.DataFrame(data=x_pca,
columns=['principal component 1',
'principal component 2'])
After this i combined both dataframes and got this.
new_df= principaldf.join(df_features)
new_df.head()

Next i attempted to plot the graph with the code below:
color= ['r','g']
plt.scatter(x_pca[:, 0], x_pca[:, 1],
edgecolor='none', alpha=0.5, c= color)
plt.xlabel('component 1')
plt.ylabel('component 2')
I got this error
ValueError: 'c' argument has 2 elements, which is not acceptable for
use with 'x' with size 261, 'y' with size 261.

Can anyone please advise or help? Hope my question was clear enough. thanks!
python matplotlib pca
I'm doing a mini project on my own. I'm trying this thing with PCA. After i have plotted my graph, I can't seem to get the color out. These are the steps below for my code. Before this i have scaled and transformed the data. After scaling i did a PCA. These are the steps. First:
from sklearn.decomposition import PCA
pca= PCA(n_components= 2)
pca.fit(scaled_data)
x_pca= pca.transform(scaled_data)
principaldf= pd.DataFrame(data=x_pca,
columns=['principal component 1',
'principal component 2'])
After this i combined both dataframes and got this.
new_df= principaldf.join(df_features)
new_df.head()

Next i attempted to plot the graph with the code below:
color= ['r','g']
plt.scatter(x_pca[:, 0], x_pca[:, 1],
edgecolor='none', alpha=0.5, c= color)
plt.xlabel('component 1')
plt.ylabel('component 2')
I got this error
ValueError: 'c' argument has 2 elements, which is not acceptable for
use with 'x' with size 261, 'y' with size 261.

Can anyone please advise or help? Hope my question was clear enough. thanks!
python matplotlib pca
python matplotlib pca
edited Nov 25 '18 at 10:55
desertnaut
21.3k84680
21.3k84680
asked Nov 25 '18 at 10:42
JonathanJonathan
467
467
Please post the full trace as text, not image - idownvotedbecau.se/imageofanexception
– desertnaut
Nov 25 '18 at 10:55
1
what variable do you want to actually color your points by? ie which points are red and which are green?
– Simon
Nov 25 '18 at 11:05
hello @Simon, for Gender will like to get red and green. Thanks
– Jonathan
Nov 25 '18 at 11:22
@desertnaut whats a full trace? and isnt it in text format? lol. read the link u sent and got no clue what it even means
– Jonathan
Nov 25 '18 at 11:25
1
It means that your error message should be posted as text, and not as a screenshot image as you have done here...
– desertnaut
Nov 25 '18 at 12:11
|
show 1 more comment
Please post the full trace as text, not image - idownvotedbecau.se/imageofanexception
– desertnaut
Nov 25 '18 at 10:55
1
what variable do you want to actually color your points by? ie which points are red and which are green?
– Simon
Nov 25 '18 at 11:05
hello @Simon, for Gender will like to get red and green. Thanks
– Jonathan
Nov 25 '18 at 11:22
@desertnaut whats a full trace? and isnt it in text format? lol. read the link u sent and got no clue what it even means
– Jonathan
Nov 25 '18 at 11:25
1
It means that your error message should be posted as text, and not as a screenshot image as you have done here...
– desertnaut
Nov 25 '18 at 12:11
Please post the full trace as text, not image - idownvotedbecau.se/imageofanexception
– desertnaut
Nov 25 '18 at 10:55
Please post the full trace as text, not image - idownvotedbecau.se/imageofanexception
– desertnaut
Nov 25 '18 at 10:55
1
1
what variable do you want to actually color your points by? ie which points are red and which are green?
– Simon
Nov 25 '18 at 11:05
what variable do you want to actually color your points by? ie which points are red and which are green?
– Simon
Nov 25 '18 at 11:05
hello @Simon, for Gender will like to get red and green. Thanks
– Jonathan
Nov 25 '18 at 11:22
hello @Simon, for Gender will like to get red and green. Thanks
– Jonathan
Nov 25 '18 at 11:22
@desertnaut whats a full trace? and isnt it in text format? lol. read the link u sent and got no clue what it even means
– Jonathan
Nov 25 '18 at 11:25
@desertnaut whats a full trace? and isnt it in text format? lol. read the link u sent and got no clue what it even means
– Jonathan
Nov 25 '18 at 11:25
1
1
It means that your error message should be posted as text, and not as a screenshot image as you have done here...
– desertnaut
Nov 25 '18 at 12:11
It means that your error message should be posted as text, and not as a screenshot image as you have done here...
– desertnaut
Nov 25 '18 at 12:11
|
show 1 more comment
1 Answer
1
active
oldest
votes
You can try something like:
Assign numerical values to Gender:
new_df['Gender'] = new_df['Gender'].replace({'Male':0, 'Female':1})
Then plot using color and cmap:
plt.scatter(x_pca[:, 0], x_pca[:, 1], edgecolor='none', alpha=0.5,
c=new_df['Gender'], cmap='RdYlGn')
When you pass in a 2 item list like ['r', 'g'] it doesnt know which points should be which colour
Hello @Simon! I have tried running the code but it returned this error. ValueError: 'c' argument must either be valid as mpl color(s) or as numbers to be mapped to colors. Here c = 0 Male
– Jonathan
Nov 25 '18 at 11:31
Or is it possible that I might have gotten the whole algo wrong in the first place?
– Jonathan
Nov 25 '18 at 11:32
1
expanded my answer. see if the edit works
– Simon
Nov 25 '18 at 11:35
oh wow! Thanks so much @Simon! really appreciate your help a lot!
– Jonathan
Nov 25 '18 at 12:59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466673%2fpca-cant-get-color-on-scatterplot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can try something like:
Assign numerical values to Gender:
new_df['Gender'] = new_df['Gender'].replace({'Male':0, 'Female':1})
Then plot using color and cmap:
plt.scatter(x_pca[:, 0], x_pca[:, 1], edgecolor='none', alpha=0.5,
c=new_df['Gender'], cmap='RdYlGn')
When you pass in a 2 item list like ['r', 'g'] it doesnt know which points should be which colour
Hello @Simon! I have tried running the code but it returned this error. ValueError: 'c' argument must either be valid as mpl color(s) or as numbers to be mapped to colors. Here c = 0 Male
– Jonathan
Nov 25 '18 at 11:31
Or is it possible that I might have gotten the whole algo wrong in the first place?
– Jonathan
Nov 25 '18 at 11:32
1
expanded my answer. see if the edit works
– Simon
Nov 25 '18 at 11:35
oh wow! Thanks so much @Simon! really appreciate your help a lot!
– Jonathan
Nov 25 '18 at 12:59
add a comment |
You can try something like:
Assign numerical values to Gender:
new_df['Gender'] = new_df['Gender'].replace({'Male':0, 'Female':1})
Then plot using color and cmap:
plt.scatter(x_pca[:, 0], x_pca[:, 1], edgecolor='none', alpha=0.5,
c=new_df['Gender'], cmap='RdYlGn')
When you pass in a 2 item list like ['r', 'g'] it doesnt know which points should be which colour
Hello @Simon! I have tried running the code but it returned this error. ValueError: 'c' argument must either be valid as mpl color(s) or as numbers to be mapped to colors. Here c = 0 Male
– Jonathan
Nov 25 '18 at 11:31
Or is it possible that I might have gotten the whole algo wrong in the first place?
– Jonathan
Nov 25 '18 at 11:32
1
expanded my answer. see if the edit works
– Simon
Nov 25 '18 at 11:35
oh wow! Thanks so much @Simon! really appreciate your help a lot!
– Jonathan
Nov 25 '18 at 12:59
add a comment |
You can try something like:
Assign numerical values to Gender:
new_df['Gender'] = new_df['Gender'].replace({'Male':0, 'Female':1})
Then plot using color and cmap:
plt.scatter(x_pca[:, 0], x_pca[:, 1], edgecolor='none', alpha=0.5,
c=new_df['Gender'], cmap='RdYlGn')
When you pass in a 2 item list like ['r', 'g'] it doesnt know which points should be which colour
You can try something like:
Assign numerical values to Gender:
new_df['Gender'] = new_df['Gender'].replace({'Male':0, 'Female':1})
Then plot using color and cmap:
plt.scatter(x_pca[:, 0], x_pca[:, 1], edgecolor='none', alpha=0.5,
c=new_df['Gender'], cmap='RdYlGn')
When you pass in a 2 item list like ['r', 'g'] it doesnt know which points should be which colour
edited Nov 25 '18 at 11:34
answered Nov 25 '18 at 11:26
SimonSimon
4,616103279
4,616103279
Hello @Simon! I have tried running the code but it returned this error. ValueError: 'c' argument must either be valid as mpl color(s) or as numbers to be mapped to colors. Here c = 0 Male
– Jonathan
Nov 25 '18 at 11:31
Or is it possible that I might have gotten the whole algo wrong in the first place?
– Jonathan
Nov 25 '18 at 11:32
1
expanded my answer. see if the edit works
– Simon
Nov 25 '18 at 11:35
oh wow! Thanks so much @Simon! really appreciate your help a lot!
– Jonathan
Nov 25 '18 at 12:59
add a comment |
Hello @Simon! I have tried running the code but it returned this error. ValueError: 'c' argument must either be valid as mpl color(s) or as numbers to be mapped to colors. Here c = 0 Male
– Jonathan
Nov 25 '18 at 11:31
Or is it possible that I might have gotten the whole algo wrong in the first place?
– Jonathan
Nov 25 '18 at 11:32
1
expanded my answer. see if the edit works
– Simon
Nov 25 '18 at 11:35
oh wow! Thanks so much @Simon! really appreciate your help a lot!
– Jonathan
Nov 25 '18 at 12:59
Hello @Simon! I have tried running the code but it returned this error. ValueError: 'c' argument must either be valid as mpl color(s) or as numbers to be mapped to colors. Here c = 0 Male
– Jonathan
Nov 25 '18 at 11:31
Hello @Simon! I have tried running the code but it returned this error. ValueError: 'c' argument must either be valid as mpl color(s) or as numbers to be mapped to colors. Here c = 0 Male
– Jonathan
Nov 25 '18 at 11:31
Or is it possible that I might have gotten the whole algo wrong in the first place?
– Jonathan
Nov 25 '18 at 11:32
Or is it possible that I might have gotten the whole algo wrong in the first place?
– Jonathan
Nov 25 '18 at 11:32
1
1
expanded my answer. see if the edit works
– Simon
Nov 25 '18 at 11:35
expanded my answer. see if the edit works
– Simon
Nov 25 '18 at 11:35
oh wow! Thanks so much @Simon! really appreciate your help a lot!
– Jonathan
Nov 25 '18 at 12:59
oh wow! Thanks so much @Simon! really appreciate your help a lot!
– Jonathan
Nov 25 '18 at 12:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466673%2fpca-cant-get-color-on-scatterplot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please post the full trace as text, not image - idownvotedbecau.se/imageofanexception
– desertnaut
Nov 25 '18 at 10:55
1
what variable do you want to actually color your points by? ie which points are red and which are green?
– Simon
Nov 25 '18 at 11:05
hello @Simon, for Gender will like to get red and green. Thanks
– Jonathan
Nov 25 '18 at 11:22
@desertnaut whats a full trace? and isnt it in text format? lol. read the link u sent and got no clue what it even means
– Jonathan
Nov 25 '18 at 11:25
1
It means that your error message should be posted as text, and not as a screenshot image as you have done here...
– desertnaut
Nov 25 '18 at 12:11