我有两个图表,其中两个具有相同的x轴,但具有不同的y轴标度.
具有常规轴的图是具有描绘衰减的趋势线的数据,而y半对数缩放描绘了拟合的准确性.
fig1 = plt.figure(figsize=(15,6))
ax1 = fig1.add_subplot(111)
# Plot of the decay model
ax1.plot(FreqTime1,DecayCount1, '.', color='mediumaquamarine')
# Plot of the optimized fit
ax1.plot(x1, y1M, '-k', label='Fitting Function: $f(t) = %.3f e^{%.3f\t} \
%+.3f$' % (aR1,kR1,bR1))
ax1.set_xlabel('Time (sec)')
ax1.set_ylabel('Count')
ax1.set_title('Run 1 of Cesium-137 Decay')
# Allows me to change scales
# ax1.set_yscale('log')
ax1.legend(bbox_to_anchor=(1.0, 1.0), prop={'size':15}, fancybox=True, shadow=True)
Run Code Online (Sandbox Code Playgroud)
现在,我正试着像这个链接http://matplotlib.org/examples/pylab_examples/subplots_demo.html提供的示例一样努力实现两者.
特别是这一个
在查看示例的代码时,我对如何植入3件事情感到有点困惑:
1)以不同方式缩放轴
2)保持指数衰减图的图形大小相同,但是线图具有较小的y尺寸和相同的x尺寸.
例如:
3)保持函数的标签仅出现在衰减图中.
非常感激任何的帮助.
在这里,我有一个参数方程.
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
t = np.linspace(0,2*np.pi, 40)
# Position Equation
def rx(t):
return t * np.cos(t)
def ry(t):
return t * np.sin(t)
# Velocity Vectors
def vx(t):
return np.cos(t) - t*np.sin(t)
def vy(t):
return np.sin(t) + t*np.cos(t)
# Acceleration Vectors
def ax(t):
return -2*np.sin(t) - t*np.cos(t)
def ay(t):
return 2*np.cos(t) - t*np.sin(t)
fig = plt.figure()
ax1 = fig.gca(projection='3d')
z = t
ax1.plot(rx(z), r(z), z)
plt.xlim(-2*np.pi,2*np.pi)
plt.ylim(-6,6)
ax.legend()
Run Code Online (Sandbox Code Playgroud)
所以我有这个参数方程来创建这个图.
我在我的代码中定义了上面的速度和加速度参数方程.
我想要做的是在定义的点上绘制我的位置图中的加速度和速度矢量.(Id …
抱歉.我刚刚学习Python以及与数据分析有关的一切.
我怎么能用Spyder打开一个.npy文件?或者我必须使用其他程序?我正在使用Mac,如果这完全相关的话.
我有一大堆元素,我RelDist在模拟体积中称之为(在尺寸上,是一个距离的单位).我试图确定"每单位体积的数量"的分布,这也是数密度.它应该类似于这个图:
在数学上,我将其设置为两个等效方程式:
其中N是相对于距离的自然对数而被区分的阵列中的元素的数量.它也可以通过引入r的另一个因子等价地以常规导数的形式重写.
同样地,
因此,对于不断增加的r,我想计算r的每个对数bin的元素N的变化.
截至目前,我在直方图中设置频率计数时遇到了问题,同时容纳了它旁边的音量.
尝试1
这是使用dN/dlnr /体积方程
def n(dist, numbins):
logdist= np.log(dist)
hist, r_array = np.histogram(logdist, numbins)
dlogR = r_array[1]-r_array[0]
x_array = r_array[1:] - dlogR/2
## I am condifent the above part of this code is correct.
## The succeeding portion does not work.
dR = r_array[1:] - r_array[0:numbins]
dN_dlogR = hist * x_array/dR
volume = 4*np.pi*dist*dist*dist
## The included volume is incorrect
return [x_array, dN_dlogR/volume]
Run Code Online (Sandbox Code Playgroud)
绘制它甚至没有正确地显示像我上面发布的第一个图的分布,它只有在我选择bin编号与我的输入数组相同的形状时才有效.包子号应该是任意的,不是吗?
尝试2
这是使用等效的dN/dr /体积方程.
numbins = np.linspace(min(RelDist),max(RelDist), 100)
hist, …Run Code Online (Sandbox Code Playgroud) 有一个散点图的示例代码及其直方图
x = np.random.rand(5000,1)
y = np.random.rand(5000,1)
fig = plt.figure(figsize=(7,7))
ax = fig.add_subplot(111)
ax.scatter(x, y, facecolors='none')
ax.set_xlim(0,1)
ax.set_ylim(0,1)
fig1 = plt.figure(figsize=(7,7))
ax1 = fig1.add_subplot(111)
ax1.hist(x, bins=25, fill = None, facecolor='none',
edgecolor='black', linewidth = 1)
fig2 = plt.figure(figsize=(7,7))
ax2 = fig2.add_subplot(111)
ax2.hist(y, bins=25 , fill = None, facecolor='none',
edgecolor='black', linewidth = 1)
Run Code Online (Sandbox Code Playgroud)
我想要做的是创建这个图形,直方图附加到他们尊重的轴上,就像这个例子一样
我熟悉堆叠和合并x轴
f, (ax1, ax2, ax3) = plt.subplots(3)
ax1.scatter(x, y)
ax2.hist(x, bins=25, fill = None, facecolor='none',
edgecolor='black', linewidth = 1)
ax3.hist(y, bins=25 , fill = None, facecolor='none',
edgecolor='black', linewidth …Run Code Online (Sandbox Code Playgroud) 我在一个 csv 文件中有数据,看起来像这样导入。
import csv
with open('Half-life.csv', 'r') as f:
data = list(csv.reader(f))
Run Code Online (Sandbox Code Playgroud)
数据将作为这个输出到它打印出诸如此类的行的地方data[0] = ['10', '2', '2']。
我想要的是将数据作为列而不是行检索,在这种情况下,有 3 列。
我知道有与此相关的线程,但是我对我想要将数据适合的位置感到困惑。
我的数据就这样导入并绘制了。
import matplotlib.pyplot as plt
%matplotlib inline
import pylab as plb
import numpy as np
import scipy as sp
import csv
FreqTime1 = []
DecayCount1 = []
with open('Half_Life.csv', 'r') as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
FreqTime1.append(row[0])
DecayCount1.append(row[3])
FreqTime1 = np.array(FreqTime1)
DecayCount1 = np.array(DecayCount1)
fig1 = plt.figure(figsize=(15,6))
ax1 = fig1.add_subplot(111)
ax1.plot(FreqTime1,DecayCount1, ".", label = 'Run 1')
ax1.set_xlabel('Time (sec)')
ax1.set_ylabel('Count')
plt.legend()
Run Code Online (Sandbox Code Playgroud)
问题是,我在设置一般指数衰减时遇到困难,其中我不确定如何从数据集中计算参数值。
如果可能的话,我也想让拟合衰减方程的方程与图形一起显示。但是,如果能够产生配合,则可以很容易地应用它。
编辑 ------------------------------------------------- ------------
所以当使用Stanely R提到的拟合函数时
def model_func(x, a, k, b):
return a * …Run Code Online (Sandbox Code Playgroud) 我想从直方图中删除垂直条纹轮廓,但保留直方图的"蚀刻",如果这样做的话.
import matplotlib.pyplot as plt
import numpy as np
bins = 35
fig = plt.figure(figsize=(7,6))
ax = fig.add_subplot(111)
ax.hist(subVel_Hydro1, bins=bins, facecolor='none',
edgecolor='black', label = 'Pecuiliar Vel')
ax.set_xlabel('$v_{_{B|A}} $ [$km\ s^{-1}$]', fontsize = 16)
ax.set_ylabel(r'$P\ (r_{_{B|A}} )$', fontsize = 16)
ax.legend(frameon=False)
Run Code Online (Sandbox Code Playgroud)
给予
这在matplotlibs直方图功能中是否可行?我希望我提供了足够的清晰度.
我正在尝试histogram2d合并颜色条对数值。
这是我当前的代码:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.colors import LinearSegmentedColormap
cmap = LinearSegmentedColormap.from_list('mycmap', ['black', 'maroon',
'crimson', 'orange', 'white'])
fig = plt.figure()
ax = fig.add_subplot(111)
H = ax.hist2d(gas_pos[:,0]/0.7, gas_pos[:,1]/0.7, cmap=cmap,
norm=matplotlib.colors.LogNorm(), bins=350, weights=np.log(gas_Temp))
ax.tick_params(axis=u'both', which=u'both',length=0)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
cb = fig.colorbar(H[3], ax=ax, shrink=0.8, pad=0.01,
orientation="horizontal", label=r'$\log T\ [\mathrm{K}]$')
cb.ax.set_xticklabels([1,2,3,4])
cb.update_ticks()
empty = Rectangle((0,0 ), 0, 0, alpha=0.0)
redshift = fig.legend([empty], [r'$z = 127$'],
loc='upper right', frameon=False, handlelength=0, handletextpad=0)
redshift.get_texts()[0].set_color('white')
#fig.add_artist(redshift)
plt.show()
Run Code Online (Sandbox Code Playgroud)
权重是未通过的值 …
在尝试开发一个类时,我遇到了这个错误.
from __future__ import division
import numpy as np
import scipy as sp
import itertools as it
from scipy.integrate import quad
import astropy.cosmology
from astropy import units as u
class NFW:
File "/Users/alexandres/Illustris/Scripts/NFWprofile2.py", line 10
^
IndentationError: expected an indented block
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u "/Users/alexandres/Illustris/Scripts/NFWprofile2.py"]
[dir: /Users/alexandres/Illustris/Scripts]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
Run Code Online (Sandbox Code Playgroud)
这是一个缩进错误怎么样?
无论我将类定义为NFW()或NFW(object),都会发生这种情况.
这是通过Sublime 3编辑的
python ×10
matplotlib ×7
numpy ×3
plot ×2
arrays ×1
class ×1
colorbar ×1
csv ×1
derivative ×1
distribution ×1
file ×1
histogram ×1
scipy ×1
subplot ×1
vector ×1