The method of popular Matplotlib.pyplot

This paper finishing method for a number of popular graphics library in Python pyplot, after all, will have a chance to use another popular, but also easy to find, in which case it is annoying (like me)! So then I will describe some of its methods, as far as possible so that you can read with white! From time to time update! ! !

1, pyplot.xticks () and yticks ()

These two methods can be used to set the scale of the x and y axes, as required in some instances the x or y-axis values ​​are not displayed, but the character, it is necessary to replace values ​​into character form, see the following example of specific usage:

import matplotlib.pyplot as plt
import matplotlib as mpl
# 使图表中可以显示中文
mpl.rcParams["font.family"] = "sans-serif"
mpl.rcParams["font.sans-serif"] = u'SimHei'

if __name__ == "__main__":
    # 指定x,y轴的刻度
    plt.xticks(range(5, 30, 5), (u'周一', u'周二', u'周三', u'周四', u'周五'))
    plt.yticks(range(1, 6, 1), ('a', 'b', 'c', 'd', 'e'))
    # 画图
    plt.plot([5, 10, 15, 20, 25], [1, 2, 3, 4, 5])
    plt.show()

Operating results shown below, the x-axis as an example, Range (5, 30, 5) is provided an x-axis scale, from 5 starts every 5 units of a scale range of less than 30, that is to say in the FIG. the actual scale of the x-axis is [5, 10, 15, 20, 25], the characters corresponding to the back is a scale, the scale and number to the number of the main consistent! In other words, the axis icon or the actual value, replace characters but displayed only grumble!
Here Insert Picture Description

2、ax.set_xticks()与set_yticks()

Pigment, you did wrong, there is a method that is mainly used to set the time axis scale is used to set the canvas fig = plt.figure () and a sub-picture setting ax = fig.add_subplot (), there is not a pyplot draw diagrams, charts but with Matplotlib in Figure () was created. Really speechless, draw a chart to engage in so many ways, and the method used in many different, you can not take a unified look at it? Specific use of the method code is as follows:

fig = plt.Figure()
ax = fig.add_subplot(111)

ax.set_xticks(range(5, 30, 5))
ax.set_xticklabels((u'周一', u'周二', u'周三', u'周四', u'周五'))
ax.set_yticks(range(1, 6, 1))
ax.set_yticklabels(('a', 'b', 'c', 'd', 'e'))

1 and the same effect, but there is provided a character display scale is a separate need to use another method set_xticklabels (), as described above in accordance with an input format on the line chatter!

3, Pltkbrh ()

This method bar () is similar to bar charts are drawn, only barh () painting is a horizontal bar chart, that this can be used to draw Gantt charts, specialized in painting than some Python Gantt chart to more than a simple, easy to understand! Ado, directly on the code:

if __name__ == "__main__":
    # 指定x,y轴的刻度
    plt.xticks(range(0, 25, 5), (u'周一', u'周二', u'周三', u'周四', u'周五'))
    plt.yticks(range(1, 6, 1), ('a', 'b', 'c', 'd', 'e'))
    # 画甘特图
    plt.barh((1, 2, 3, 4, 5), (5, 5, 5, 5, 5), 0.5, (0, 5, 10, 15, 20))
    plt.show()

The code and the same, but changed the line drawing only. As shown below, (1, 2, 3, 4, 5) is the ordinate scale corresponding to, i.e., there are five horizontal bar chart, the ordinate respectively is 1,2,3,4,5. * (5, 5, 5, 5, 5) * is the width of each bar, you can set yourself, I set every 5 just direct the length scale of the abscissa two! 0.5 is a bar graph height customizable! * (0, 5, 10, 15, 20) * The abscissa is the starting position of each bar, namely, the left end position.
Here Insert Picture Description

Published 17 original articles · won praise 3 · Views 1801

Guess you like

Origin blog.csdn.net/weixin_43350361/article/details/104996631