0

I was plotting a line graph and a bar chart in matplotlib and both individually were working fine with my script.
but i'm facing a problem:
1. if i want to plot both graphs in the same output window
2. if i want to customize the display window to 1024*700

in 1 st case I was using subplot to plot two graphs in same window but i'm not being able to give both graphs their individual x-axis and y-axis names and also their individual title. my failed code is:

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
xs,ys = np.loadtxt("c:/users/name/desktop/new folder/x/counter.cnt",delimiter = ',').T
fig = plt.figure()
lineGraph = fig.add_subplot(211)
barChart = fig.add_subplot(212)

plt.title('DISTRIBUTION of NUMBER')
lineGraph = lineGraph.plot(xs,ys,'-')  #generate line graph
barChart = barChart.bar(xs,ys,width=1.0,facecolor='g') #generate bar plot

plt.grid(True)
plt.axis([0,350,0,25])  #controlls axis for charts x first and then y axis.


plt.savefig('new.png',dpi=400)
plt.show()

but with this I am not being able to mark both graphs properly.
and also please site some idea about how to resize the window to 1024*700.

Serenity
  • 35,289
  • 20
  • 120
  • 115
diffracteD
  • 758
  • 3
  • 10
  • 32
  • Possible duplicate of [How to make several plots on a single page using matplotlib?](http://stackoverflow.com/questions/1358977/how-to-make-several-plots-on-a-single-page-using-matplotlib) – Joel Cornett May 17 '12 at 07:52
  • You should really only ask one question per post. So I would edit out the question about the window size, especially as the answer can be found here: http://stackoverflow.com/a/638443/623518. – Chris May 17 '12 at 09:53

2 Answers2

1

When you say

I was using subplot to plot two graphs in same window but i'm not being able to give both graphs their individual x-axis and y-axis names and also their individual title.

do you mean you want to set axis labels? If so try using lineGraph.set_xlabel and lineGraph.set_ylabel. Alternatively, call plt.xlabel and plot.ylabel just after you create a plot and before you create any other plots. For example

# Line graph subplot
lineGraph = lineGraph.plot(xs,ys,'-')
lineGraph.set_xlabel('x')
lineGraph.set_ylabel('y')

# Bar graph subplot
barChart = barChart.bar(xs,ys,width=1.0,facecolor='g')
barChart.set_xlabel('x')
barChart.set_ylabel('y')

The same applies to the title. Calling plt.title will add a title to the currently active plot. This is the last plot that you created or the last plot you actived with plt.gca. If you want a title on a specific subplot use the subplot handle: lineGraph.set_title or barChart.set_title.

Chris
  • 44,602
  • 16
  • 137
  • 156
0

fig.add_subplot returns a matplotlib Axes object. Methods on that object include set_xlabel and set_ylabel, as described by Chris. You can see the full set of methods available on Axes objects at http://matplotlib.sourceforge.net/api/axes_api.html.

jiffyclub
  • 1,837
  • 2
  • 16
  • 9