是否可以使用Python matplotlib代码在RStudio中绘制图形?
例如,在Python matplotlib代码下面:
import numpy as np
import matplotlib.pyplot as plt
n = 256
X = np.linspace(-np.pi,np.pi,n,endpoint=True)
Y = np.sin(2*X)
plt.plot (X, Y+1, color='blue', alpha=1.00)
plt.plot (X, Y-1, color='blue', alpha=1.00)
plt.show()
Run Code Online (Sandbox Code Playgroud)
输出图将是:
然后我需要写一个R Markdown来包含这些代码并在编织降价后自动生成图形.
我的 juypter 笔记本出现以下错误。我已将 mathplotlib 更新为最新版本,但仍然出现错误
'c' 参数看起来像一个单一的数字 RGB 或 RGBA 序列,应该避免这种情况,因为如果其长度与 'x' 和 'y' 匹配,则值映射将具有优先权。如果您真的想为所有点指定相同的 RGB 或 RGBA 值,请使用单行的二维数组。
X=lab3_data
range_n_clusters = [2, 3, 4, 5, 6,7,8]
for n_clusters in range_n_clusters:
# Create a subplot with 1 row and 2 columns
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.set_size_inches(18, 7)
# The 1st subplot is the silhouette plot
# The silhouette coefficient can range from -1, 1 but in this example all
# lie within [-0.1, 1]
ax1.set_xlim([0, 1])
# The …Run Code Online (Sandbox Code Playgroud) 我正在使用Anaconda虚拟环境设置python项目。我正在生成requirements.txt,以便其他人可以轻松地为项目设置自己的虚拟环境。
我想知道,当其他开发人员想要为该项目做出贡献,但是想要使用virtualenv而不是Anaconda时,他们可以这样做吗?
我尝试了以下方法:
我在Anaconda环境中设置了一个空项目,并安装了aiohttp模块。然后conda list --export > requirements.txt生成以下内容:
# This file may be used to create an environment using:
# $ conda create --name <env> --file <this file>
# platform: win-64
aiohttp=2.3.9=py36_0
async-timeout=2.0.0=py36hc3e01a3_0
certifi=2018.1.18=py36_0
chardet=3.0.4=py36h420ce6e_1
multidict=3.3.2=py36h72bac45_0
pip=9.0.1=py36h226ae91_4
python=3.6.4=h6538335_1
setuptools=38.4.0=py36_0
vc=14=h0510ff6_3
vs2015_runtime=14.0.25123=3
wheel=0.30.0=py36h6c3ec14_1
wincertstore=0.2=py36h7fe50ca_0
yarl=0.14.2=py36h27d1bf2_0
Run Code Online (Sandbox Code Playgroud)我在virtualenv环境中设置了一个空项目,并在那里也安装了aiohttp模块。pip freeze > requirements.txt然后生成:
aiohttp==3.0.1
async-timeout==2.0.0
attrs==17.4.0
chardet==3.0.4
idna==2.6
idna-ssl==1.0.0
multidict==4.1.0
yarl==1.1.0
Run Code Online (Sandbox Code Playgroud)因此,显然两者的输出是不同的,我的理论是:一旦我在项目上使用conda生成了requirements.txt文件,其他开发人员便不能选择virtualenv了-至少如果他们不准备通过以下方式安装长清单要求(当然,不仅仅是aiohttp模块)。
乍看之下,将conda生成的requirements.txt导入virtualenv(pip install -r requirements-conda.txt)上的项目会引发以下错误:
Invalid requirement: 'aiohttp=2.3.9=py36_0'
= is not a valid operator. Did you mean == …Run Code Online (Sandbox Code Playgroud) 我一直在努力对fviz_pca来自 R 包的函数内的输出图中的默认点形状进行更改FactoExtra。
该图以我想要自定义的点形状的特定顺序*出现。
*对应的形状分别为16、17、15、12、0、8
fviz_pca_biplot(PCA, axes = c(1, 2),
label="var", col.var = "black", #setas
geom = "point", pointsize = 2, col.ind=PCADF$groups,
addEllipses = TRUE, ellipse.level = 0.95,
ellipse.type ="confidence", palette = "aaas") + theme_minimal()
Run Code Online (Sandbox Code Playgroud)
我尝试添加到函数中:
geom_point(aes(shape = c(19,20,21,22,23,24)))
Run Code Online (Sandbox Code Playgroud)
它向我返回了一条错误消息:
geom[1] 中的错误:“环境”类型的对象不是可子集的
在函数 fviz_pca 中管理和自定义点形状的任何建议?
我有一个简单的模型,有 1 个主键和 3 个字段(简化):
该模型是通过继承创建的django.db.models。这是最小的可重现代码:
from django.db import models
class QuestionSet(models.Model):
passingscore = models.PositiveSmallIntegerField("passing score")
maxscore = models.PositiveSmallIntegerField("max score")
maxattempt = models.PositiveSmallIntegerField("max attempt")
Run Code Online (Sandbox Code Playgroud)
我想添加一个约束,使其passingscore永远不应大于maxscore数据库中的值。
我使用了跨越多个字段的约束unique_together,例如StackOverflow 上的这个线程。但显然这是一个不同的用例。
我还简单考虑过直接编写 PostgreSQL 代码添加约束:
ALTER TABLE tableB ADD CONSTRAINT score_policy_1 CHECK (maxscore >= passingscore)
Run Code Online (Sandbox Code Playgroud)
但这违背了使用 ORM 的目的,并且违反了“松散耦合”的理念,使得不同数据库后端之间的迁移变得困难。
如果这可能的话,请向我指出在 Django 中编写此约束的更惯用的方法。
这是我的views.py:
class ChoicesViewSet(viewsets.ModelViewSet):
queryset = SingleChoice.objects.all()
serializer_class = SingleChoiceSerializer
...
class AssessmentTakersViewSet(viewsets.ModelViewSet):
queryset = AssessmentTaker.objects.all()
serializer_class = AssessmentTakersSerializer
...
@api_view(['POST'])
@parser_classes((JSONParser,))
def studio_create_view(request, format=None):
""""
A view that accept POST request with JSON content and in turn build out the
questions and choices. Post with application/json type.
"""
...
Run Code Online (Sandbox Code Playgroud)
这是我的urls.py:
urlpatterns = [
# http://localhost:8000/survey/api/studio-create
path('api/studio-create', views.studio_create_view, name='studio-create-api'),
]
# drf config
router = routers.DefaultRouter()
router.register('api/choices', views.ChoicesViewSet)
router.register('api/assessment-takers', views.AssessmentTakersViewSet)
urlpatterns += router.urls
Run Code Online (Sandbox Code Playgroud)
这在功能上有效,并且被认为是功能完整的,但由于studio-create_view未在路由器中注册,因此该路径不会显示在 API …
我有一个作业需要调用Networkx的函数来获取所有节点的度数,然后为这些度数绘制一个箱线图。
但箱线图未显示,并出现以下错误:
“ Degree_values = list(my_ Degrees.values());
AttributeError:“DegreeView”对象没有属性“值””
如何解决这个问题呢?谢谢。
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_edges_from([['9606.EN01','9606.EN02'],['9606.EN01','9606.EN03']])
fig = plt.figure();
nx.draw(G, with_labels=True, font_weight='bold')
plt.draw()
my_degrees = G.degree();
degree_values = list(my_degrees.values());
fig = plt.figure();
plt.boxplot(degree_values)
Run Code Online (Sandbox Code Playgroud) 几天以来,我不时在RStudio中收到以下错误消息,无法弄清楚是什么原因引起的。
当我在控制台窗口中写入以寻址data.frame,然后在$中写入data.frame中的特定列(例如df$SomeVariable)时,以下消息显示在控制台窗口中,并在每个字母I的上方打印类型
Error in gsub(reStrip, "", completions, perl = TRUE) :
input string 38 is invalid UTF-8
Run Code Online (Sandbox Code Playgroud)
该错误消息没有任何实际效果。除了自动完成变量名之外,其他一切都很好。我在Windows计算机上使用R版本3.4.4和RStudio版本1.0.143。在当前正在使用的R脚本中,我不使用gsub或任何其他“字符串”或正则表达式函数。该问题出现在各种data.frames和data.frames中的各种类型的变量(数字,整数,日期,因子等)上。各种程序包也会发生这种情况。当前,我正在使用以下软件包的组合:readr,dplyr,plm,lfe,readstata13,infuser和RPostgres。关闭RStudio并再次打开它后,问题消失了一段时间,但工作了一段时间后,问题再次出现。
有谁知道这可能是什么原因以及如何解决?
根据R手册,
运算符< - 可以在任何地方使用,而operator =仅允许在顶层(例如,在命令提示符下键入的完整表达式中)
所以,我想RM(名单<-ls()),因为< - 如上面所述的操作可以在任何地方使用,但[R给了我一个错误说"......必须包含名称或字符串".
rm(list = ls())没有任何问题.据我所知,< - 是一个更普遍的任务,而=可以在更严格的情况下使用.但是为什么使用< - 在'rm'函数时会出错?