Matplotlib chart axis display problem

Those who are learning the column of wizardforcel:

Introductory Python Data Science Tutorial: Matplotlib (  click to open the link )

Among them, the stacking graph in Chapter 5 uses the following source code to draw the graph, and the problem that the graph area deviates from the Y-axis appears as follows, which is very ugly.

import matplotlib.pyplot as plt

days = [1,2,3,4,5]

sleeping = [7,8,6,11,7]
eating =   [2,3,4,3,2]
working =  [7,8,7,2,2]
playing =  [8,5,7,8,13]


plt.plot([],[],color='m', label='Sleeping', linewidth=5)
plt.plot([],[],color='c', label='Eating', linewidth=5)
plt.plot([],[],color='r', label='Working', linewidth=5)
plt.plot([],[],color='k', label='Playing', linewidth=5)

plt.stackplot(days, sleeping,eating,working,playing, colors=['m','c','r','k'])

plt.xlabel('x')
plt.ylabel('y')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.show()




Baidu found a solution: set the x, y coordinate range,

import matplotlib.pyplot as plt
# The following two lines of code are specially used to solve the problem of Chinese display
from pylab import *  
mpl.rcParams ['font.sans-serif'] = ['Microsoft YaHei']  

days = [1,2,3,4,5]

sleeping = [7,8,6,11,7]
eating =   [2,3,4,3,2]
working =  [7,8,7,2,2]
playing =  [8,5,7,8,13]

# draw label legend
plt.plot([],[],color='m', label='Sleeping-睡觉', linewidth=5)
plt.plot([],[],color='c', label='Eating-吃饭', linewidth=5)
plt.plot([],[],color='r', label='Working', linewidth=5)
plt.plot([],[],color='k', label='Playing', linewidth=5)

plt.stackplot(days, sleeping,eating,working,playing, colors=['m','c','r','k'])

# Set the value range of the x and y axes to:
plt.ylim(0, 24)
plt.xlim(1, 5)

plt.xlabel('x-date')
plt.ylabel('y-item')
plt.title('Interesting Graph\nCheck it out')
# draw label legend
plt.legend()
plt.show()



Perfect solution!



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325658791&siteId=291194637