如果用于从熊猫注释的功能

时间:2014-06-17 09:17:45

标签: python matplotlib pandas

我从pandas数据帧中分散绘制值。我想仅在值大于100时才注释点。我不知道如何去做。

这是我正在使用的代码(这很糟糕,但我对此非常陌生):

female_data = r'/home/jg/Desktop/hurricanedata_f.csv'
female_df = read_csv(female_data)

male_data = r'/home/jg/Desktop/hurricanedata_m.csv'
male_df = read_csv(male_data)

x = female_df['Year']
y = female_df['alldeaths']
z = female_df['Name']
y_mean = [np.mean(y) for i in x]
a = male_df['Year']
b = male_df['alldeaths']
b_mean = [np.mean(b) for i in b]

fig = plt.figure()
ax = fig.add_subplot(1,1,1)


ax.annotate('Agnes', xy=(1972,117))
ax1 = fig.add_subplot(1,1,1)
ax1.scatter(x,y, label = 'female', color = 'r')
ax2 = fig.add_subplot(1,1,1)
ax2.scatter(a,b, label = 'male')
ax3 = fig.add_subplot(1,1,1)
ax3.plot(x, y_mean, linestyle='--', color = 'r')
ax4 = fig.add_subplot(1,1,1)
ax4.plot(a, b_mean, linestyle='--', color = 'blue')

plt.title('Hurricanes')
plt.xlabel('Year')
plt.ylabel('Deaths')
plt.legend(loc='upper right')
plt.ylim([-5,300])
plt.xlim([1948,2020])

plt.show()

1 个答案:

答案 0 :(得分:0)

您可以遍历所有数据点并检查每个数据点是否大于100.然后为这些点添加注释。

import matplotlib.pyplot as plt
import numpy as np
import string

# Fake data
x = np.arange(10)
y = 10*np.random.rand(10) + 95
names = string.lowercase[:10] # first 10 lowercase letters

# Plot data
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x,y)

# Annonate points with y values greater than 100 
for xi, yi, iname in zip(x,y,names): # Loop over x and y values
    if yi > 100: # Check if y is greater than 100
        ax.annotate(iname, (xi, yi),size = 30) # Add an annoatation. 

plt.show()

enter image description here