A summary of the basic methods of Python drawing __python

Source: Internet
Author: User
Import Pandas as PD

Import NumPy as NP


Frommatplotlib.ticker Import Multiplelocator, Formatstrformatter
Import Matplotlib as Mpl
# matplotlib.use (' Agg ')
Import Matplotlib.pyplot as Plt


1. Set Canvas size

Plt.figure (figsize=) #设置画布尺寸大小, affects the size of the auto-bounce chart box
Ax = Plt.subplot (1, 1, 1) # Draw a sub chart

2. Take different color number
Col_num = df.shape[1]
ColorMap = plt.cm.gist_ncar # color axis continuous, python with the letter color only 7, sometimes not enough

colors = [ColorMap (i) for I in Np.linspace (0, 0.9, Col_num)]

3. Draw point chart, line chart, label will correspond to legend
For I in range (1, df.shape[1]):
Plt.plot (df.iloc[:, 0], df_total_guodu_3_relative30up.iloc[:, I], color=colors[i],
Linestyle= '-', linewidth=6, label= '%s '% df.columns[i])

4. Specify the path when displaying Chinese

Use Fc-list:lang=zh to query in the terminal which fonts are included

MyFont = Mpl.font_manager. Fontproperties (fname= '/USR/SHARE/FONTS/TRUETYPE/ARPHIC/UKAI.TTC ') #指定中文字体路径
Myfont1 = Mpl.font_manager. Fontproperties (fname= '/USR/SHARE/FONTS/OPENTYPE/NOTO/NOTOSANSCJK.TTC ', size=20)

Ax.legend (Legend, loc= ' best ', bbox_to_anchor= (1.03, -0.04), ncol=8, Frameon=false,prop=myfont1)

# can not set the font size alone, Fontsize,prop can not be used at the same time, if only display English numerals, do not need to specify the font, you can directly use the FontSize

5. Set the label to be placed horizontally, define the function flip

----------------------------------------------------------------------------------------------------------

Import Itertools

Def flip (items, ncol):
Return Itertools.chain (*[items[i::ncol] to I in range (Ncol))

-------------------------------------------------------------------------------------------------------

Handles, labels = ax.get_legend_handles_labels ()
Plt.legend (Flip (handles, 8), Flip (labels, 8), loc= ' best ', fontsize=17, bbox_to_anchor= (0.9, -0.02), Ncol=8,
Frameon=false)

6. Title
Plt.title (U ' transition ', Fontproperties=myfont, fontsize=25)

7. Various sets of axes
Xmajorlocator = Multiplelocator (1) # Sets the X primary tick label to a multiple of 1
Ax.xaxis.set_major_locator (Xmajorlocator)

Ymajorformatter = Formatstrformatter ('%.2f ') # set y-axis label text format two-bit decimal ('%.2f%% ') percent sign format
Ax.yaxis.set_major_formatter (Ymajorformatter)


Ticklab = Ax.yaxis.get_ticklabels () [0] #设置y轴标签的位置
trans = Ticklab.get_transform ()
Ax.yaxis.set_label_coords ( -0.06, 6000, Transform=trans)


Ax.set_ylim ([0, 5]) #设置y轴取值范围
Ax.set_xlim ([0, 11]) # Set the x-axis value range

For tick in Ax.xaxis.get_major_ticks (): # Set the size of the x-axis scale text
Tick.label1.set_fontsize (18)

For tick in Ax.yaxis.get_major_ticks (): # Set the size of the y-axis scale text
Tick.label1.set_fontsize (18)

Ax.yaxis.grid (True) # y-axis grid

8. Save Picture

Plt.savefig ('./total.jpg ', format= ' jpg ') # Save picture


9. Cumulative Column Chart

# The number of each proportional column chart, to facilitate the number of draw back
N = df[1]-1
left = Np.arange (N)
width = 0.55

Height = []

Height.append (df[0, 1:3]/1000000)
Plt.bar (left, height[0], width, facecolor = colors[0], Edgecolor = ' white ', align= ' center ')

For I in range (1, df[0]):
Height.append (Df_total_owing.iloc[i, 1:3]/1000000)
Plt.bar (left, height


10. Set the horizontal axis label is the text, date, and rotation angle

N = df.shape[0] #设置x轴刻度标签
left = Np.arange (N)
X_ticks = df.iloc[:, 0] # Set the x axis tick value and rotate, the text scale value is too long, put in the back does not work
Plt.xticks (left, X_ticks, rotation=90) # Left is where each tick starts



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.