我来自这个问题Matplotlib:两个x轴和两个y轴我在那里学习了如何在同一个图上绘制两个x和y轴.
这是一个MWE:
import matplotlib.pyplot as plt
import numpy as np
# Generate random data.
x1 = np.random.randn(50)
y1 = np.linspace(0, 1, 50)
x2 = np.random.randn(20)+15.
y2 = np.linspace(10, 20, 20)
# Plot both curves.
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xlabel('x_1')
ax1.set_ylabel('y_1')
plt.plot(x1, y1, c='r')
ax2 = ax1.twinx().twiny()
ax2.set_xlabel('x_2')
ax2.set_ylabel('y_2')
plt.ylim(min(y2), max(y2))
ax2.plot(x2, y2, c='b')
plt.show()
Run Code Online (Sandbox Code Playgroud)
这是输出:

右y轴和顶x轴对应于蓝色曲线.
如您所见,y即使定义了第二个标签,也会丢失第二个标签.我已经尝试了许多不同的方法,但我无法让它显示出来.难道我做错了什么?
加:
显然这条线存在问题:
ax2 = ax1.twinx().twiny()
Run Code Online (Sandbox Code Playgroud)
如果我这样颠倒它:
ax2 …Run Code Online (Sandbox Code Playgroud) 我从这个问题中学到了Matplotlib:更改数学字体大小如何更改数学文本的默认大小matplotlib.我所做的是:
from matplotlib import rcParams
rcParams['mathtext.default'] = 'regular'
Run Code Online (Sandbox Code Playgroud)
这有效地使LaTeX字体与常规字体大小相同.
我不知道的是如何将其重置为默认行为,即:LaTeX字体看起来比常规字体小一点.
我需要这个,因为我希望LaTeX字体看起来只与一个图上的常规字体大小相同,而不是我图中所有使用LaTex数学格式的图.
这是MWE我如何创建我的数字:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.gridspec as gridspec
# Generate random data.
x = np.random.randn(60)
y = np.random.randn(60)
fig = plt.figure(figsize=(5, 10)) # create the top-level container
gs = gridspec.GridSpec(6, 4) # create a GridSpec object
ax0 = plt.subplot(gs[0:2, 0:4])
plt.scatter(x, y, s=20, label='aaa$_{subaaa}$')
handles, labels = ax0.get_legend_handles_labels()
ax0.legend(handles, labels, loc='upper right', numpoints=1, fontsize=14)
plt.ylabel('A$_y$', fontsize=16) …Run Code Online (Sandbox Code Playgroud) 假设我有一个由4个子图组成的图像,如下所示:
将matplotlib.pyplot导入为plt import numpy as np
# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
# Four axes, returned as a 2-d array
f, axarr = plt.subplots(2, 2)
axarr[0, 0].plot(x, y)
axarr[0, 0].set_title('Axis [0,0]')
axarr[0, 1].scatter(x, y)
axarr[0, 1].set_title('Axis [0,1]')
axarr[1, 0].plot(x, y ** 2)
axarr[1, 0].set_title('Axis [1,0]')
axarr[1, 1].scatter(x, y ** 2)
axarr[1, 1].set_title('Axis [1,1]')
# Fine-tune figure; hide x ticks for top plots and y ticks for …Run Code Online (Sandbox Code Playgroud) 在这个问题中,如何重命名 Git 标签?给出了重命名旧标签的说明。
我想知道这些步骤将保留已编辑标签中的原始日期。
例如,在我的gitrepo 中,git tag命令显示:
v0.1
v1.0.0-beta
Run Code Online (Sandbox Code Playgroud)
和 Github 显示:

