2

I am using matplotlib in Spyder for a simple plotting of sine and cosine. When i run this code

import matplotlib.pyplot as plt
import numpy as np

x1 = np.arange(0,4*np.pi,0.1)   # start,stop,step
y1 = np.sin(x1)
z1 = np.cos(x1)
plt.plot(x1,y1, z1)
plt.show()

I get this plot.

enter image description here

I didn't like the default plotting colors I am getting as you see in my plot above. However, I wanted to change them with good looking colors. I tried it with plt.style.use('classic') as the following

import matplotlib.pyplot as plt
import numpy as np
plt.style.use('classic')

x1 = np.arange(0,4*np.pi,0.1)   # start,stop,step
y1 = np.sin(x1)
z1 = np.cos(x1)
plt.plot(x1,y1, z1)
plt.show()

But this again seems to add grayed background outside of the wall as you can see it and besides it seems to shrink the space of the plot lines to touch the X axis and this seems to be a bit odd.

enter image description here

Is there any elegant way of doing this in matplotlib without the grayed background outside the plot and without shrinking the space?

DavidG
  • 24,279
  • 14
  • 89
  • 82

2 Answers2

1

There were quite a few changes when matplotlib upgraded to version 2. Using plt.style.use('classic') changes back to the defaults before this upgrade. Some examples are a change in background colour and the addition of default x and y axis margins which can be seen in the two plots in the question.

The colour cycle was also changed in matplotlib versions 2 and above. Reading the documentation shows you how to change the colour cycle back without affecting other parameters such as the background colour:

import matplotlib as mpl
from cycler import cycler
mpl.rcParams['axes.prop_cycle'] = cycler(color='bgrcmyk')

Your code would look like:

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from cycler import cycler

mpl.rcParams['axes.prop_cycle'] = cycler(color='bgrcmyk')

x1 = np.arange(0,4*np.pi,0.1)   # start,stop,step
y1 = np.sin(x1)
z1 = np.cos(x1)
plt.plot(x1,y1, z1)

plt.show()

enter image description here

DavidG
  • 24,279
  • 14
  • 89
  • 82
  • But this gives another error `AttributeError: module 'matplotlib' has no attribute 'plot'`. Did you try to run it? –  Oct 24 '18 at 21:23
  • @Brown I've updated the answer with the code I ran and the result I get – DavidG Oct 24 '18 at 21:25
  • Thanks and I have marked it as an accepted answer. You can also do me a favor by upvoting the question :-) –  Oct 24 '18 at 21:29
  • Glad to have helped :-) and well done for a good first question! – DavidG Oct 24 '18 at 21:30
0
import matplotlib.pyplot as plt
import numpy as np

x1 = np.arange(0,4*np.pi,0.1)   # start,stop,step
y1 = np.sin(x1)
z1 = np.cos(x1)
plt.plot(x1,y1,'blue')
plt.plot(x1,z1,'red')
plt.show()

Note, you don't have to do everything in a single plot - you can plot multiple times and they'll all be shown at a same time with show

Rocky Li
  • 5,641
  • 2
  • 17
  • 33