简体   繁体   中英

Highlight last data point in Scatter plot with pandas

How can I highlight the last data point of the following dataframe in this scatter plot? Lets say I want to see it as a red point instead of blue.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

dates = pd.date_range('20130101', periods=30)
df = pd.DataFrame(np.random.randn(30,2) , columns=list('AB'))
df.plot(kind='scatter', y = 'A', x = 'B')
plt.show()

Sorry I can´t post the chart as still haven´t reached 10 reputation points...

You can do it this way using the matplotlib API:

ax = plt.subplot()
color = ['b'] * (len(df) - 1) + ['r']
ax.scatter(df.A, df.B, color=color)

This will give all blue points but the last, which will be red. The array can be any length and it will cycle through the colours sequentially until all points are coloured. So, eg, to alternate between red and blue you can just pass ['r', 'b'] . You can pass in names of colours, shorthands (as above) and colour codes like '#eeefff' . Check out the colour guide for more info: http://matplotlib.org/api/colors_api.html Another handy thing is the s parameter which controls the size of the dots. This follows the same rules so each dot can scaled according to an array of values.

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