在使用子图和单个图时,我在matplotlib中对函数的微妙差异(就是它们被称为是什么?)感到非常困惑.
举个例子:
如果我想在单个数字上更改xlim,我会这样做: plt.xlim()
如果我想在子图上更改xlim,我会这样做: ax.set_xlim()
为什么是这样?任何人都可以将我发送到解释惯例的网站吗?这是一个简单的例子,因为我知道它们是不同的,以及如何,但有许多我不知道的,我很难搞清楚 - 例如:
1)将集合应用于子图:ax.collection(collection_name).你是如何为一个人物做的?
2)将刻度标签应用于子图:ax.xaxis.ticklabels(tick_labels).你如何为一个人物做这个?
对不起,这是一个新问题!
我正在使用Matplotlib并努力在我正在绘制的包含一个子图网格的图形周围获得恰当数量的空白.
要求:能够选择所有子图的纵横比(它们都是相同的),准确定义子图与(保存的)图边缘的顶部,底部和左侧之间有多少空白.我右边不需要任何额外的空间.
我到目前为止的代码包含以下内容:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(ncols=6, nrows=2, sharex=True,
sharey=True, figsize=(16,8))
axes[0,0].plot(x,y)
etc...
plt.subplots_adjust(hspace=1.0, wspace=0.02, bottom=0.17, left=0.075, top=0.18)
plt.savefig('Fig1_matplot.pdf', bbox_inches='tight')
Run Code Online (Sandbox Code Playgroud)
实际上,如果我没有指定子图figsize=()的宽高比是错误的.如果我这样做,那么只要我更改参数,宽高比就会改变subplots_adjust().显然left cannot be >= right和bottom cannot be >= top.如果我将top定义为除0以外的任何值,它会将所有子图完全压缩到图底部的一条线.
如果我指定bbox_inches='tight'然后它会在底部和左侧切断我定义的空格,但如果我没有指定它,那么我在右边留下了多余的空格,因为我无法摆脱它(右边是<=剩下).
我有点卡住,任何帮助都会非常感激.
干杯.
我正在尝试基于Pandas groupby对象生成子图的网格.我希望每个绘图都基于groupby对象的一组的两列数据.假数据集:
C1,C2,C3,C4
1,12,125,25
2,13,25,25
3,15,98,25
4,12,77,25
5,15,889,25
6,13,56,25
7,12,256,25
8,12,158,25
9,13,158,25
10,15,1366,25
Run Code Online (Sandbox Code Playgroud)
我试过以下代码:
import pandas as pd
import csv
import matplotlib as mpl
import matplotlib.pyplot as plt
import math
#Path to CSV File
path = "..\\fake_data.csv"
#Read CSV into pandas DataFrame
df = pd.read_csv(path)
#GroupBy C2
grouped = df.groupby('C2')
#Figure out number of rows needed for 2 column grid plot
#Also accounts for odd number of plots
nrows = int(math.ceil(len(grouped)/2.))
#Setup Subplots
fig, axs = plt.subplots(nrows,2)
for ax in …Run Code Online (Sandbox Code Playgroud) 我有4个子图,我实现在1行上绘制3个子图,但是我不能在第二行上添加另一个图,它必须具有3个数字的相同宽度.前三个数字:

