我正在尝试使用matplotlib将标签字体中的刻度标签字体更改为Times New Roman.我认为这应该像更改标题和轴标签的字体一样简单,但事实证明它有点棘手.目前,我只是想设置x-tick标签的字体,这是自动编码的日期(这可能是我的问题之一,但我不确定).
当运行下面的相关代码片段时,我得到错误"没有属性'set_fontproperties'的Axessubplot".
ticks_font = matplotlib.font_manager.FontProperties(family='times new roman', style='normal', size=12, weight='normal', stretch='normal')
fig.autofmt_xdate()
ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
for label in ax.get_xticklabels():
ax.set_fontproperties(ticks_font)
Run Code Online (Sandbox Code Playgroud)
任何帮助是极大的赞赏.
谢谢.
更新/编辑:啊,我觉得自己像个蠢货.刚想通了,一旦我意识到这一点就很明显了.在上面的代码片段中,答案是:
label.set_fontproperties(ticks_font)
Run Code Online (Sandbox Code Playgroud) 当我运行django项目时,我遇到了一个奇怪的问题:
ImportError: /usr/local/lib/python2.7/site-packages/numpy/core/multiarray.so: undefined symbol: PyUnicodeUCS2_AsASCIIString
Run Code Online (Sandbox Code Playgroud)
如果II以dev模式运行此项目(python manage.py runserver 0.0.0.0:8000),则不会发生.但是当我在apache中部署这个项目时,就会出现这个问题.
在这个项目中,我使用nltk包和mongodb,而numpy是一个依赖项.我的操作系统是CentOS 6.3,我将python从2.6.6升级到2.7.3,这是我自己编译的.
按照一些说明,我使用./configure--enable-unicode = ucs2重新编译和重建python.但这似乎不起作用.
所以任何人都知道原因或解决方案?非常感谢!
我正在尝试生成一个字符串,其中包含偶尔带有尾随零的浮点数.这是文本字符串的MWE,我试图用以下方法删除它们{0:g}:
xn, cod = 'r', 'abc'
ccl = [546.3500, 6785.35416]
ect = [12.350, 13.643241]
text = '${}_{{t}} = {0:g} \pm {0:g}\;{}$'.format(xn, ccl[0], ect[0], cod)
print text
Run Code Online (Sandbox Code Playgroud)
不幸的是,这回归
ValueError: cannot switch from automatic field numbering to manual field specification
Run Code Online (Sandbox Code Playgroud)
这个问题使用.format()来格式化具有相同问题报告的字段宽度参数的列表,但我无法弄清楚如何将答案应用于此问题.
我需要生成一个在两个轴上具有相同纵横比且在右侧具有颜色条的图。我尝试过设置aspect='auto'、aspect=1、 ,但aspect='equal'没有好的结果。请参阅下面的示例和 MWE。
使用aspect='auto'颜色条的高度正确,但绘图扭曲:

使用aspect=1或aspect='equal'绘图是方形的(两个轴的长宽相等),但颜色条扭曲:

