假设我有以下2D numpy数组,包括四行和三列:
>>> a = numpy.arange(12).reshape(4,3)
>>> print(a)
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
Run Code Online (Sandbox Code Playgroud)
什么是生成包含所有列的总和的一维数组的有效方法(如[18, 22, 26])?这可以在不需要遍历所有列的情况下完成吗?
如果您有多个包含辅助y轴的子图(使用twinx创建),那么如何在子图之间共享这些辅助y轴?我希望它们能够以自动方式均等地扩展(因此不能手动设置y限制).对于主y轴,可以通过在子图调用中使用关键字sharey来实现.
下面的示例显示了我的尝试,但它无法共享两个子图的辅助y轴.我正在使用Matplotlib/Pylab:
ax = []
#create upper subplot
ax.append(subplot(211))
plot(rand(1) * rand(10),'r')
#create plot on secondary y-axis of upper subplot
ax.append(ax[0].twinx())
plot(10*rand(1) * rand(10),'b')
#create lower subplot and share y-axis with primary y-axis of upper subplot
ax.append(subplot(212, sharey = ax[0]))
plot(3*rand(1) * rand(10),'g')
#create plot on secondary y-axis of lower subplot
ax.append(ax[2].twinx())
#set twinxed axes as the current axes again,
#but now attempt to share the secondary y-axis
axes(ax[3], sharey = ax[1])
plot(10*rand(1) * …Run Code Online (Sandbox Code Playgroud) 我在win-8使用python 3.4.我想从python代码获取.exe程序.我了解到它可以通过cx_Freeze来完成.
在MS-DOS命令行中,我写了pip install cx_Freeze来设置cx_Freeze.它已安装但无法正常工作.
(当我将cxfreeze写入命令行时,我收到此警告:C:\ Users\USER> cxfreeze'cxfreeze'未被识别为内部或外部命令,可运行程序或批处理文件.)
(我还通过环境变量将cxfreeze的位置添加到"PATH")
任何帮助都会受到感谢.
我想知道是否有人有问题Matplotlib的盒子情节传单没有显示?
我把这个例子复制粘贴到python脚本中:http: //blog.bharatbhole.com/creating-boxplots-with-matplotlib/
...但是盒子图传单(异常值)没有显示.有谁知道为什么我可能不会看到它们?对不起,如果这是一个愚蠢的问题,但我不能为我的生活弄清楚为什么它不起作用.
## Create data
np.random.seed(10)
collectn_1 = np.random.normal(100, 10, 200)
collectn_2 = np.random.normal(80, 30, 200)
collectn_3 = np.random.normal(90, 20, 200)
collectn_4 = np.random.normal(70, 25, 200)
## combine these different collections into a list
data_to_plot = [collectn_1, collectn_2, collectn_3, collectn_4]
# Create a figure instance
fig = plt.figure(1, figsize=(9, 6))
# Create an axes instance
ax = fig.add_subplot(111)
# Create the boxplot
bp = ax.boxplot(data_to_plot)
Run Code Online (Sandbox Code Playgroud)
我也尝试添加showfliers=True到该脚本的最后一行,但它仍然无法正常工作.
这是我得到的输出:

我正在解析一个巨大的ascii文件,其中包含分配给条目的日期.所以,我发现自己使用与numpy.datetime64并行的datetime包来添加数组功能.我知道pandas包可能最适合用于约会,但是尝试在没有熊猫的情况下将其拉出来.我一直在寻找一种巧妙的方法来添加/减去某个日期步骤,例如一年或者3个月的datetime64对象.
目前,我正在将dt64对象转换为dt对象,并使用replace函数来更改年份,例如,之后必须将其转换回dt64,这对我来说有点麻烦.所以,如果有人只使用numpy.datetime64格式有更好的解决方案,我将不胜感激.
示例:将"YYYY-12-31"转换为"(YYYY-1)-12-31"
a = np.datetime64(2014,12,31) # a is dt64 object
b = a.astype(object) # b is dt object converted from a
c = np.datetime64( b.replace(b.year-1)) # c is dt64 object shifted back 1 year (a -1year)
Run Code Online (Sandbox Code Playgroud) 假设我们有以下pandas DataFrame:
In [1]:
import pandas as pd
import numpy as np
df = pd.DataFrame([0, 1, 0, 0, 1, 1, 0, 1, 1, 1], columns=['in'])
df
Out[1]:
in
0 0
1 1
2 0
3 0
4 1
5 1
6 0
7 1
8 1
9 1
Run Code Online (Sandbox Code Playgroud)
如何在熊猫中以矢量化方式计算连续数?我希望得到这样的结果:
in out
0 0 0
1 1 1
2 0 0
3 0 0
4 1 1
5 1 2
6 0 0
7 1 1
8 1 2 …Run Code Online (Sandbox Code Playgroud) 当我使用 pandas read_csv 读取具有时区感知日期时间的列(并将此列指定为索引)时,pandas 将其转换为时区天真 utc DatetimeIndex。
Test.csv 中的数据:
DateTime,Temperature
2016-07-01T11:05:07+02:00,21.125
2016-07-01T11:05:09+02:00,21.138
2016-07-01T11:05:10+02:00,21.156
2016-07-01T11:05:11+02:00,21.179
2016-07-01T11:05:12+02:00,21.198
2016-07-01T11:05:13+02:00,21.206
2016-07-01T11:05:14+02:00,21.225
2016-07-01T11:05:15+02:00,21.233
从 csv 读取的代码:
In [1]: import pandas as pd
In [2]: df = pd.read_csv('Test.csv', index_col=0, parse_dates=True)
Run Code Online (Sandbox Code Playgroud)
这会产生一个表示时区天真的 UTC 时间的索引:
In [3]: df.index
Out[3]: DatetimeIndex(['2016-07-01 09:05:07', '2016-07-01 09:05:09',
'2016-07-01 09:05:10', '2016-07-01 09:05:11',
'2016-07-01 09:05:12', '2016-07-01 09:05:13',
'2016-07-01 09:05:14', '2016-07-01 09:05:15'],
dtype='datetime64[ns]', name='DateTime', freq=None)
Run Code Online (Sandbox Code Playgroud)
我尝试使用 date_parser 函数:
In [4]: date_parser = lambda x: pd.to_datetime(x).tz_localize(None)
In [5]: df = pd.read_csv('Test.csv', index_col=0, parse_dates=True, date_parser=date_parser)
Run Code Online (Sandbox Code Playgroud)
这给出了相同的结果。 …
python ×7
matplotlib ×2
numpy ×2
pandas ×2
batch-file ×1
boxplot ×1
cx-freeze ×1
date ×1
datetime ×1
datetime64 ×1
python-3.x ×1
seaborn ×1