我想将旧v0.1标签更新为更具描述性的v1.0.0-alpha(也遵循语义版本控制指南),但我也想保留原始日期(2013 年 12 月 4 日)。
是否可以使用上述问题的答案中给出的命令?据我了解,这些将是我的情况下的命令:
git tag v1.0.0-alpha v0.1
git tag -d v0.1
git push origin :refs/tags/v0.1
git push --tags
Run Code Online (Sandbox Code Playgroud) 我需要将几个浮点数写入我正在使用该format()方法的文件。我想要的是将浮点数四舍五入到给定的小数位数并同时将它们对齐。
这是一个 MWE:
a = 546.35642
b = 6785.35416
c = 12.5235
d = 13.643241
line = [str('{:.2f}'.format(a)),
str('{:.4f}'.format(b)),
str('{:.5f}'.format(c)),
str('{:.3f}'.format(d))]
with open('format_test.dat', "a") as f_out:
f_out.write('''{:>10} {:>15} {:>16} {:>15}'''.format(*line))
f_out.write('\n')
Run Code Online (Sandbox Code Playgroud)
这完成了工作,但对我来说似乎非常复杂。有没有更好的方法来做到这一点format()?
我有一组N观察(x[i], y[i]), i=0..N在2D空间中作为点分布.每个点都有坐标(e_x[i], e_y[i], i=0..N)中的相关错误以及附加到它的权重(w[i], i=0..N).
我想这些生成的二维直方图N百分点,占不仅为权重,但也为错误,这将导致各点进行传播可能很多箱中如果误差值足够大(假设一个标准高斯分布对于错误,尽管可能会考虑其他分布).
我看到numpy.histogram2d有一个weights参数,所以这是照顾.问题是如何解释每个N观察点的错误.
有没有让我这样做的功能?我愿意在任何事情numpy和scipy.
追溯ValueError: cannot convert float NaN to integer我发现的那条线:
max('a', 5)
max(5, 'a')
Run Code Online (Sandbox Code Playgroud)
将返回a而不是5.
在上面的例子中,我使用了示例字符串,a但在我的实际情况中,字符串是a NaN(未能收敛的拟合过程的结果).
这种行为背后的理由是什么?为什么python不会自动识别出那里有一个字符串并且它应该返回该数字?
更好奇的是,min() 没有工作,因为预期:
min('a', 5)
min(5, 'a')
Run Code Online (Sandbox Code Playgroud)
回报5.
我有一个很大的代码,可以输出多个图像。有时,情节看起来像这样(请参见下面的MWE):

请注意,x轴上的刻度线几乎彼此重叠,这看起来很糟糕。
我的问题是:为什么matplotlib无法实现这一点,而只是绘制较少的滴答声(通常不会出现问题)?我该如何强制matplotlib以一般方式绘制较少的滴答声(即:我不是只寻求适用于该特定绘图的解决方案,这只是一个示例)?
MWE(如果代码看起来不必要地复杂,那是因为它只是较大代码中的一小部分)
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import random
def scatter_plot(x, y):
ax = plt.subplot(gs[0:2, 0:2])
plt.xlim(min(x), 0.00158)
plt.xlabel('$x$', fontsize=16)
plt.ylabel('$y$', fontsize=16)
ax.minorticks_on()
plt.scatter(x, y)
# Generate random data.
x = [random.random() / 100. for i in xrange(10000)]
y = [random.random() for i in xrange(10000)]
# Define size of output figure.
fig = plt.figure(figsize=(30, 25)) # create the top-level container
gs = gridspec.GridSpec(10, 12) # create a GridSpec object …Run Code Online (Sandbox Code Playgroud) 我希望能够geom_smooth在ggplot中使用.但是,当我输入时conda install ggplot,我收到错误no packages found in current win-32 channels matching ggplot.有谁知道发生了什么?
见下面的MWE:
import matplotlib.pyplot as plt
import matplotlib.offsetbox as offsetbox
fig = plt.figure()
ax = fig.add_subplot(111)
text1 = r'$a\,=\,{}\pm{}$'.format(0.01, 0.002) # Works.
text2 = r'$a\;=\,{}\pm{}$'.format(0.01, 0.002) # Works.
text3 = r'$a\:=\,{}\pm{}$'.format(0.01, 0.002) # Does not work.
text = text3
ob = offsetbox.AnchoredText(text, pad=1, loc=6, prop=dict(size=13))
ob.patch.set(alpha=0.85)
ax.add_artist(ob)
plt.show()
Run Code Online (Sandbox Code Playgroud)
虽然text1和text2它的使用\,和\;间距将工作,没有问题,text3它使用\:(即:位于水平间距前两个之间)失败了ValueError:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_gtk3agg.py", line 42, in on_draw_event
self._render_figure(w, h)
File …Run Code Online (Sandbox Code Playgroud)