在这两个图中,由于某种原因,颜色条的位置离右侧太远。如何获得具有匹配高度的颜色条的方形图?
微量元素
import numpy as np
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
def col_plot(params):
gs, i, data = params
xarr, yarr, zarr = zip(*data)[0], zip(*data)[1], zip(*data)[2]
xmin, xmax = min(xarr), max(xarr)
ymin, ymax = min(yarr), max(yarr)
#plt.subplot(gs[i], aspect='auto')
plt.subplot(gs[i], aspect=1)
#plt.subplot(gs[i], aspect='equal')
plt.xlim(xmin, xmax)
plt.ylim(xmin, xmax)
plt.xlabel('$x axis$', fontsize=20)
plt.ylabel('$y axis$', fontsize=20)
# Scatter plot.
cm = plt.cm.get_cmap('RdYlBu_r')
SC = plt.scatter(xarr, …Run Code Online (Sandbox Code Playgroud) 相当简单的问题.说我有一个列表,如:
a = [3, 4, 54, 8, 96, 2]
Run Code Online (Sandbox Code Playgroud)
我可以使用切片在列表中间省略一个元素来生成这样的东西吗?
a[some_slicing]
[3, 4, 8, 96, 2]
Run Code Online (Sandbox Code Playgroud)
这个元素54被遗漏了.我猜想这可以解决问题:
a[:2:]
Run Code Online (Sandbox Code Playgroud)
但结果不是我的预期:
[3, 4]
Run Code Online (Sandbox Code Playgroud) 我试图提高func函数的性能,我发现aX生成列表的方式的简单改变可以提高性能:
import timeit
import numpy as np
def func(a, b):
return [_ for _ in a if _ not in b]
Na, Nb = 10000, 5000
b = list(np.random.randint(1000, size=Nb))
# Ordered list of Na integers
a1 = [_ for _ in range(Na)]
# Random list of Na integers
a2 = list(np.random.randint(Na, size=Na))
# Ordered list of Na integers generated with numpy
a3 = list(np.arange(Na))
start_time = timeit.default_timer()
ab1 = func(a1, b)
abt1 = timeit.default_timer() - …Run Code Online (Sandbox Code Playgroud) 我有两套点的2D A和B我需要找到每个点的最小距离A,在一个点B.到目前为止,我一直在使用SciPy的cdist和下面的代码
import numpy as np
from scipy.spatial.distance import cdist
def ABdist(A, B):
# Distance to all points in B, for each point in A.
dist = cdist(A, B, 'euclidean')
# Indexes to minimum distances.
min_dist_idx = np.argmin(dist, axis=1)
# Store only the minimum distances for each point in A, to a point in B.
min_dists = [dist[i][md_idx] for i, md_idx in enumerate(min_dist_idx)]
return min_dist_idx, min_dists
N = 10000
A = …Run Code Online (Sandbox Code Playgroud) 我有一个简单的脚本:
set term postscript portrait color
set output 'output.ps'
plot 'data_file' using 1:2:3 with image, 'data_file2' using 1:2 with lines
Run Code Online (Sandbox Code Playgroud)
问题是该with image命令使抽动杆在两个轴上都消失了,除非删除该命令,否则我无法使gnuplot显示它们,因为我正在绘制一个命令,所以我无法这样做cbrange(第三列是范围)
谢谢。
我有一个一个维numpy数组,我需要找出是否任何价值为零或非常接近.有了这条线,我可以快速检查零:
if 0. in my_array:
# do something
Run Code Online (Sandbox Code Playgroud)
但是我也有非常小的元素,比如1.e-22,我也想把它视为零(否则我会得到一个零除以前的警告)
假设我的阈值是1.e-6,我想有效地检查我的数组中的任何值是否小于该值.我怎样才能做到这一点?
我有两个来源与赤道坐标(ra, dec)和(ra_0, dec_0)位于距离r和r_0,我需要计算它们之间的3D距离.
我使用两种方法,据我所知,应该给出相同的结果,但不是.
第一种方法是应用astropy的separation_3d函数.第二种方法是使用表达式给出两个具有球面坐标的源之间的距离:
如图所示这里.
在下面的MCVE中,返回的值是:
91.3427173002 pc
93.8470493776 pc
Run Code Online (Sandbox Code Playgroud)
这两个值不应该相等吗?
MCVE:
from astropy.coordinates import SkyCoord
from astropy import units as u
import numpy as np
# Define some coordinates and distances for the sources.
c1 = SkyCoord(ra=9.7*u.degree, dec=-50.6*u.degree, distance=1500.3*u.pc)
c2 = SkyCoord(ra=7.5*u.degree, dec=-47.6*u.degree, distance=1470.2*u.pc)
# Obtain astropy's distance between c1 & c2 coords.
print c1.separation_3d(c2)
# Obtain distance between c1 & …Run Code Online (Sandbox Code Playgroud) python ×9
numpy ×3
matplotlib ×2
performance ×2
arrays ×1
astropy ×1
colorbar ×1
coordinates ×1
django ×1
formatting ×1
gnuplot ×1
plot ×1
scipy ×1
slice ×1
string ×1