bins = np.array([0, 0.5, 1, 2, 10]) # 股价上涨的公司 shares_up = shares_price[shares_price['increase'] > 0] # 按涨幅进行分组 shares_up['label'] = pd.cut(shares_up['increase'], bins) # 分组统计 up_label_count = shares_up[['label', 'code']].groupby('label').count() up_label_count['占比'] = up_label_count['code'] / up_label_count.sum().values sns.barplot(x=up_label_count['占比'], y=up_label_count.index)修改代码,使配色好看
时间: 2024-01-20 07:04:00 浏览: 25
bins = np.array([0, 0.5, 1, 2, 10])
shares_up = shares_price[shares_price['increase'] > 0]
shares_up['label'] = pd.cut(shares_up['increase'], bins)
up_label_count = shares_up[['label', 'code']].groupby('label').count()
up_label_count['占比'] = up_label_count['code'] / up_label_count.sum().values
plt.figure(figsize=(8, 6))
sns.set(style="whitegrid")
sns.barplot(x=up_label_count['占比'], y=up_label_count.index, palette="Blues_d")
plt.xlabel("占比", size=12)
plt.ylabel("涨幅", size=12)
plt.title("股价上涨的公司涨幅分布", size=14)
相关问题
bins = np.arange(11) # hist, _ = np.histogram(d, bins=bins)
这是使用 NumPy 库中的 arange 函数生成一个包含 11 个元素的一维数组,用于设置直方图的边界。然后使用 np.histogram 函数计算直方图,第一个参数 d 是数据,第二个参数 bins 是直方图的边界。函数返回两个值,第一个值是直方图的频数,第二个值是直方图的边界。这里使用了下划线(_)来表示返回值中的第二个值不被使用,因为我们只需要直方图的频数。
import numpy as np import pandas as pd def localmin(points, pixel_size): x_min = np.min(points[:, 0]) y_min = np.min(points[:, 1]) x_max = np.max(points[:, 0]) y_max = np.max(points[:, 1]) w = x_max - x_min h = y_max - y_min wn = w // pixel_size + 1 hn = h // pixel_size + 1 x_bins = np.array([i * pixel_size for i in range(int(wn + 1))]) y_bins = np.array([i * pixel_size for i in range(int(hn + 1))]) df = pd.DataFrame(points, columns=['x', 'y', 'z', 'classification', 'indices']) df['x_bin'] = pd.cut(df['x'], bins=x_bins) df['y_bin'] = pd.cut(df['y'], bins=y_bins) result = df.groupby(['x_bin', 'y_bin']).apply(lambda x: x.loc[x['z'].idxmin()])[['x', 'y', 'z', 'classification', 'indices']] return result a = np.random.random([100,3]) b = np.random.random([100, 1])//0.5 c = np.arange(1000).reshape([100,1]) a = np.concatenate([a,b,c],axis=-1) d = localmin(a,0.2) 如何从a中删除d中的点
可以使用pandas的isin()方法来判断a中的点是否出现在d中,然后使用~运算符将不包含在d中的点筛选出来。具体实现代码如下:
```python
import numpy as np
import pandas as pd
def localmin(points, pixel_size):
x_min = np.min(points[:, 0])
y_min = np.min(points[:, 1])
x_max = np.max(points[:, 0])
y_max = np.max(points[:, 1])
w = x_max - x_min
h = y_max - y_min
wn = w // pixel_size + 1
hn = h // pixel_size + 1
x_bins = np.array([i * pixel_size for i in range(int(wn + 1))])
y_bins = np.array([i * pixel_size for i in range(int(hn + 1))])
df = pd.DataFrame(points, columns=['x', 'y', 'z', 'classification', 'indices'])
df['x_bin'] = pd.cut(df['x'], bins=x_bins)
df['y_bin'] = pd.cut(df['y'], bins=y_bins)
result = df.groupby(['x_bin', 'y_bin']).apply(lambda x: x.loc[x['z'].idxmin()])[['x', 'y', 'z', 'classification', 'indices']]
return result
a = np.random.random([100,3])
b = np.random.random([100, 1])//0.5
c = np.arange(1000).reshape([100,1])
a = np.concatenate([a,b,c],axis=-1)
d = localmin(a,0.2)
# 筛选出不包含在d中的点并重新赋值给a
a = a[~a[:, :2].isin(d.iloc[:, :2].values).any(axis=1)]
```
其中,isin()方法用于判断一个DataFrame或Series对象中的值是否在另一个DataFrame或Series对象中出现过。在这里我们用isin()方法来判断a中的前两列(即x和y坐标)是否出现在d中,然后使用any()方法将结果沿着列方向合并,得到一个长度为100的布尔型数组,表示a中每个点是否在d中出现过。最后使用~运算符对数组进行取反,得到不包含在d中的点的布尔型数组,然后使用该数组对a进行筛选即可。