标签: subplot

如何在matplotlib中更改axis对象的刻度字体大小

我有一个数字我添加了子图(插入).我用过:

fig = plt.figure()
ax = fig.add_subplot(111)
subA = fig.add_axes([0.4,0.14,0.2,0.2])
Run Code Online (Sandbox Code Playgroud)

我现在想要改变子图的xtick字体大小.我试过一些天真的方法,比如

subA.get_xaxis().get_xticks().set_fontsize(10)
Run Code Online (Sandbox Code Playgroud)

没有运气.

那怎么办呢?

python axis matplotlib subplot

24
推荐指数
2
解决办法
9万
查看次数

在matplotlib中以虚线更改破折号的间距

在Python中,使用matplotlib,有一种方法可以改变不同线条样式的破折号的距离,例如,使用以下命令:

plt.plot(x,y,linestyle='--')
Run Code Online (Sandbox Code Playgroud)

python plot matplotlib line subplot

24
推荐指数
1
解决办法
2万
查看次数

Matplotlib - 将子图添加到子图中?

我正在尝试创建一个由2x2网格组成的图形,其中每个象限中有2个垂直堆叠的子图(即2x1网格).但我似乎无法弄清楚如何实现这一目标.

我得到的最接近的是使用gridspec和一些丑陋的代码(见下文),但是因为gridspec.update(hspace=X)改变了所有子图的间距,我仍然不是我想要的.

理想情况下,我想要的是,使用下面的图片作为示例,减少每个象限内的子图之间的间距,同时增加顶部和底部象限之间的垂直间距(即1-3和2-4之间).

有没有办法做到这一点(使用或不使用gridspec)?我最初想象的是生成每个子子图网格(即每个2x1网格)并将它们插入更大的2x2子图网格中,但我还没想出如何将子图添加到子图中,如果有偶数的话一种方式.

在此输入图像描述

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
plt.figure(figsize=(10, 8))
gs = gridspec.GridSpec(4,2)
gs.update(hspace=0.4)
for i in range(2):
    for j in range(4):
        ax = plt.subplot(gs[j,i])
        ax.spines['top'].set_visible(False)
        ax.spines['right'].set_visible(False)
        plt.tick_params(which='both', top='off', right='off')
        if j % 2 == 0:
            ax.set_title(str(i+j+1))
            ax.plot([1,2,3], [1,2,3])
            ax.spines['bottom'].set_visible(False)
            ax.get_xaxis().set_visible(False)   
        else:
            ax.plot([1,2,3], [3,2,1])
Run Code Online (Sandbox Code Playgroud)

python matplotlib figure subplot

23
推荐指数
3
解决办法
2万
查看次数

Python:循环中的子图:第一个面板出现在错误的位置

我是Python的新手,来自更Matlab的观点.我试图制作一系列2 x 5面板contourf子图.到目前为止,我的方法是将我的Matlab代码转换(在某种程度上)到Python并在循环中绘制我的子图.代码的相关部分如下所示:

fig=plt.figure(figsize=(15, 6),facecolor='w', edgecolor='k')
for i in range(10):

    #this part is just arranging the data for contourf 
    ind2 = py.find(zz==i+1)
    sfr_mass_mat = np.reshape(sfr_mass[ind2],(pixmax_x,pixmax_y))
    sfr_mass_sub = sfr_mass[ind2]
    zi = griddata(massloclist, sfrloclist, sfr_mass_sub,xi,yi,interp='nn')


    temp = 250+i  # this is to index the position of the subplot
    ax=plt.subplot(temp)
    ax.contourf(xi,yi,zi,5,cmap=plt.cm.Oranges)
    plt.subplots_adjust(hspace = .5,wspace=.001)

    #just annotating where each contour plot is being placed
    ax.set_title(str(temp))
Run Code Online (Sandbox Code Playgroud)

作为这个论坛的新手,我似乎不允许附加结果图像.但是,通过我在代码中的索引作为'temp',2 x 5面板的最终布局是:

251 - 252 - 253 - 254 - 255
256 - 257 - 258 - 259 …
Run Code Online (Sandbox Code Playgroud)

python matplotlib subplot

21
推荐指数
3
解决办法
6万
查看次数

使用plt.subplots时的图形大小

我在使用时尝试更改图形尺寸时遇到了一些麻烦plt.subplots.使用下面的代码,我只得到标准尺寸图,其中所有的子图集合在一起(大约有100个),显然只是一个额外的空图形.我尝试过使用tight_layout,但无济于事.

def plot(reader):
    channels=[]
    for i in reader:
        channels.append(i)

    plt.figure(figsize=(50,100))
    fig, ax = plt.subplots(len(channels), sharex=True)

    plot=0    
    for j in reader: 

        ax[plot].plot(reader["%s" % j])
        plot=plot+1

    plt.tight_layout()
    plt.show()
Run Code Online (Sandbox Code Playgroud)

任何帮助都会很棒!

在此输入图像描述

python layout matplotlib subplot

20
推荐指数
1
解决办法
5万
查看次数

使用pyplot在python中绘制多个子图上的水平线

我正在同一页面上绘制三个子图.我想在所有子图中绘制一条horiZontal线.以下是我的代码和结果图:(您可以注意到我可以在其中一个图上获得水平线,但不是全部)

gs1 = gridspec.GridSpec(8, 2)
gs1.update(left=0.12, right=.94, wspace=0.12)
ax1 = plt.subplot(gs1[0:2, :])
ax2 = plt.subplot(gs1[3:5, :], sharey=ax1)
ax3 = plt.subplot(gs1[6:8, :], sharey=ax1)

ax1.scatter(theta_cord, density, c = 'r', marker= '1')
ax2.scatter(phi_cord, density, c = 'r', marker= '1')
ax3.scatter(r_cord, density, c = 'r', marker= '1')
ax1.set_xlabel('Theta (radians)')
ax1.set_ylabel('Galaxy count')
ax2.set_xlabel('Phi (radians)')
ax2.set_ylabel('Galaxy count')
ax3.set_xlabel('Distance (Mpc)')
ax3.set_ylabel('Galaxy count')
plt.ylim((0,0.004))
loc = plticker.MultipleLocator(base=0.001)
ax1.yaxis.set_major_locator(loc)

plt.axhline(y=0.002, xmin=0, xmax=1, hold=None)

plt.show()
Run Code Online (Sandbox Code Playgroud)

这会生成以下内容: 在此输入图像描述

同样,我希望我在最后一个子图上绘制的线也出现在前两个子图上.我怎么做?

python matplotlib scatter-plot subplot line-plot

20
推荐指数
2
解决办法
5万
查看次数

在matplotlib中制作更长的子图刻度标记?

我试图改变沿python多面板子图的轴的刻度线.我有两个共用一个x轴的面板.我已经使绘图周围的边框更粗,并使沿轴的所有刻度线更粗.我有两个问题:

如何使所有刻度线(两个轴)更长,以便更加明显?

如何在主要刻度线之间添加较小但仍然明显的刻度线?

这是我到目前为止的最低工作示例.

from numpy import *
from scipy import *
from pylab import *
from random import *
import pylab
import matplotlib.pyplot as plt


#make the axis border thick
pylab.rc("axes", linewidth=4.0)
pylab.rc("lines", markeredgewidth=4)


#create a figure with two panels that shares the x-axis
f, (ax1, ax2) = plt.subplots(2, sharex=True, sharey=False)
#example figure1
ax1.plot(range(2),range(2),linewidth=2)
#example figure 2
ax2.plot(range(2),range(2),linewidth=2)



