从Pandas DataFrame创建一个散点图

时间:2017-10-22 23:07:47

标签: arrays pandas matplotlib dataframe scatter-plot

我正在研究Pandas DF问题,我无法将一些Pandas数据转换为可用格式来创建Scatter Plot。

以下是下面的代码,请让我知道我做错了什么以及如何纠正它。由于我是初学者,所以需要诚实的批评。

# Import Data
df = pd.read_csv(filepath + 'BaltimoreData.csv')

df = df.dropna()
print(df.head(20))
# These are two categories within the data
df.plot(df['Bachelors degree'], df['Median Income'])

# Plotting the Data
df.plot(kind = 'scatter', x = 'Bachelor degree', y = 'Median Income')
df.plot(kind = 'density')

2 个答案:

答案 0 :(得分:1)

只需在y上绘制x,如下所示,其中df是您的数据框,x和y是您的依赖和自变量:

#include <iostream>
using namespace std;

struct node{
int data;
node * next;
};

class singly{
private:
node * head,*tail;
public:
singly(){
    head=NULL;
    tail=NULL;
}

void createNode(int value){
    node * temp = new node;
    temp->data=value;
    temp->next=NULL;
    if(head==NULL){
        head=temp;
        tail=temp;
        temp=NULL;
    }

    else{
        tail->next=temp;
        tail=temp;
    }
}

void display(){
    node * temp = new node;
    head=temp;

    while(temp!=NULL){
        cout << temp->data << "\t" << endl;
        temp->next=temp;
    }
}

void insert_end(int value){
    node*newnode = new node;
    node*temp = new node;
    newnode->data=value;
    newnode->next=NULL;
    temp=head;

    while(temp->next!=NULL){
        temp = temp->next;
    }
    temp->next=newnode;
}

void delete_node(){
    node*current = new node;
    node*previous = new node;
    current = head;
    while(current->next!=NULL){
        previous=current;
        current=current->next;
    }

    tail=previous;
    previous->next=NULL;
    delete current;
}
};

int main(){

singly lists;
lists.createNode(32);
lists.createNode(654);
lists.createNode(34);
lists.createNode(234);

cout<<"\n--------------------------------------------------\n";
cout<<"---------------Displaying All nodes---------------";
cout<<"\n--------------------------------------------------\n";
lists.display();
cout<<"\n--------------------------------------------------\n";
cout<<"-----------------Inserting At End-----------------";
cout<<"\n--------------------------------------------------\n";
lists.createNode(55);
lists.display();
cout<<"\n--------------------------------------------------\n";
cout<<"-----------------Deleing At End-------------------";
cout<<"\n--------------------------------------------------\n";
lists.delete_node();
lists.display();


}

答案 1 :(得分:0)

您可以使用pandas中的scatter plot

import pandas
import matplotlib.pyplot as plt
plt.style.use('ggplot')
df.plot.scatter(x='Bachelors degree', y='Median Income');
plt.show()