我需要帮助设置matplotlib上的y轴限制.这是我尝试过的代码,但没有成功.
import matplotlib.pyplot as plt
plt.figure(1, figsize = (8.5,11))
plt.suptitle('plot title')
ax = []
aPlot = plt.subplot(321, axisbg = 'w', title = "Year 1")
ax.append(aPlot)
plt.plot(paramValues,plotDataPrice[0], color = '#340B8C',
marker = 'o', ms = 5, mfc = '#EB1717')
plt.xticks(paramValues)
plt.ylabel('Average Price')
plt.xlabel('Mark-up')
plt.grid(True)
plt.ylim((25,250))
Run Code Online (Sandbox Code Playgroud)
根据我对该图的数据,我得到20和200的y轴限制.但是,我想要限制20和250.
在Python中,[2]列表在哪里,以下代码给出了这个输出:
[2] * 5 # Outputs: [2,2,2,2,2]
Run Code Online (Sandbox Code Playgroud)
使用JavaScript中的数组是否存在一种简单的方法?
我写了以下函数来做到这一点,但是有更短或更好的东西吗?
var repeatelem = function(elem, n){
// returns an array with element elem repeated n times.
var arr = [];
for (var i = 0; i <= n; i++) {
arr = arr.concat(elem);
};
return arr;
};
Run Code Online (Sandbox Code Playgroud) 我想在多个行中分割R脚本中的一行(因为它太长了).我怎么做?
具体来说,我有一条线,如
setwd('~/a/very/long/path/here/that/goes/beyond/80/characters/and/then/some/more')
Run Code Online (Sandbox Code Playgroud)
是否有可能将长路径分成多条线?我试过了
setwd('~/a/very/long/path/here/that/goes/beyond/80/characters/and/
then/some/more')
Run Code Online (Sandbox Code Playgroud)
用return键在第一行的末尾; 但这不起作用.
谢谢.
有没有办法初始化一个形状的numpy数组并添加到它?我将用列表示例解释我需要什么.如果我想创建一个循环中生成的对象列表,我可以这样做:
a = []
for i in range(5):
a.append(i)
Run Code Online (Sandbox Code Playgroud)
我想用numpy数组做类似的事情.我知道vstack,连接等等.但是,似乎这些需要两个numpy数组作为输入.我需要的是:
big_array # Initially empty. This is where I don't know what to specify
for i in range(5):
array i of shape = (2,4) created.
add to big_array
Run Code Online (Sandbox Code Playgroud)
本big_array应具有的形状(10,4).这该怎么做?
编辑:
我想补充以下说明.我知道我可以定义big_array = numpy.zeros((10,4))然后填写它.但是,这需要提前指定big_array的大小.我知道这种情况下的大小,但如果我不知道怎么办?当我们使用.append函数在python中扩展列表时,我们不需要事先知道它的最终大小.我想知道是否有类似的东西从较小的数组创建一个更大的数组,从一个空数组开始.
我有一个名称的csv文件params.csv.我打开ipython qtconsole并用以下方法创建了一只熊猫dataframe:
import pandas
paramdata = pandas.read_csv('params.csv', names=paramnames)
Run Code Online (Sandbox Code Playgroud)
其中,paramnames是一个字符串对象的python列表.示例paramnames(实际列表的长度为22):
paramnames = ["id",
"fc",
"mc",
"markup",
"asplevel",
"aspreview",
"reviewpd"]
Run Code Online (Sandbox Code Playgroud)
在ipython提示符下,如果我输入paramdata并按回车键,那么我不会获得带有列和值的数据框,如Pandas网站上的示例所示.相反,我获得有关数据帧的信息.我明白了:
In[35]: paramdata
Out[35]:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 59 entries, 0 to 58
Data columns:
id 59 non-null values
fc 59 non-null values
mc 59 non-null values
markup 59 non-null values
asplevel 59 non-null values
aspreview 59 non-null values
reviewpd 59 non-null values
Run Code Online (Sandbox Code Playgroud)
如果我键入,paramdata['mc']那么我确实得到了mc列的预期值.我有两个问题:
(1)在pandas网站上的例子中(例如,参见 …
我在代码中遇到了python中的未绑定方法错误
class Sample(object):
'''This class defines various methods related to the sample'''
def drawSample(samplesize,List):
sample=random.sample(List,samplesize)
return sample
Choices=range(100)
print Sample.drawSample(5,Choices)
Run Code Online (Sandbox Code Playgroud)
在这里阅读了很多有用的帖子之后,我想到了如何在上面添加@staticmethod以使代码正常工作.我是python新手.有人可以解释为什么人们想要定义静态方法吗?或者,为什么并非所有方法都定义为静态方法?
有人可以解释为什么设置图形的facecolor时下面的代码不起作用?
import matplotlib.pyplot as plt
# create figure instance
fig1 = plt.figure(1)
fig1.set_figheight(11)
fig1.set_figwidth(8.5)
rect = fig1.patch
rect.set_facecolor('red') # works with plt.show().
# Does not work with plt.savefig("trial_fig.png")
ax = fig1.add_subplot(1,1,1)
x = 1, 2, 3
y = 1, 4, 9
ax.plot(x, y)
# plt.show() # Will show red face color set above using rect.set_facecolor('red')
plt.savefig("trial_fig.png") # The saved trial_fig.png DOES NOT have the red facecolor.
# plt.savefig("trial_fig.png", facecolor='red') # Here the facecolor is red.
Run Code Online (Sandbox Code Playgroud)
当我指定使用fig1.set_figheight(11) fig1.set_figwidth(8.5)这些图的高度和宽度时,由命令拾取plt.savefig("trial_fig.png") …
我有一个2D numpy数组.这个数组中的一些值是NaN.我想使用这个数组执行某些操作.例如,考虑数组:
[[ 0. 43. 67. 0. 38.]
[ 100. 86. 96. 100. 94.]
[ 76. 79. 83. 89. 56.]
[ 88. NaN 67. 89. 81.]
[ 94. 79. 67. 89. 69.]
[ 88. 79. 58. 72. 63.]
[ 76. 79. 71. 67. 56.]
[ 71. 71. NaN 56. 100.]]
Run Code Online (Sandbox Code Playgroud)
我试图逐行取一行,按相反顺序对其进行排序,以从行中获取最多3个值并取其平均值.我试过的代码是:
# nparr is a 2D numpy array
for entry in nparr:
sortedentry = sorted(entry, reverse=True)
highest_3_values = sortedentry[:3]
avg_highest_3 = float(sum(highest_3_values)) / 3
Run Code Online (Sandbox Code Playgroud)
这不适用于包含的行NaN.我的问题是,是否有一种快速方法可以NaN …
有没有办法在循环中在python中生成变量名并为它们赋值?例如,如果我有
prices = [5, 12, 45]
Run Code Online (Sandbox Code Playgroud)
我想要
price1 = 5
price2 = 12
price3 = 45
Run Code Online (Sandbox Code Playgroud)
我可以在一个循环或某事做,而不是手动分配price1 = prices[0],price2 = prices[1]等等.
谢谢.
编辑
很多人建议我写一个要求这个的理由.首先,有些时候我认为这可能比使用列表更方便...我不记得确切的时间,但我想我已经想过在有很多级别的嵌套时使用它.例如,如果有一个列表列表,以上述方式定义变量可能有助于降低嵌套级别.其次,今天我在尝试学习使用Pytables时想到了这一点.我刚看到Pytables,我看到在定义表结构时,列名和类型按以下方式描述:
class TableFormat(tables.IsDescription):
firstColumnName = StringCol(16)
secondColumnName = StringCol(16)
thirdColumnName = StringCol(16)
Run Code Online (Sandbox Code Playgroud)
如果我有100列,则明确键入每列的名称似乎很多工作.所以,我想知道是否有办法动态生成这些列名.
我知道我可以用map以下方式使用一个变量的函数:
var squarefunc = function(x) {
return x*x;
};
values = [1,2,3,4]
values.map(squarefunc) // returns [1,4,9,16]
Run Code Online (Sandbox Code Playgroud)
如何使用map以下功能:
var squarefuncwithadjustment = function(x, adjustment) {
return (x*x + adjustment);
}
Run Code Online (Sandbox Code Playgroud)
其中,我想adjustment在调用map时手动输入参数值,比如说adjustment=2,并且x从数组中获取值values.
python ×7
arrays ×2
javascript ×2
matplotlib ×2
numpy ×2
multiline ×1
nan ×1
pandas ×1
r ×1
repeat ×1