我有一个看起来像这样的图(这是著名的 Wine 数据集):
正如您所看到的,x 轴标签重叠,因此我需要旋转。
注意!我对旋转 x 刻度不感兴趣(如此处所述),但对标签文本(即alcohol、malic_acid等)感兴趣。
创建绘图的逻辑如下:我使用 创建一个网格axd = fig.subplot_mosaic(...),然后为底部绘图设置标签axd[...].set_xlabel("something")。set_xlabel如果能接受一个参数那就太好了rotation,但不幸的是事实并非如此。
我正在尝试将图像添加到 matplotlib 图的各个子图后面的“空白”。
与此主题类似的大多数讨论都是将图像添加到绘图本身,但是我还没有找到更改整个“画布”背景的方法。
我发现的最相似的功能是set_facecolor(),但是这只允许将单一颜色设置为背景。
fig, ax = plt.subplots(2,2)
fig.patch.set_facecolor('xkcd:mint green')
plt.show()
Run Code Online (Sandbox Code Playgroud)
但是,我正在寻求一种解决方案来导入绘图后面的图像,类似于此(手动制作):
我已经用谷歌搜索,搜索了SO,并浏览了matplotlib文档,但我只得到了或者plt.imshow(image)或set_facecolor()类似的结果。
我想在这里做同样的事情: matplotlib-share-x-axis-but-dont-show-x-axis-tick-labels-for-both-just-one
set(plot,'visible','off')
Run Code Online (Sandbox Code Playgroud)
没有显示任何轴.
是否可以在matplotlib中自动生成多个子图?我想要自动化的过程的一个例子是:
import matplotlib.pyplot as plt
figure = plt.figure()
ax1 = figure.add_subplot(2, 3, 1)
ax2 = figure.add_subplot(2, 3, 2)
ax3 = figure.add_subplot(2, 3, 3)
ax4 = figure.add_subplot(2, 3, 4)
ax5 = figure.add_subplot(2, 3, 5)
ax6 = figure.add_subplot(2, 3, 6)
Run Code Online (Sandbox Code Playgroud)
子图需要唯一的名称,因为这将允许我做以下的事情:
for ax in [ax1, ax2, ax3, ax4, ax5, ax6]:
ax.set_title("example")
Run Code Online (Sandbox Code Playgroud)
非常感谢.
另外:是否有任何功能可以自动生成多个子图?如果我需要重复上述过程100次怎么办?我是否必须输出每个ax1到ax100?
我从matplotlib下载了这个例子并改变了一些东西.
到目前为止,我绘制了一个条形图,其中我在y轴上应用了4个不同的阵列,在x轴上绘制了测量计数.现在我想绘制4个子图,每个子图用于其中一个数组.
我已经到了这一步:
import numpy as np
import matplotlib.pyplot as plt
n= 6
m1 = (0.10,0.12,0.10,0.11,0.14,0.10)
m2=(0.21,0.21,0.20,0.22,0.20,0.21)
m3=(0.29,0.27,0.28,0.24,0.23,0.23)
m4=(0.41,0.39,0.35,0.37,0.41,0.40)
x=[1,2,3,4,5,6]
fig, ax = plt.subplots()
index = np.arange(n)
bar_width = 0.2
opacity = 0.4
error_config = {'ecolor': '0.3'}
r1 = ax.bar(index, m1, bar_width,
alpha=opacity,
color='b',
error_kw=error_config)
r2 = ax.bar(index + bar_width, m2, bar_width,
alpha=opacity,
color='r',
error_kw=error_config)
r3 = ax.bar(index + bar_width+ bar_width, m3, bar_width,
alpha=opacity,
color='y',
error_kw=error_config)
r4 = ax.bar(index + bar_width+ bar_width+ bar_width, m4, bar_width,
alpha=opacity,
color='c',
error_kw=error_config)
plt.xlabel('D')
plt.ylabel('Anz') …Run Code Online (Sandbox Code Playgroud) 我有一个复杂的数字在matplotlib制作有三个子图,其中两个有双轴.它运作良好,除了图中没有刻度标记,我无法弄清楚如何获得它们.
这是代码和图:
fig = plt.figure()
gs = gridspec.GridSpec(3, 1)
idx=0
n=len(d_10[idx])
fac=0.2
ax1=fig.add_subplot(gs[0,0])
ax1.errorbar(range(n),d_10[idx]["abs(Overlap)"][0:n],yerr=d_10[idx]["ErrOverlap"][0:n],ms=4,mew=0.7,marker="o",ls='none',elinewidth=0.7,capsize=3,fillstyle='none',color=qual_cp4[2])
ax1.plot([0],d_10[idx]["abs(Overlap)"][0],marker="o",ls='none',fillstyle='none',color=qual_cp4[2],ms=4,mew=0.4,label='d=0')
ax1.plot(range(n),np.sqrt(hc_10),marker="+",ls='none',color=qual_cp4[0],label='Laughlin')
ax1.axes.get_xaxis().set_ticklabels([])
plt.yscale('log', nonposy="clip")
plt.axis([-2,101,1e-5,1.2])
plt.legend(loc='upper right')
yl1=plt.ylabel(r'$|\langle\psi_L^d|\phi_n\rangle|$')
plt.axis([-1,101,0.5e-5,2]);
ax2=fig.add_subplot(gs[1,0])
fac2=0.15
ex=corr*(18.5650511599/(2*10)-10/(2*np.sqrt(27.0/2)))
ax2.plot(range(n),energies[idx],color=qual_cp4[1])
yl2=plt.ylabel(r'$E_n$')
deltaE=abs(min(np.array(energies[idx]))-max(np.array(energies[idx])))
Emin=min(np.array(energies[idx]))-fac2*deltaE
Emax=max(np.array(energies[idx]))+fac2*deltaE
plt.axis([-1,n,Emin,Emax]);
plt.yticks(np.arange(Emin, Emax, abs(Emax-Emin)/4.0))
ax2.plot([0,n+1],[ex,ex],ls=':',color='r',lw=1)
ax3 = ax2.twinx()
fac3=0.4
ax3.plot([0],overlaps[idx][0],color=qual_cp4[1],label=r'$E_n$')
ax3.plot(range(n),overlaps[idx],ls="--",color=qual_cp4[3],label=r'$f$')
ax3.plot([0],overlaps[idx][0],ls=':',color='r',lw=1,label=r'$E_L$')
yl3=plt.ylabel(r'$f_n$')
deltaov=abs(min(overlaps[idx])-max(overlaps[idx]))
ovmin=min(overlaps[idx])-fac3*deltaov
ovmax=max(overlaps[idx])+fac3*deltaov
plt.axis([-1,n,ovmin,ovmax]);
ax3.plot([0,n+1],[1,1],ls='-',color='k',lw=1)
ax2.axes.get_xaxis().set_ticklabels([])
ax3.axes.get_xaxis().set_ticklabels([])
plt.legend(loc='lower right',ncol=3)
ax4=fig.add_subplot(gs[2,0])
ex=corr*(18.5650511599/(2*10)-10/(2*np.sqrt(27.0/2)))
ax4.plot(range(n),np.absolute(energies[idx]-ex),color=qual_cp4[1],label=r'$E$')
yl2=plt.ylabel(r'$|E_n-E_L|$')
xl=plt.xlabel(r'$n$')
deltaE=abs(min(np.absolute(np.array(energies[idx])-ex))-max(np.absolute(np.array(energies[idx])-ex)))
Emin=min(np.absolute(np.array(energies[idx])-ex))-fac*deltaE
Emax=max(np.absolute(np.array(energies[idx])-ex))+fac*deltaE
#plt.yticks(np.arange(Emin, Emax, abs(Emax-Emin)/4.0))
plt.yscale('log', nonposy="clip")
currov=np.abs(np.array(overlaps[idx])-1.0)
ax5 = ax4.twinx()
ax5.plot([0],currov[0],color=qual_cp4[1],label=r'$E$')
ax5.plot(range(n),currov,ls="--",color=qual_cp4[3],label=r'$f$')
yl5=plt.ylabel(r'$|f_n-1|$')
deltaov=abs(min(currov)-max(currov))
ovmin=min(currov)-fac*deltaov
ovmax=max(currov)+fac*deltaov
plt.yscale('log', nonposy="clip")
plt.legend(loc='upper …Run Code Online (Sandbox Code Playgroud) 我想在MATLAB中的图中有一个直线和条形图.如何为两个图形使用相同的x轴?下面的条形图x轴应与上面的x轴相同.我希望保留比较数字的能力.
图链接:点击这里
所以我正在尝试创建一个包含9个图形的3x3盒子,虽然我已经设法通过手动编写每个盒子的代码来完成它,但我想学习如何使用循环来完成它.我似乎无法弄明白.目前,我正在使用以下内容:
from matplotlib import gridspec
f, ((ax1, ax2, ax3), (ax4, ax5, ax6), (ax7, ax8, ax9)) = plt.subplots(3, 3, sharex='col', sharey='row')
gs = gridspec.GridSpec(3, 3)
fig = plt.figure(figsize=(20,20))
fig.text(0.5, .95, 'Constant Slope for [O/Fe]/[Fe/H] for Various R and Z', ha='center', va='center', size = 50)
fig.text(0.5, 0.08, '[Fe/H]', ha='center', va='center', size = 60)
fig.text(0.09, 0.5, '[O/Fe]', ha='center', va='center', rotation='vertical', size = 60)
ax1 = plt.subplot(gs[0])
histogram1 = ax1.hist2d(fehsc, ofesc, bins=nbins, range=[[-1,.5],[0.2,0.4]])
counts = histogram1[0]
xpos = histogram1[1]
ypos = histogram1[2]
image = …Run Code Online (Sandbox Code Playgroud) 有没有一种简单的方法可以修改此代码,以使绘图变大而不更改轴上的比例?
import numpy as np
import matplotlib.pyplot as plt
import math
%matplotlib inline
a, c = -10, 10
x = np.linspace(a,c,100)
x = np.array(x)
def y(x): return np.arctan(x)
h = 0.0000001
def grad(x,h): return (y(x+h)-y(x))/h
m = grad(x,h)
plt.figure(1)
plt.subplot(121)
plt.plot(x, y(x), 'b')
plt.xlim([a,c])
plt.ylim([min(y(x)),max(y(x))])
plt.gca().set_aspect('equal', adjustable='box')
plt.subplot(122)
plt.plot(x,m,'b')
plt.xlim([a,c])
plt.ylim([min(m),max(m)])
plt.gca().set_aspect('equal', adjustable='box')
plt.subplots_adjust(wspace = 0.5)
plt.show()
Run Code Online (Sandbox Code Playgroud)
如果我摆脱了plt.gca().set_aspect('equal', adjustable='box')这些阴谋,那就说出一个不错的规模,但它们没有规模。
我有2个子图-1个散点图和一个条,我想要一个共享的x轴。散点图有一个色条。sharex似乎与此不兼容,因为两个图的轴不一致。我的代码:
fig, (ax, ax2) = plt.subplots(2,1, gridspec_kw = {'height_ratios':[13,2]},figsize=(15,12), sharex=True)
df_plotdata.plot(kind='scatter', ax=ax, x='index_cancer', y='index_g', s=df_plotdata['freq1']*50, c=df_plotdata['freq2'], cmap=cmap)
df2.plot(ax=ax2, x='index_cancer', y='freq', kind = 'bar')
Run Code Online (Sandbox Code Playgroud) subplot ×10
matplotlib ×8
python ×7
plot ×3
matlab ×2
axes ×1
axis-labels ×1
colorbar ×1
image ×1
label ×1
pandas ×1
python-2.7 ×1
python-3.x ×1
seaborn ×1