第二个数字,我想补充一下:
我试着这样:
plt.close()
font={'family':'sans-serif','weight':'normal','size':'14'}
plt.rc('font',**font)
fig, axes = plt.subplots(nrows=3, ncols=1)
plt.tight_layout(pad=0.05, w_pad=0.001, h_pad=2.0)
ax1 = plt.subplot(231) # creates first axis
#ax1.text(20, 2500, 'CYCLE :', size=14)
#ax1.text(1000, 2500, str((image)*50+50), size=14)
ax1.tick_params(labelsize=8)
ax1.set_xticks([0,2000,500,1000,1500])
ax1.set_yticks([0,2000,500,1000,1500])
ax1.tick_params(labelsize=8)
ax1.imshow(mouchetis[::-1,::],alpha=0.6)
ax1.imshow(V_exp_invmaskA,cmap='hot',extent=(X.min(),2015,Y.min(),2015), alpha=0.4)
i1 = ax1.imshow(V_maskA,cmap='hot',extent=(X.min(),2015,Y.min(),2015),alpha=0.8)
ax1.set_xticklabels([0,2000,500,1000,1500])
ax1.set_yticklabels([0,2000,500,1000,1500])
ax1.set_title("MASQUE A", y=1.05, fontsize=10)
ax2 = plt.subplot(232) # creates second axis
ax2.set_xticks([0,2000,500,1000,1500])
ax2.set_yticks([0,2000,500,1000,1500])
ax2.imshow(mouchetis[::-1,::],alpha=0.6)
ax2.imshow(V_exp_invmaskB,cmap='hot',extent=(X.min(),2015,Y.min(),2015), alpha=0.4)
i2 = ax2.imshow(V_maskB,cmap='hot',extent=(X.min(),2015,Y.min(),2015),alpha=0.8)
ax2.set_title("MASQUE B", y=1.05, fontsize=10)
ax2.set_xticklabels([])
ax2.set_yticklabels([])
ax3 = plt.subplot(233) # creates first axis
ax3.set_xticks([0,2000,500,1000,1500])
ax3.set_yticks([0,2000,500,1000,1500])
ax3.imshow(mouchetis[::-1,::],alpha=0.6)
ax3.imshow(V_exp_invmaskC,cmap='hot',extent=(X.min(),2015,Y.min(),2015), alpha=0.4)
i3 …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作5x4网格的子图,从查看示例看来,最好的方法是:
import matplotlib.pyplot as plt
plt.figure()
plt.subplot(221)
Run Code Online (Sandbox Code Playgroud)
子图(22)中的前两个数字表示它是2x2网格,第三个数字表示您正在制作的4个中的哪一个.但是,当我尝试这个时,我不得不去:
plt.subplot(5420)
Run Code Online (Sandbox Code Playgroud)
我收到了错误:
ValueError: Integer subplot specification must be a three digit number. Not 4
Run Code Online (Sandbox Code Playgroud)
那么这是否意味着你不能制作更多的10个子图,或者是否有办法绕过它,或者我误解它是如何工作的?
先感谢您.
我正在尝试为以下图创建子图:
我用于图的代码是:
radius_mean1 = df[df['diagnosis']==1]['radius_mean']
radius_mean0 = df[df['diagnosis']==0]['radius_mean']
trace_rm1 = go.Histogram(x = radius_mean1, opacity = 0.75, name = 'malignant')
trace_rm2 = go.Histogram(x = radius_mean0, opacity = 0.75, name = 'benign')
data2 = [trace_rm1,trace_rm2]
layout2 = go.Layout(barmode = 'overlay', title = 'radius mean')
fig2 = go.Figure(data=data2, layout=layout2)
py.iplot(fig2)
Run Code Online (Sandbox Code Playgroud)
和其他情节类似
现在我使用以下代码创建子图:
fig.append_trace(fig1['data'][1], 1, 1)
fig.append_trace(fig2['data'][0], 2, 1)
py.iplot(fig)
Run Code Online (Sandbox Code Playgroud)
我如何在子图中添加良性和恶性结果?我似乎无法编辑[0]或[1]以显示两者,即将前两个图完全显示在我的子图中,显示恶性和良性,而不仅仅是一个或另一个.
我有一个由 3 个子图组成的图形。我想将最后一个子图定位在第二行中间。目前它位于图的左下角。我该怎么做呢?我找不到堆栈溢出的答案。
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(13,10))
ax= axes.flatten()
ax[0].plot(vDT, np.cumsum(mWopt0[asset0,:])*percentage/iTT, label= 'COAL, c = 0')
ax[0].legend()
ax[0].set_title('Proportion in most invested stock')
ax[1].plot(vDT, np.cumsum(mWopt01[asset01,:])*percentage/iTT, label= 'OINL, c = 0.1')
ax[1].plot(vDT, np.cumsum(mWopt03[asset03,:])*percentage/iTT, label= 'OINL, c = 0.3')
ax[1].plot(vDT, np.cumsum(mWopt05[asset05,:])*percentage/iTT, label= 'OINL, c = 0.5')
ax[1].plot(vDT, np.cumsum(mWopt2[asset2,:])*percentage/iTT, label= 'OINL, c = 2')
ax[1].plot(vDT, np.cumsum(mWopt5[asset5,:])*percentage/iTT, label= 'OINL, c = 5')
ax[1].plot(vDT, np.cumsum(mWopt10[asset10,:])*percentage/iTT, label= 'OINL, c = 10')
ax[1].legend()
ax[1].set_title('Proportion in most invested stock')
ax[2].plot(vDT, np.cumsum(mWopt01[index,:])*percentage/iTT, label= 'c = 0')
ax[2].plot(vDT, np.cumsum(mWopt01[index,:])*percentage/iTT, …Run Code Online (Sandbox Code Playgroud) I have a series of subplots, and I want them to share x and y axis in all but 2 subplots (on a per-row basis).
I know that it is possible to create all subplots separately and then add the sharex/sharey functionality afterward.
However, this is a lot of code, given that I have to do this for most subplots.
A more efficient way would be to create all subplots with the desired sharex/sharey properties, …
我有几个子图,想要通过 ax.tick_params 调整轴刻度设置。一切工作正常,但是,没有显示小刻度。这是一个代码示例
import matplotlib.pyplot as plt
x = np.linspace(0,1,100)
y = x*x
f, (ax1,ax2) = plt.subplots(2, 1)
ax1.tick_params(axis="both", direction="in", which="both", right=False, top=True)
ax2.tick_params(axis="both", direction="in", which="both", right=True, top=False)
ax1.plot(x,y)
ax2.plot(x,-y)
plt.show()
Run Code Online (Sandbox Code Playgroud)
我认为 which=both 会给我小刻度。但是我需要添加一个额外的
plt.minorticks_on()
Run Code Online (Sandbox Code Playgroud)
这使得它们可见,但仅在 ax2 中可见。
我该如何解决?
我正在使用Octave在同一个图中绘制子图,并且想要添加一般标题,是否有像Matlabsgtitle中那样的内置函数,因为 这个老问题
有一个解决方案,但没有使用任何很酷的内置函数,而 Octave 没有。也在这里Octave - Bugs:bug #55019,我发现Octave 中有新的 sgtitle 函数,但找不到文档或包名称或任何东西......它是否尚未完全实现或有其他替代方案???sgtitle
我问的是 package 或 bultin 函数而不是实现,我将从某人那里编写或复制