Python Data Analysis Library pandas------initial knowledge of Matpoltlib:matplotliab drawing how to display Chinese, set coordinate labels; theme; Picture sub-chart; Pandas time data format conversion; legend;

Source: Internet
Author: User



Open the canvas, the value of x, Y, can simply draw a graph



 1 import matplotlib.pyplot as plt
 2 
 3 c = [
 4     0.9012051747628913, 0.9012051747628913, 0.9012051747628913, 0.9012051747628913,
 5     0.9012051747628913, 0.9012051747628913, 0.9012051747628913, 0.9012051747628913,
 6     0.9012051747628913, 0.9012051747628913, 0.9012051747628913, 0.8246557944522697,
 7     0.7544842280408721, 0.6902265689813546, 0.6313864504646931, 0.5775093282079332,
 8     0.5211950040503636, 0.46780543149450055, 0.41969125813285063, 0.37634483770567245,
 9     0.33730644971557494, 0.30215984264922796, 0.2705281787178777, 0.24207035338627686,
10     0.2164776560859111, 0.19347074047424476, 0.17279687546043812, 0.15422745095614654,
11     0.13755571478805428, 0.12259471943174775, 0.10917545922273249, 0.09238916892440648,
12     0.07810309775699459, 0.132592491963472, 0.11187615362102099, 0.09429909803049094,
13     0.07939692421648169, 0.06677264402087058, 0.0560870414573683, 0.04705039444088354,
14     0.039415368208175136, 0.03203425502245122, 0.02583224107946905, 0.020769682030363956,
15     0.016646574068682765, 0.013296693161182788, 0.010582068906654783, 0.008388339009249155,
16     0.006620847041428546, 0.005201367205062169, 0.004065357659143943, 0.003159659141824776,
17     0.002440568476024901, 0.0018722274554178776, 0.0014252768539108648, 0.0010757331361786078,
18     0.0008040520819562571, 0.0005943491533515433, 0.0004337511864414913, 0.00031185800679816146
19      ]
20 
21 plt.plot(range(0, 60), c)
22 plt.show()


  Allowed results:






  The picture is simple and monotonous. Generally for the sake of aesthetics we need more parameter designations.



Some operations on the x-axis


 
 
Get the current locations and labels:

    >>> locs, labels = xticks()

Set label locations:

    >>> xticks(np.arange(0, 1, step=0.2))

Set text labels:

    >>> xticks(np.arange(5), (‘Tom‘, ‘Dick‘, ‘Harry‘, ‘Sally‘, ‘Sue‘))

Set text labels and properties:

    >>> xticks(np.arange(12), calendar.month_name[1:13], rotation=20) 


The following cases:



1 plt.plot (range (0, 60), c)
2 # xticks (* args, ** kwargs) -----> xticks (locs, [labels], ** kwargs) # Set locations and labels
3 # np.arange (0, 60, step = 5) is the position of the specified scale;
4 # The second parameter ("0", "100,000", "200,000", "300,000", "400,000", "500,000", "600,000") specifies the identifier of the corresponding scale
5 # Third, we can use rotation = 20 to specify the angle of the logo.
6 plt.xticks (np.arange (0, 61, step = 10), ("0W", "10W", "20W", "30W", "40W", "50W", "60W"), rotation = 20)
7 plt.show ()
8 loc_, labels = plt.xticks () # Get the position and label of the scale
9 print (loc_, "\ n", labels)


  Operation Result:









Add horizontal and vertical labels, as well as headings



1 # matplotlib does not support Chinese by default, here we specify the font with fontproperties = font
2 from matplotlib.font_manager import FontProperties
3 font = FontProperties (fname = r "c: \ windows \ fonts \ STKAITI.TTF", size = 14)
4 plt.plot (range (0, 60), c)
5 plt.xticks (np.arange (0, 61, step = 10), ("0W", "10W", "20W", "30W", "40W", "50W", "60W"), rotation = 20)
6 plt.xlabel ("Benefit (W: million)", fontproperties = font)
7 plt.ylabel ("Probability", fontproperties = font)
8 plt.title ("Probability Curve", fontproperties = font)
9 plt.show ()


  Operation Result:






Spend multiple sub-graphs on a canvas



Operation Result:






  At this point, you may wonder if we want to draw an example such as a coordinate marker onto the first word canvas, whereas the matplotlib is drawn by default on the last Word canvas



Above, how to do? For more information please go to other blogs, where more detailed instructions are available .





Pandas import time data for format conversion



  






Draw multiple graphs on one canvas and add legends


1 from matplotlib.font_manager import FontProperties
  2 font = FontProperties (fname = r "c: \ windows \ fonts \ STKAITI.TTF", size = 14)
  3 colors = ["red", "green"] # specify the color of the line
  4 labels = ["JingDong", "12306"] # specify the legend
  5 plt.plot (range (0, 60), c, c = colors [0], label = labels [0])
  6 plt.plot (range (0, 60), np.arange (0, 0.89, step = 0.015), c = colors [1], label = labels [1])
  7 plt.legend (loc = "best") # Specify where the legend is displayed. Without plt.legend (), the legend will not be displayed
  8 plt.xticks (np.arange (0, 61, step = 10), ("0 million", "100,000", "200,000", "300,000", "400,000", "500,000", " 600000"),
  9 rotation = 20, fontproperties = font)
10 plt.xlabel ("Benefit (W: million)", fontproperties = font)
11 plt.ylabel ("Probability", fontproperties = font)
12 plt.title ("Probability Curve", fontproperties = font)
13 plt.show ()

  Operation Result:









Python Data Analysis Library pandas------initial knowledge of Matpoltlib:matplotliab drawing how to display Chinese, set coordinate labels; theme; Picture sub-chart; Pandas time data format conversion; legend;


Related Article

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.