简体   繁体   中英

How to change the color of scatter plot with pandas?

I'm trying to color the dots of scatter plot by the values of a column in a CSV file. I'm using pandas dataframe to work with the file and I tried it with a simple file like this:

在此处输入图片说明

With this file it works well but when I try to do it with the CSV that I need this is the error:

Error: c of shape (31,) not acceptable as a color sequence for x with size 31, y with size 31

This is the column that I'm using to color the dots:

在此处输入图片说明

My function to read CSV and plot it:

       def readCSV(self):
        try:
            nombre, ok = QtWidgets.QFileDialog.getOpenFileName(self, 'Open file', "*.csv")

            if ok:
                df = pd.read_csv(nombre, delimiter=";")
                #print (df)
                ax = self.figure.add_subplot(111)
                ax.clear()
                df.groupby('Handle').plot(y='Frecuencia Inicial (MHz)', x='TOA (ns)', kind='line', ax=ax, title='Frec VS TOA')
                df.groupby('Handle').plot(y='Frecuencia Inicial (MHz)', x='TOA (ns)', kind='scatter', ax=ax, c=df['Causa DDW'])
                self.canvas.draw()

        except Exception as e:
            print("Error:", e)

I also tried:

 df.groupby('Handle').plot(y='Frecuencia Inicial (MHz)', x='TOA (ns)', kind='scatter', ax=ax, c='Causa DDW')

but this didn't work and this is the error:

Error: 'str' object has no attribute 'shape'

What am I missing or doing wrong? Thanks in advance!

In case someone need it! I just found a solution, probably not the best but it works. I asociate all the values from that column to a color in a map and just use it for "c":

 col = df['Causa DDW'].map({'EXC': 'g', 'MOD': 'b', 'DUR': 'r'})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM