SORU
9 Aralık 2012, Pazar


Matplotlib 2 Subplots, 1 Colorbar

Tamamen aynı paylaşmak için iki subplots nasıl araştırma çok uzun y ekseni tek bir colorbar Matplotlib ikisi arasındaki ortak geçirdim.

Ne oluyordu o zaman aradım colorbar() işlev ya da subplot1 subplot2, olacaktır otomatik ölçek arsa, colorbar artı arsa uyabilir içinde 'sahne' sınırlayıcı kutu, neden ikisi yan yana araziler için iki çok farklı boyutlarda.

Bunu çözmek için, o zaman hiçbir komplo işlemek için sadece bir colorbar ile mevcut hacklenmiş olan üçüncü bir sahne oluşturmaya çalıştım. Tek sorun, şimdi iki kurgunun yükseklik ve genişlikleri eşit değil, herşey daha iyi görünmesi için nasıl çözemiyorum.

İşte benim kod:

from __future__ import division
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import patches
from matplotlib.ticker import NullFormatter

# SIS Functions
TE = 1 # Einstein radius
g1 = lambda x,y: (TE/2) * (y**2-x**2)/((x**2 y**2)**(3/2)) 
g2 = lambda x,y: -1*TE*x*y / ((x**2 y**2)**(3/2))
kappa = lambda x,y: TE / (2*np.sqrt(x**2 y**2))

coords = np.linspace(-2,2,400)
X,Y = np.meshgrid(coords,coords)
g1out = g1(X,Y)
g2out = g2(X,Y)
kappaout = kappa(X,Y)
for i in range(len(coords)):
    for j in range(len(coords)):
        if np.sqrt(coords[i]**2 coords[j]**2) <= TE:
            g1out[i][j]=0
            g2out[i][j]=0

fig = plt.figure()
fig.subplots_adjust(wspace=0,hspace=0)

# subplot number 1
ax1 = fig.add_subplot(1,2,1,aspect='equal',xlim=[-2,2],ylim=[-2,2])
plt.title(r"$\gamma_{1}$",fontsize="18")
plt.xlabel(r"x ($\theta_{E}$)",fontsize="15")
plt.ylabel(r"y ($\theta_{E}$)",rotation='horizontal',fontsize="15")
plt.xticks([-2.0,-1.5,-1.0,-0.5,0,0.5,1.0,1.5])
plt.xticks([-2.0,-1.5,-1.0,-0.5,0,0.5,1.0,1.5])
plt.imshow(g1out,extent=(-2,2,-2,2))
plt.axhline(y=0,linewidth=2,color='k',linestyle="--")
plt.axvline(x=0,linewidth=2,color='k',linestyle="--")
e1 = patches.Ellipse((0,0),2,2,color='white')
ax1.add_patch(e1)

# subplot number 2
ax2 = fig.add_subplot(1,2,2,sharey=ax1,xlim=[-2,2],ylim=[-2,2])
plt.title(r"$\gamma_{2}$",fontsize="18")
plt.xlabel(r"x ($\theta_{E}$)",fontsize="15")
ax2.yaxis.set_major_formatter( NullFormatter() )
plt.axhline(y=0,linewidth=2,color='k',linestyle="--")
plt.axvline(x=0,linewidth=2,color='k',linestyle="--")
plt.imshow(g2out,extent=(-2,2,-2,2))
e2 = patches.Ellipse((0,0),2,2,color='white')
ax2.add_patch(e2)

# subplot for colorbar
ax3 = fig.add_subplot(1,1,1)
ax3.axis('off')
cbar = plt.colorbar(ax=ax2)

plt.show()

CEVAP
9 Aralık 2012, Pazar


Sadece kendi ekseni colorbar yer ve subplots_adjust ona yer açmak için kullanın.

Hızlı bir örnek olarak:

import numpy as np
import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=2, ncols=2)
for ax in axes.flat:
    im = ax.imshow(np.random.random((10,10)), vmin=0, vmax=1)

fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
fig.colorbar(im, cax=cbar_ax)

plt.show()

enter image description here

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • ICON

    ICON

    19 EKİM 2011
  • SolidWorksTutoriels

    SolidWorksTu

    14 Kasım 2013
  • Tom Megalis

    Tom Megalis

    18 NİSAN 2006