# Fine-tune figure; make subplots close to each other and hide x ticks for
# all but bottom plot.
f.subplots_adjust(hspace=0)
plt.setp([a.get_xticklabels() …
Run Code Online (Sandbox Code Playgroud)

python matplotlib subplot

19
推荐指数
1
解决办法
1万
查看次数

来自plt.subplots()的轴是一个"numpy.ndarray"对象,没有属性"plot"

如果您尝试了解错误消息,则以下信息可能是多余的.请阅读开始的答案@ user707650.

使用MatPlotLib,我想要一个可推广的脚本,它可以从我的数据中创建以下内容.

含有窗口一个布置副区,以便有b每列副区.我希望能够改变ab的值.

如果我有用于数据2a中副区,我想2窗口,每个窗口与先前描述的" 一个根据布置副区b每列次要情节".

我正在绘制的x和y数据是存储在np.arrays中的浮点数,结构如下:

  • 对于所有图,x数据始终相同,长度为5.

     'x_vector': [0.000, 0.005, 0.010, 0.020, 0.030, 0.040]
    
    Run Code Online (Sandbox Code Playgroud)
  • 所有图的y数据都存储在y_vector中,其中第一个图的数据存储在索引0到5中.第二个图的数据存储在索引6到11中.第三个图得到12-18,第四个图得到-24,依此类推.

总的来说,对于这个数据集,我有91个图(即91*6 = 546个值存储在y_vector中).

尝试:

import matplotlib.pyplot as plt

# Options:
plots_tot = 14 # Total number of plots. In reality there is going to be 7*13 = 91 plots.
location_of_ydata = 6 # The values for the n:th plot can be found in the y_vector …
Run Code Online (Sandbox Code Playgroud)

numpy matplotlib multidimensional-array python-2.7 subplot

19
推荐指数
2
解决办法
4万
查看次数

循环生成子图时出错

我有一个关于循环从数据框中绘制多个子图时收到的错误的问题.

我的数据框有很多列,我在其中循环以获得每列的子图.

这是我的代码

 def plot(df):
    channels=[]
    for i in df:
        channels.append(i)

    fig, ax = plt.subplots(len(channels), sharex=True, figsize=(50,100))

    plot=0    
    for j in df: 

        ax[plot].plot(df["%s" % j])
        ax[plot].set_xlabel('%s' % j)
        plot=plot+1

    plt.tight_layout()
    plt.show() 
Run Code Online (Sandbox Code Playgroud)

我得到的情节很好,但也是一个空框架和错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\AClayton\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 538, in runfile
    execfile(filename, namespace)
  File "C:/Users/AClayton/Desktop/Data/TS.py", line 67, in <module>
    plot(all_data)
  File "C:/Users/AClayton/Desktop/Data/TS.py", line 49, in plot
    ax[plot].plot(reader["%s" % j])
TypeError: 'AxesSubplot' object does not support indexing
Run Code Online (Sandbox Code Playgroud)

如果第一个图表生成正常,或者为什么产生第二个图形,我看不出这个错误来自哪里?

感谢您的任何见解

python matplotlib dataframe subplot

18
推荐指数
1
解决办法
2万
查看次数

Matplotlib:获取并设置轴位置

在matlab中,可以直接获取和设置图上现有轴的位置:

  pos = get(gca(), 'position')
  set(gca(), 'position', pos)
Run Code Online (Sandbox Code Playgroud)

我怎么在Matplotlib中这样做?

我出于两个相关原因需要这个:

这些是我试图解决的具体问题:

  • 我有一列子图,其中一些有颜色条,有些没有,并且它们的宽度不同,即X轴不对齐.colorbar从轴上窃取空间.这也发生在matlab中,我会使用上面的技巧通过将带有颜色条的轴的宽度复制到没有颜色条的轴来使所有轴同样宽.

  • 通过收缩轴在各个子图之间添加空格.adjust_subplots()函数调整所有子图相同.

python axes matplotlib subplot

18
推荐指数
1
解决办法
4万
查看次数