我想用Python matplotlib创建包含许多(100)子图的绘图.我找不到合适的语法:
我想要的东西(这不起作用)
plt.subplot(10,10,i,X1, Y)
Run Code Online (Sandbox Code Playgroud)
在一个循环中,i从0到99,然后
plt.show()
Run Code Online (Sandbox Code Playgroud)
许多教程中都提供了语法,以便只有很少的子图.然后,语法可以
plt.close('all')
fig = plt.figure()
ax1 = plt.subplot(221)
ax2 = plt.subplot(223)
ax3 = plt.subplot(122)
example_plot(ax1)
example_plot(ax2)
example_plot(ax3)
plt.tight_layout()
Run Code Online (Sandbox Code Playgroud)
对于我的问题,我想我不能使用与我一样的语法plt.subplot(10101),等等,我不明白.
你有解决方案吗?
谢谢
我需要通过循环迭代绘制一个图的子图; 每次迭代都会调用另一个模块(=另一个py文件)中定义的函数,该函数会绘制一对子图.这是我尝试过的 - 唉不起作用:
1)在循环之前,创建一个具有足够行数和2列的图形:
import matplotlib.pyplot as plt
fig, axarr = plt.subplots(nber_rows,2)
Run Code Online (Sandbox Code Playgroud)
2)在循环内部,在迭代编号iter_nber中,调用绘制每个子图的函数:
fig, axarr = module.graph_function(fig,axarr,iter_nber,some_parameters, some_data)
Run Code Online (Sandbox Code Playgroud)
3)有问题的功能基本上是这样的; 每次迭代在同一行上创建一对子图:
def graph_function(fig,axarr,iter_nber,some_parameters, some_data):
axarr[iter_nber,1].plot(--some plotting 1--)
axarr[iter_nber,2].plot(--some plotting 2--)
return fig,axarr
Run Code Online (Sandbox Code Playgroud)
这不起作用.我在循环结束时得到一个空数字.我已经尝试了上面的各种组合,比如只在函数的返回参数中留下axarr,但无济于事.显然我不明白这个图及其子图的逻辑.
任何建议非常感谢.
我有两个带有两列的条形图,创建如下:
fig = tools.make_subplots(rows=1, cols=2)
trace1 = go.Bar(x=x1 , y=y1)
trace2 = go.Bar(x=x2, y=y2)
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 1)
trace3 = go.Bar(x=x3 , y=y3)
trace4 = go.Bar(x=x4, y=y4)
fig.append_trace(trace3, 1, 2)
fig.append_trace(trace4, 1, 2)
Run Code Online (Sandbox Code Playgroud)
我只想要带有堆叠条的第二个子图。我尝试了以下
fig['layout']["xaxis2"].update(barmode='stack')
Run Code Online (Sandbox Code Playgroud)
但这当然行不通。如何将“堆栈”属性仅应用于第二个子图而不是整个图形?
谢谢
图形的边框/x 轴与 pyplot 条形图中的第一个和最后一个条形之间有很大的空间(第一张图片中的红色箭头)。在下图中,左图中看起来不错,但右图中浪费了很多空间。图形越大,空间就越大。
\n\n看看第二张图中浪费了多少空间:
\n\n知道如何解决这个问题吗?
\n代码:
\nplt.figure(figsize=figsize)\ngrid = plt.GridSpec(figsize[1], figsize[0], wspace=120/figsize[0])\nplt.suptitle(feature.upper())\n\n# NaN plot\nplt.subplot(grid[:, :2])\nplt.title(\'Pr\xc3\xa9sence\')\nplt.ylabel(\'occurences\')\nplt.bar([0, 1], [df.shape[0] - nan, nan], color=[\'#4caf50\', \'#f44336\'])\nplt.xticks([0, 1], [\'Renseign\xc3\xa9\', \'Absent\'], rotation=\'vertical\')\n\n# Distrib plot\nplt.subplot(grid[:, 2:])\nplt.title(\'Distribution\')\nx_pos = [i for i, _ in enumerate(sizes)]\nplt.bar(x_pos, sizes)\nplt.xticks(x_pos, labels, rotation=\'vertical\')\nplt.show()\nRun Code Online (Sandbox Code Playgroud)\ndf是我的 pandas DataFrame,nan我正在绘制的特征的空值数量,sizes是每个值出现的次数,labels是相应标签的值。
我正在使用matplotlib在一个图上绘制多个箱图,遵循以下示例:http:
//matplotlib.org/examples/pylab_examples/subplots_demo.html
一切都按预期工作,但我无法弄清楚如何更改x轴上的刻度标签.我有四个数据系列,它们是绘图,但是x轴刻度标记为'1,2,3,4',我想在这些值中指定文本.
我的绘图代码如下所示:
f, axarr = plt.subplots(2, sharex=True)
axarr[0].boxplot(WS_Bias, whis = 100)
#axarr[0].xticks(range(1, len(Labels)+1),Labels)
axarr[0].axhspan(-0.5, 0.5, facecolor='c', alpha = 0.2)
axarr[1].boxplot(WS_RMS, whis = 100)
#axarr[1].xticks(range(1, len(Labels)+1),Labels)
axarr[1].axhspan(0, 2, facecolor='c', alpha = 0.2)
pl.show()
Run Code Online (Sandbox Code Playgroud)
当我只处理一个数据集时,注释掉的行可以更改标签,但是这个命令无法用我正在使用的方法识别.Pyplot以plt形式导入.
我想创建一个由九个子图组成的图形.我真的很讨厌我需要分别创建ax1到ax9的事实所以我创建了一个for循环来这样做.但是,当我想要包含一个颜色条时,颜色条位于最后一个子图的右侧.这也在下图中说明:

