-1

I need to plot this graph (exactly as in the picture). It is sin and cos of x, where x are equally spaced 100 numbers between 0 and 2 pi. enter image description here

So far, I wrote

x=np.linspace(0, 2*math.pi,100)
import matplotlib.pyplot as plt
%matplotlib inline 
plt.figure(figsize=(10,5)) 
plt.yticks(np.arange(-1,1.25,0.25))
plt.xticks(np.arange(0, 7, 1))
plt.plot(x,np.cos(x),'r',label="sine")
plt.plot(x,np.sin(x),'b',label="cosine")
plt.xlabel('x (in radians)')
plt.legend(loc=3)

But the plot I get looks like this enter image description here

How do I make the legend smaller, the box for the legend in grey and the plot indented?

Editing: I am using the version of Matplotlib 2.1.0 [proof][https://i.stack.imgur.com/FFXFV.png]

huda95x
  • 149
  • 1
  • 1
  • 5
  • If you run your code from the command-line (instead of through Jupyter notebook) do you get the same result? – unutbu Jan 13 '18 at 15:15
  • You could have more than one matplotlib version installed on your system. What does `print(plt.matplotlib.__version__)` show you? – unutbu Jan 13 '18 at 15:17
  • yes, the same ones – huda95x Jan 13 '18 at 15:17
  • it shows 2.1.0. – huda95x Jan 13 '18 at 15:19
  • So the real question is why matplotlib <1.5 is used, even though matplotlib 2.1 is installed?! (The picture itself is no proof of anything). In that case edit the question with the relevant information. – ImportanceOfBeingErnest Jan 13 '18 at 15:35
  • What happens if you add `plt.matplotlib.style.use('default')` at the top of your script? (I can reproduce your problem **if** I add `plt.matplotlib.style.use('classic')` to your code, using matplotlib version 2.0.2...) – unutbu Jan 13 '18 at 15:41

1 Answers1

0

The first plot shown here is produced with a matplotlib version >2.0, while the second plot is produced with an earlier version of matplotlib.

Hence the easiest option to obtain the first plot is to use the newest matplotlib version (currently 2.1).

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Thank you for the answer. But I only downloaded python on Monday with all the functions from it. So I guess I am using the newest version of matplotlib – huda95x Jan 13 '18 at 15:05
  • If the second shown plot is what you get when you run the code, you **are not** using the newest matplotlib version. – ImportanceOfBeingErnest Jan 13 '18 at 15:06
  • @huda95x: Change `plt.xticks(np.arange(0, 6, 1))` to `plt.xticks(np.arange(0, 7, 1))` to show the `6` ticker label. – unutbu Jan 13 '18 at 15:12
  • Thank you. I will edit now the question with the correction. But it still does not look the same as needed. But better though – huda95x Jan 13 '18 at 15:22