我有一个名为的 Pandas 数据框clean,其中包含一列v,我想为其绘制直方图并叠加密度图。我知道我可以这样绘制一个:
import pandas as pd
import matplotlib.pyplot as plt
Maxv=200
plt.subplot(211)
plt.hist(clean['v'],bins=40, range=(0, Maxv), color='g')
plt.ylabel("Number")
plt.subplot(212)
ax=clean['v'].plot(kind='density')
ax.set_xlim(0, Maxv)
plt.xlabel("Orbital velocity (km/s)")
ax.get_yaxis().set_visible(False)
Run Code Online (Sandbox Code Playgroud)
但是当我尝试叠加时,y 尺度不匹配(并且我丢失了 y 轴刻度和标签):
yhist, xhist, _hist = plt.hist(clean['v'],bins=40, range=(0, Maxv), color='g')
plt.ylabel("Number")
ax=clean['v'].plot(kind='density') #I would like to insert here a normalization to max(yhist)/max(ax)
ax.set_xlim(0, Maxv)
plt.xlabel("Orbital velocity (km/s)")
ax.get_yaxis().set_visible(False)
Run Code Online (Sandbox Code Playgroud)
一些提示?(附加问题:如何更改密度平滑的宽度?)
我正在使用 Python 3(不知道该信息是否相关)。我有 2 个 Pandas DataFrame(来自read_csv()):Compact和SDSS_DR7_to_DR8。合并之前,它们包含以下类型:
Compact.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2070 entries, 0 to 2069
Data columns (total 8 columns):
Group 2070 non-null int64
Id 2070 non-null int64
RA 2070 non-null float64
Dec 2070 non-null float64
z 2070 non-null float64
R 2070 non-null float64
G 2070 non-null float64
objid 2070 non-null int64
dtypes: float64(5), int64(3)
memory usage: 129.5 KB
Run Code Online (Sandbox Code Playgroud)
和
SDSS_DR7_to_DR8.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 243500 entries, 0 to 243499
Data columns (total 5 …Run Code Online (Sandbox Code Playgroud) 我想这是相当简单的事情,但我找不到如何做到这一点.我一直在寻找教程和stackoverflow.
假设我有一个像这样的数据帧df:
Group Id_In_Group SomeQuantity
1 1 10
1 2 20
2 1 7
3 1 16
3 2 22
3 3 5
3 4 12
3 5 28
4 1 1
4 2 18
4 3 14
4 4 7
5 1 36
Run Code Online (Sandbox Code Playgroud)
我想只选择组中至少有4个对象的行(因此至少有4行具有相同的"组"编号),对于第4个对象的SomeQuantity,当通过提升SomeQuantity在组中排序时,大于20(例如).
例如,在给定的Dataframe中,它只返回第3组,因为它有4(> = 4)个成员,而它的第4个SomeQuantity(排序后)是22(> = 20),所以它应该构造数据帧:
Group Id_In_Group SomeQuantity
3 1 16
3 2 22
3 3 5
3 4 12
3 5 28
Run Code Online (Sandbox Code Playgroud)
(是否按SomeQuantity排序,无论如何).
有人可以帮助我吗?:)
我尝试显示位于此处的图像:http : //skyservice.pha.jhu.edu/DR12/ImgCutout/getjpeg.aspx?ra=118.70299999999999&dec=45.721000000000004&width=10&height=10&scale=0.6
图像如下所示:
我使用以下代码:
import matplotlib.pyplot as plt
import numpy as np
import urllib
import cv2
url = 'http://skyservice.pha.jhu.edu/DR12/ImgCutout/getjpeg.aspx?ra=118.70299999999999&dec=45.721000000000004&width=10&height=10&scale=0.6'
def url_to_image(url):
resp = urllib.request.urlopen(url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
return image
img = url_to_image(url)
plt.imshow(img)
Run Code Online (Sandbox Code Playgroud)
它显示如下:
所有颜色都太蓝的地方。我尝试过各种可能的方法来更改cv2.IMREAD_COLOR手册中,StackOverflow或网上其他位置(例如-1、0、1,...)中的值,cv2.COLOR_BGR2RGB但是我一直无法获得正确的颜色。我尝试过cv2.COLOR_BGR2GRAY,它甚至没有显示为灰度。我什至尝试了这个答案,但cv2.CV_LOAD_IMAGE_COLOR似乎不再存在了……
cv2.imdecode()flag 是否有正确的值,或者plt.imshow()的特殊颜色图会为我提供初始颜色?
我目前正在将panda用于数据科学目的,这是非常非原创的,所以我确信有一种比我更好的方法,即以excel的方式将列添加到从其他数据库计算的数据帧中.
例如,我有一个数据帧,其中我有一个质量和一个句点,我想添加一个列(M/P)^(1/3),M和P在转换为适当的单位.然后我写
day_sec=60.0*60.0*24.0
Msun=1.989 * 10**30
clean=reduced.dropna()
v=pd.DataFrame(clean['orbital_period'].apply(lambda x: x**(1/3.0))/clean['star_mass'].apply(lambda x: x**(1/3.0)), columns=list('v'))*day_sec/(Msun**(1/3.0))
clean.append(v)
Run Code Online (Sandbox Code Playgroud)
这甚至不像我想要的那样,并且写入和读取都非常复杂(这里,方程式非常简单).有什么建议吗?
pandas ×4
python ×3
dataframe ×2
python-3.x ×2
cv2 ×1
histogram ×1
matplotlib ×1
opencv ×1
seaborn ×1
slice ×1