出了什么问题,我该如何解决这个问题?
已使用以下代码生成图像:
import numpy
import layout
import matplotlib.pylab as plt
data = numpy.random.random((10, 10))
test = ["ax1", "ax2", "ax3", "ax4", "ax5", "ax6", "ax7", "ax8", "ax9"]
fig = plt.figure(1)
for idx in range(len(test)):
vars()[test[idx]] = fig.add_subplot(3, 3, (idx + 1))
im = ax1.imshow(data)
plt.colorbar(im)
im2 = ax3.imshow(data)
plt.colorbar(im2)
plt.show()
Run Code Online (Sandbox Code Playgroud) 我试图用两个不同的contourf子图来制作一个使用完全不同的colormap的图.但是,对于我目前拥有的代码(为其中一个子图创建自定义颜色图),子图使用相同的颜色图.任何想法如何解决这一问题?
h = figure;
subplot(2,1,1)
title('GEFS 20 Member Mean Vorticity');
axesm('eqdcylin','maplonlimit',[-180 179],'maplatlimit',[0 90]);
%eqdcylin
contourm(gLat, gLon, squeeze(meanhx(x,:,:))', 16,'Color',[0.05 0.05 0.05],'LineWidth',2);
hold on
contourfm(gLat, gLon, squeeze(vmeanx(x,:,:))', 30, 'LineStyle', 'none');
shading flat;
lm=worldlo('POline');
for i=1:length(lm);
lm(i).otherproperty = {'color','m','LineWidth',1.5};
end
displaym(lm);
gridm on;
tightmap;
set(h, 'Position', [1 1 2200 1100]);
colormap(b2r(-5*10^-5, 5*10^-5));
freezeColors;
cbfreeze(colorbar)
%caxis([-5*10^-5 5*10^-5])
colorbar;
subplot(2,1,2)
title('GEFS 20 Member Vorticity Variance');
axesm('eqdcylin','maplonlimit',[-180 179],'maplatlimit',[0 90]);
%eqdcylin
contourm(gLat, gLon, squeeze(meanhx(x,:,:))', 16,'Color',[0.05 0.05 0.05],'LineWidth',2);
hold on
contourfm(gLat, gLon, squeeze(vvarx(x,:,:))', 30, 'LineStyle', 'none');
shading flat; …Run Code Online (Sandbox Code Playgroud) 我尝试为我的身材生成3个子图.它看起来很有效,但并不是完全一致的.如何在底部的数字和下面的数字之间有相同的宽度?
plt.close()
fig = plt.figure()
ax1 = plt.subplot(211)
ax1.plot(cycles,np.asarray(df_int),'wo',label='')
ax1.plot(cycles,np.asarray(df_proj),'k+',label='')
ax1.legend(loc=2)
#ax1.subplots_adjust(left=0.15)
ax1.set_ylim(0.001,0.004)
ax2 = plt.subplot(223)
i2=ax2.imshow(c,cmap=plt.cm.hot, vmin=-5, vmax=5);#plt.colorbar();plt.clim(-5,5)
cb2=plt.colorbar(i2,ax=ax2,ticks=[-5,0,5],fraction=0.046, pad=0.04,format='%.2f')
cb2.ax.tick_params(labelsize=12)
ax3 = plt.subplot(224)
i3=ax3.imshow(residue_proj,cmap=plt.cm.hot, vmin=-5, vmax=5);#plt.colorbar();plt.clim(-5,5);
cb3=plt.colorbar(i3,ax=ax3,ticks=[-5,0,5],fraction=0.046, pad=0.04,format='%.2f')
cb3.ax.tick_params(labelsize=12)
plt.savefig('test.png', dpi=500, bbox_inches='tight', pad_inches=0.1)
#plt.show()
Run Code Online (Sandbox Code Playgroud)
我想更改 xticks 的旋转,但我以 x 和 yticks 旋转结束。我怎样才能只旋转xticks?
这是我的代码:
# Plot mit Sidestepped 0/1
sns.set(style="darkgrid")
fig, ax = plt.subplots(1,2, figsize=(14,5))
for i in range(len(ax)):
ax[i].tick_params(labelsize=15)
ax[i].set_xlabel('label', fontsize=17, position=(.5,20))
ax[i].set_ylabel('label', fontsize=17)
sns.countplot(x="page_name", hue="successful", data=mainDf, ax=ax[0]);
sns.countplot(x="industry", hue="successful", data=mainDf, ax=ax[1]);
fig.suptitle('Categorical Features Count', position=(.5,1.1), fontsize=20)
ax[0].set_title('Type by Industry', fontsize=18)
ax[0].set_xlabel('Industry')
ax[0].tick_params(rotation=50)
ax[1].set_title('Success by Industry', fontsize=18)
ax[1].set_xlabel('Industry')
fig.tight_layout()
fig.show()
Run Code Online (Sandbox Code Playgroud)
这是我得到的(x 但不幸的是 yticks 也旋转了!只看 ax[0] 处的左图!我想只旋转左图的 xticks!):
我试图消除两个特定轴之间的垂直空间;然而plt.figure(layout='constrained')阻止matplotlib.pyplot.subplots_adjust(hspace=0)。
layout='constrained'import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10, 8),layout='constrained')
# fig = plt.figure(figsize=(10, 8))
subfigs = fig.subfigures(2, 1)
axsUp = subfigs[0].subplots(1, 3)
subfigsnest = subfigs[1].subfigures(1, 2, width_ratios=[1, 2])
ax = subfigsnest[0].subplots(1)
axsnest = subfigsnest[1].subplots(2, 1, sharex=True)
subfigsnest[1].subplots_adjust(hspace=0)
plt.show()
Run Code Online (Sandbox Code Playgroud)

layout='constrained'import matplotlib.pyplot as plt
# fig = plt.figure(figsize=(10, 8),layout='constrained')
fig = plt.figure(figsize=(10, 8))
subfigs = fig.subfigures(2, 1)
axsUp = subfigs[0].subplots(1, 3)
subfigsnest = subfigs[1].subfigures(1, 2, width_ratios=[1, 2])
ax = subfigsnest[0].subplots(1)
axsnest = subfigsnest[1].subplots(2, …Run Code Online (Sandbox Code Playgroud)