简体   繁体   English

使用熊猫突出显示散点图中的最后一个数据点

[英]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...抱歉,我无法发布图表,因为还没有达到 10 声望点...

You can do it this way using the matplotlib API:您可以使用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'] .因此,例如,要在红色和蓝色之间交替,您只需传递['r', 'b'] You can pass in names of colours, shorthands (as above) and colour codes like '#eeefff' .您可以传入颜色名称、速记(如上)和颜色代码,如'#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.查看颜色指南以获取更多信息: http : //matplotlib.org/api/colors_api.html另一个方便的事情是控制点大小的s参数。 This follows the same rules so each dot can scaled according to an array of values.这遵循相同的规则,因此每个点都可以根据一组值进行缩放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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