我在miniconda虚拟环境中使用Python 3上的matplotlib版本2.0.0.我正在开发一个unix科学计算集群,我没有root权限.我通常通过ipython笔记本执行python代码.如果我执行基本命令,例如:
import matplotlib.pyplot as plt
plt.scatter([1,5], [1,5])
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息:
path_to_miniconda/miniconda3/envs/conda34/lib/python3.4/site-
packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family
['sans-serif'] not found. Falling back to DejaVu Sans
(prop.get_family(), self.defaultFamily[fontext]))
Run Code Online (Sandbox Code Playgroud)
我希望能够使用Times New Roman字体,但即使删除了我从这里找到的字体缓存文件(fontList.py3k.cache):
import matplotlib as mpl
fm = mpl.font_manager
fm.get_cachedir()
Run Code Online (Sandbox Code Playgroud)
命令:
mpl.rcParams['font.family'] = ['serif']
mpl.rcParams['font.serif'] = ['Times New Roman']
Run Code Online (Sandbox Code Playgroud)
没有效果,我得到与上面相同的错误.真正的字体类型目录:
path_to_miniconda/miniconda3/ENVS/conda34/lib中/ python3.4 /站点包/ matplotlib/MPL-数据/字体/ TTF /
其中只有40种字体:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy
知道可能会发生什么,以及如何添加其他字体?谢谢!
在python中使用Pandas包,我想在一个系列中使用3级多索引对一个级别进行求和(边缘化)以生成具有2级多索引的系列.例如,如果我有以下内容:
ind = [tuple(x) for x in ['ABC', 'ABc', 'AbC', 'Abc', 'aBC', 'aBc', 'abC', 'abc']]
mi = pd.MultiIndex.from_tuples(ind)
data = pd.Series([264, 13, 29, 8, 152, 7, 15, 1], index=mi)
A B C 264
c 13
b C 29
c 8
a B C 152
c 7
b C 15
c 1
Run Code Online (Sandbox Code Playgroud)
我想总结变量C以产生以下输出:
A B 277
b 37
a B 159
b 16
Run Code Online (Sandbox Code Playgroud)
熊猫做这件事的最佳方式是什么?
假设我有一个Pandas DataFrame,我想获得格式[(index1,column1),(index2,column2)......]的元组列表,描述DataFrame中某些条件为真的所有元素的位置.例如:
x = pd.DataFrame(np.random.normal(0, 1, (4,4)), index=['a', 'b', 'c', 'd'],
columns=['e', 'f', 'g', 'h'])
x
e f g h
a -1.342571 -0.274879 -0.903354 -1.458702
b -1.521502 -1.135800 -1.147913 1.829485
c -1.199857 0.458135 -1.993701 -0.878301
d 0.485599 0.286608 -0.436289 -0.390755
y = x > 0
Run Code Online (Sandbox Code Playgroud)
有没有办法获得:
x.loc[y]
Run Code Online (Sandbox Code Playgroud)
回来:
[(b, h), (c,f), (d, e), (d,f)]
Run Code Online (Sandbox Code Playgroud)
还是一些等价的?显然,我可以这样做:
postup = []
for i in x.index:
for j in x.columns:
if x.loc[i, j] > 0:
postup.append((i, j))
Run Code Online (Sandbox Code Playgroud)
但我认为可能/已经实施的更好的东西.在matlab中,函数find与sub2ind结合起来完成工作.
我有一个数据框 x:
x = pd.DataFrame(np.random.randn(3,3), index=[1,2,3], columns=['A', 'B', 'C'])
x
A B C
1 0.256668 -0.338741 0.733561
2 0.200978 0.145738 -0.409657
3 -0.891879 0.039337 0.400449
Run Code Online (Sandbox Code Playgroud)
我想选择一堆索引列对来填充新系列。例如,我可以选择[(1, 'A'), (1, 'B'), (1, 'A'), (3, 'C')]哪个将生成具有 4 个元素的列表、数组或系列:
[0.256668, -0.338741, 0.256668, 0.400449]
Run Code Online (Sandbox Code Playgroud)
知道我应该怎么做吗?
我正在尝试将本地PDF图像文件嵌入到在jupyter-lab中运行的jupyter笔记本中,如下所示:
from IPython.display import IFrame
plot_fn = 'example_data/example_cNMF/example_plot.pdf'
IFrame(plot_fn, width=600, height=400)
Run Code Online (Sandbox Code Playgroud)
您知道发生了什么或如何解决此问题吗?我正在使用Python 3.6,jupyter == 1.0.0 jupyterlab == 0.32.1 matplotlib == 2.2.2 ipython == 6.5.0
假设您有一个区域(开始,结束)坐标的DataFrame和另一个位置的DataFrame,这些位置可能会也可能不会落在给定区域内.例如:
region = pd.DataFrame({'chromosome': [1, 1, 1, 1, 2, 2, 2, 2], 'start': [1000, 2000, 3000, 4000, 1000, 2000, 3000, 4000], 'end': [2000, 3000, 4000, 5000, 2000, 3000, 4000, 5000]})
position = pd.DataFrame({'chromosome': [1, 2, 1, 3, 2, 1, 1], 'BP': [1500, 1100, 10000, 2200, 3300, 400, 5000]})
print region
print position
chromosome end start
0 1 2000 1000
1 1 3000 2000
2 1 4000 3000
3 1 5000 4000
4 2 2000 1000
5 2 3000 2000
6 …Run Code Online (Sandbox Code Playgroud) 我想从数据框中过滤 X 列中包含重复项的行。但是,如果 X 中的某个值存在重复项,我想根据另一列 Y 的值优先选择其中一个值。例如:
import pandas as pd
print pd.__version__
x = pd.DataFrame([
['best', 'a', 'x'],
['worst', 'b', 'y'],
['best', 'c', 'x'],
['worst','d', 'y'],
['best','d', 'y'],
['worst','d', 'y'],
['best','d', 'z'],
['best','d', 'z'],
], columns=['a', 'b', 'c'])
print x
x.drop_duplicates(cols='c', inplace=True)
print x
a b c
0 best a x
1 worst b y
2 best c x
3 worst d y
4 best d y
5 worst d y
6 best d z
7 best d z …Run Code Online (Sandbox Code Playgroud) 我想为数据矩阵制作热图,使得1的所有位置都是红色,2的所有位置都是白色,等等具有任意规格.理想情况下,这应该处理所有值相同的情况,只绘制一个统一的颜色.
我提出的最佳解决方案是使用:
from matplotlib import colors
import matplotlib.pyplot as plt
import numpy as np
cmap = colors.ListedColormap(['white', 'blue', 'red', 'purple'])
data = np.array([[0, 0,0,0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]])
plt.imshow(data, interpolation='none', aspect='auto', origin='upper', cmap=cmap)
Run Code Online (Sandbox Code Playgroud)
它成功地打印出每种颜色的条纹.但是,如果我改为:
dat2 = np.array([[0, 0,0,0], [1, 1, 1, 1]])
plt.imshow(dat2, interpolation='none', aspect='auto', origin='upper', cmap=cmap)
Run Code Online (Sandbox Code Playgroud)
相反,它绘制白色和紫色而不是白色和蓝色.如果数据只包含其中一个数字,则只会绘制白色.
python ×7
pandas ×5
matplotlib ×2
ipython ×1
jupyter ×1
merge ×1
miniconda ×1
multi-index ×1
statistics ×1
truetype ×1