Python subplots not working properly

kanayamalakar

I am trying to plot three different graphs in three sub-plots within a single figure. Also I want the first figure to be of double width than the other two. Accordingly I have used

gs = gridspec.GridSpec(2, 2, width_ratios=[2,1], height_ratios=[1,1])

But the output has all the figures plotted on ax3.

enter image description here

My code is given here

import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import matplotlib.gridspec as gridspec

gs = gridspec.GridSpec(2, 2, width_ratios=[2,1], height_ratios=[1,1])
ax1=plt.subplot(gs[:,:-1])
ax2=plt.subplot(gs[:-1,-1])
ax3=plt.subplot(gs[-1,-1])


# ax 1
X=np.linspace(0,10,100)
Y=np.sin(X)
ax1 = plt.gca()
ax1.scatter(X, Y)
ax1.axis("tight")
ax1.set_title('ax1')
ax1.set_xlim([0,10])
ax1.set_ylim([-1,1])
plt.xticks([])
plt.yticks([])


# ax 2
ax2 = plt.gca()
vel=np.random.rand(1000)
n, bins, patches = plt.hist(vel, 10, normed=True, histtype='stepfilled', facecolor='green', alpha=1.0)
ax2.set_title('Velocity Distribution')
ax2.axis("tight")
plt.xticks([0,0.05,0.10])
plt.yticks([0,10,20])


# ax 3
Z=np.exp(X)
ax3.plot(X,Z,'red',lw=5)

plt.show()

Can somebody tell me how I can rectify this. Thank you in advance.

su79eu7k

Fixed several lines. Please compare with your code.

import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import matplotlib.gridspec as gridspec


gs = gridspec.GridSpec(2, 2, width_ratios=[2,1], height_ratios=[1,1])
ax1=plt.subplot(gs[:,:-1])
ax2=plt.subplot(gs[:-1,-1])
ax3=plt.subplot(gs[-1,-1])


# ax 1
X=np.linspace(0,10,100)
Y=np.sin(X)
#ax1 = plt.gca()
ax1.scatter(X, Y)
ax1.axis("tight")
ax1.set_title('ax1')
ax1.set_xlim([0,10])
ax1.set_ylim([-1,1])
# You can use ax1.set_xticks() and ax1.set_xticklabels() instead.
ax1.set_xticks([])
ax1.set_yticks([])
#plt.xticks([])
#plt.yticks([])


# ax 2
#ax2 = plt.gca()
vel=np.random.rand(1000)
n, bins, patches = ax2.hist(vel, 10, normed=True, histtype='stepfilled', facecolor='green', alpha=1.0)
ax2.set_title('Velocity Distribution')
ax2.axis("tight")
# You can use ax2.set_xticks() and ax2.set_xticklabels() instead.
ax2.set_xticks([0,0.5,1])
ax2.set_yticks([0,1,2])
#plt.xticks([0,0.05,0.10])
#plt.yticks([0,10,20])


# ax 3
Z=np.exp(X)
ax3.plot(X, Z,'red', lw=5)
# You can use ax3.set_xticks() and ax3.set_xticklabels() instead.
ax3.set_xticks([0, 5, 10])
ax3.set_yticks([0, 10000, 20000])
ax3.set_yticklabels(['0', '10K', '20K'])

plt.show()

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python subplots not working properly

From Dev

python .exe not working properly

From Dev

This function in python is not working properly

From Dev

Python Split not working properly

From Dev

Legend is not working properly in Python

From Dev

Python Program not working properly

From Dev

Python string count not working properly?

From Dev

Indentation not working properly in emacs for python

From Dev

Python mysqldb fetchmany not working properly

From Dev

Python: Regular Expression not working properly

From Dev

Python variable interchange not working properly?

From Dev

While loop not working properly in Python

From Dev

BST Insert not working properly in Python

From Dev

Python while loop not working properly

From Dev

Spell corrector not working properly in Python

From Dev

Download bar not working properly, Python?

From Dev

Python - For inside for loop not working properly

From Dev

Python ABC not working properly in Python 2.7.6?

From Dev

POST Request in Python 'requests' module not working properly

From Dev

Latex command \bar not working properly in Python docstring

From Dev

Is this working properly - Sum of Fibonacci in Python 3

From Dev

Python-Django timezone is not working properly

From Dev

Minimum and Maximum query not working properly (Python 3.5)

From Dev

python image scrapper, not working properly on bing

From Dev

POST Request in Python 'requests' module not working properly

From Dev

Python threading script is not starting/working properly

From Dev

Why is my python installation not working properly?

From Dev

Python3 Regex groupdict not working properly

From Dev

Changing fontsize in python subplots

Related Related

HotTag

Archive