我试图从形状文件中读取并将多边形与公共标记ID合并.
library(rgdal)
library(maptools)
if (!require(gpclib)) install.packages("gpclib", type="source")
gpclibPermit()
usa <- readOGR(dsn = "./path_to_data/", layer="the_name_of_shape_file")
usaIDs <- usa$segment_ID
isTRUE(gpclibPermitStatus())
usaUnion <- unionSpatialPolygons(usa, usaIDs)
Run Code Online (Sandbox Code Playgroud)
当我尝试绘制合并的多边形时:
for(i in c(1:length(names(usaUnion)))){
print(i)
myPol <- usaUnion@polygons[[i]]@Polygons[[1]]@coords
polygon(myPol, pch = 2, cex = 0.3, col = i)
}
Run Code Online (Sandbox Code Playgroud)
所有合并的段看起来都很好,除了那些在密歇根州周围以非常奇怪的方式发生合并的那些段,这样特定段的结果区域只给出如下的小多边形.
i = 10
usaUnion@polygons[[i]]@Polygons[[1]]@coords
Run Code Online (Sandbox Code Playgroud)
输出:
[,1] [,2]
[1,] -88.62533 48.03317
[2,] -88.90155 47.96025
[3,] -89.02862 47.85066
[4,] -89.13988 47.82408
[5,] -89.19292 47.84461
[6,] -89.20179 47.88386
[7,] -89.15610 47.93923
[8,] -88.49753 48.17380
[9,] -88.62533 48.03317
Run Code Online (Sandbox Code Playgroud)
原来是一个北方小岛:
我怀疑问题是由于某种原因,该unionSpatialPolygons
功能不喜欢地理上分离的多边形[密歇根州的左侧和右侧],但我还没有找到解决方案. …
我有一个 Pandas 数据框,其中包含变量名称、每个变量的值和count
(显示该行的频率):
df = pd.DataFrame({'var':['A', 'B', 'C'], 'value':[10, 20, 30], 'count':[1,2,3]})
var value count
A 10 1
B 20 2
C 30 3
Run Code Online (Sandbox Code Playgroud)
我想用来count
获得这样的输出:
var value
A 10
B 20
B 20
C 30
C 30
C 30
Run Code Online (Sandbox Code Playgroud)
最好的方法是什么?
我多年来一直在 github-pages 上托管一个个人网页。这是简单的 HTML 结构,并且在 gh-pages 上运行得很好。
最近,我在 bootstrap ( https://bootstrapmade.com/ )上发现了一个很酷的网站模板,我很想在我的 github 页面上使用它。但是当我尝试在 github 页面上使用这个模板时。这根本行不通!
使用https://bootstrapmade.com/到 github 页面的引导模板的最佳方式是什么?
这是先前问题的延伸。如何将数据框的两列组合为
data <- data.frame('a' = c('A','B','C','D','E'),
'x' = c("t",2,NA,NA,NA),
'y' = c(NA,NA,NA,4,"r"))
Run Code Online (Sandbox Code Playgroud)
显示为
'a' 'x' 'y'
A t NA
B 2 NA
C NA NA
D NA 4
E NA r
Run Code Online (Sandbox Code Playgroud)
要得到
'a' 'mycol'
A t
B 2
C NA
D 4
E r
Run Code Online (Sandbox Code Playgroud)
我试过这个
cbind(data[1], mycol = na.omit(unlist(data[-1])))
Run Code Online (Sandbox Code Playgroud)
但它显然没有保留行NA
。
我只是想使用geopandas
两个多边形区域的并集和交集。我定义:
import geopandas as gpd
from shapely.geometry import Polygon
polys1 = gpd.GeoSeries([Polygon([(0,0), (2,0), (2,2), (0,2)]),
Polygon([(2,2), (4,2), (4,4), (2,4)])])
polys2 = gpd.GeoSeries([Polygon([(1,1), (3,1), (3,3), (1,3)]),
Polygon([(3,3), (5,3), (5,5), (3,5)])])
df1 = gpd.GeoDataFrame({'geometry': polys1, 'df1':[1,2]})
df2 = gpd.GeoDataFrame({'geometry': polys2, 'df2':[1,2]})
Run Code Online (Sandbox Code Playgroud)
我尝试以下方法来获得union
:
res_union = gpd.overlay(df1, df2, how='union')
Run Code Online (Sandbox Code Playgroud)
并且失败并显示以下错误:
AttributeError: 'NoneType' object has no attribute 'intersection'
Run Code Online (Sandbox Code Playgroud)
我正在按照这里的说明进行操作。
我希望 seaborn 热图在热图的每个单元格中显示多个值。这是我想看到的手动示例,只是为了清楚:
data = np.array([[0.000000,0.000000],[-0.231049,0.000000],[-0.231049,0.000000]])
labels = np.array([['A\nExtra Stuff','B'],['C','D'],['E','F']])
fig, ax = plt.subplots()
ax = sns.heatmap(data, annot = labels, fmt = '')
Run Code Online (Sandbox Code Playgroud)
这里以让 seaborn.heatflightsRoundUp
在单元格中显示值为例。
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
def RoundUp(x):
return int(np.ceil(x/10)*10)
# Load the example flights dataset and conver to long-form
flights_long = sns.load_dataset("flights")
flights = flights_long.pivot("month", "year", "passengers")
flightsRoundUp = flights.applymap(RoundUp)
# Draw a heatmap with the numeric values in each cell
f, ax = plt.subplots(figsize=(9, 6))
sns.heatmap(flights, annot=flightsRoundUp, fmt="", …
Run Code Online (Sandbox Code Playgroud) 我有一个包含3列的数据表,我想在彩色2D绘图中根据前两个绘制第三列.例如,对于下表,即
4.0 4.0 0.313660827978
4.0 5.0 0.365348418405
4.0 6.0 0.423733120134
5.0 4.0 0.365348418405
5.0 5.0 0.439599930621
5.0 6.0 0.525083754405
6.0 4.0 0.423733120134
6.0 5.0 0.525083754405
6.0 6.0 0.651536351379
Run Code Online (Sandbox Code Playgroud)
我使用以下代码:
x,y,z = np.loadtxt('output_overlap.dat').T #Transposed for easier unpacking
nrows, ncols = final_step_j-1, final_step_k-1
grid = z.reshape((nrows, ncols))
plt.imshow(grid, extent=(x.min(), x.max(), y.max(), y.min()),
interpolation='nearest',
cmap='binary')
fig1 = plt.gcf()
plt.colorbar()
plt.xlabel('m1')
plt.ylabel('m2')
plt.draw()
fig1.savefig('test.pdf', dpi=100)
close('all')
Run Code Online (Sandbox Code Playgroud)
这给了我以下情节:https: //dl.dropboxusercontent.com/u/31460244/test.png
哪个是对的.现在,我的问题是:如何更改在Y轴上显示数据的顺序?我想在原点得到点(4,4).
我试过改变
plt.imshow(grid, extent=(x.min(), x.max(), y.max(), y.min())
Run Code Online (Sandbox Code Playgroud)
至:
plt.imshow(grid, extent=(x.min(), x.max(), y.min(), y.max())
Run Code Online (Sandbox Code Playgroud)
它确实会更改网格中的数字,但不会更改实际数据.这不是解决方案.有人可以帮我吗?
我有一个包含两列的数据框:
**+-----+-------+
| V1 | V2 |
+-----+---------+
| 1 | a,b,c |
| 2 | a,c |
| 3 | b,d |
| 4 | e |
| . | . |
+-----+-------+**
Run Code Online (Sandbox Code Playgroud)
我想根据第二列内的标记字符将第二列拆分为多列.我希望输出如下.
**+-----+-------------+
| V1 | V2 | V3 | V4 |
+-----+---------------+
| 1 | a | b | c |
| 2 | a | c | NA |
| 3 | b | d | NA |
| 4 | e | NA | …
Run Code Online (Sandbox Code Playgroud) 如何在seaborn dist图中y处于最大值的x位置添加一条垂直线?
import seaborn as sns, numpy as np
sns.set(); np.random.seed(0)
x = np.random.randn(5000)
ax = sns.distplot(x, kde = False)
Run Code Online (Sandbox Code Playgroud)
PS_ 在上面的例子中,我们知道它可能会选择 at 0
。对于任何给定的 x 分布,我很想知道如何找到这个值。
如果有人可以向我描述,我将不胜感激!我的想法即将爆发这种基本的逻辑不一致!
> class(trlog$X.sce_status.[1])
[1] "factor"
> trlog$X.sce_status.[1]
[1] "Successful"
Levels: "Failed-CMD INF ERROR" "Failed-TRANS EXPIRED" "Successful"
> trlog$X.sce_status.[1] == as.character("Successful")
[1] FALSE
Run Code Online (Sandbox Code Playgroud) 如何在打印功能中打印出字符"%".以下行失败.
print "The result is %s out of %s i.e. %d %" % (nominator, denominator, percentage)
Run Code Online (Sandbox Code Playgroud) 我在下面的步骤中使用python中的类但我无法成功:
首先,我在一个名为shape.py的文件中创建了一个类
class Shape:
description = "This shape has not been described yet"
author = "Nobody has claimed to make this shape yet"
def __init__(self,x,y):
self.x = x
self.y = y
def area(self):
return self.x * self.y
def perimeter(self):
return 2 * self.x + 2 * self.y
def describe(self,text):
self.description = text
def authorName(self,text):
self.author = text
def scaleSize(self,scale):
self.x = self.x * scale
self.y = self.y * scale
Run Code Online (Sandbox Code Playgroud)
其次,我去终端打开python.
第三,我输入
import shape
Run Code Online (Sandbox Code Playgroud)
最后,我写道
rectangle = Shape(100, 45)
Run Code Online (Sandbox Code Playgroud)
它不起作用.有什么建议吗?
错误消息是 …
如何将我的 Pandas DataFrame 列的组件从浮点类型转换为实际字符串?
最初,当我read_csv
它们时,它们以科学记数法显示:
例如 1.04983E+13
我用下面这行来改变显示类型以显示所有没有小数的数字:
pd.set_option('display.float_format', lambda x: '%.0f' % x)
Run Code Online (Sandbox Code Playgroud)
最初似乎有效(未显示小数点!):
sample_df['gpid'][0:2]
0 10498310800133
1 767838527881217
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试从中列出一个列表时,它清楚地包含.0
每个组件前面的小数
list(sample_df['gpid'])[0:2]
[10498310800133.0, 767838527881217.0]
Run Code Online (Sandbox Code Playgroud)
我怎样才能避免这种不需要的.0
?【昨天一整天都是烦人的事……】
python ×8
r ×4
pandas ×3
plot ×3
dataframe ×2
matplotlib ×2
seaborn ×2
axis ×1
bootstrap-4 ×1
character ×1
class ×1
decimal ×1
geopandas ×1
gis ×1
github-pages ×1
heatmap ×1
ipython ×1
missing-data ×1
percentage ×1
printing ×1
r-maptools ×1
rgdal ×1