Solve the Problem of legend display when python uses matplotlib for plotting, pythonmatplotlib

Source: Internet
Author: User

Solve the Problem of legend display when python uses matplotlib for plotting, pythonmatplotlib

Preface

Matplotlib is an open-source project based on the Python language. It aims to provide a data drawing package for Python. When using the Python matplotlib library to draw a Data graph, you need to use a legend to mark the data category. However, when passing parameters, the legend interpretation text will only display the first character, you can solve this problem by adding a comma (which should be a python syntax and a comma) after the parameter,

Example:

Import numpy as np import matplotlib. pyplot as plt from matplotlib. ticker import MultipleLocator from pylab import mpl xmajorLocator = MultipleLocator (24*3) # Set the X axis primary scale label to a multiple of 24*3 ymajorLocator = MultipleLocator (100*2) # Set the main scale label of the Y axis to a multiple of 100*2 # Set the Chinese font mpl. rcParams ['font. sans-serif'] = ['simhei'] # import file data = np. loadtxt ('H:/dataset/ __ba.csv ', delimiter =', ', dtype = int) # capture array data x = data [:, 0] y = data [:, 1] plt. figure (num = 1, figsize = (8, 6) ax = plt. subplot (111) ax. xaxis. set_major_locator (xmajorLocator) ax. yaxis. set_major_locator (ymajorLocator) ax. xaxis. grid (True, which = 'major') # Use the primary scale ax for the x axis grid. yaxis. grid (True, which = 'major') # Use the primary scale plt for the x axis grid. xlabel ('time Index') plt. ylabel ('activity frequency ') plt. title ('line my') plt. xlim (0, 1152) plt. ylim (0, 2200) # plt. plot (x, y, 'rs-') line1 = ax. plot (x, y, 'B. -') ax. legend (line1, ('weibo ') plt. show ()

The display effect is as follows:

Code Modification

From pylab import mpl xmajorLocator = MultipleLocator (24*3) # Set the X axis primary scale label to a multiple of 24*3 ymajorLocator = MultipleLocator (100*2) # Set the main scale label of the Y axis to a multiple of 100*2 # Set the Chinese font mpl. rcParams ['font. sans-serif'] = ['simhei'] # import file data = np. loadtxt ('H:/dataset/ __ba.csv ', delimiter =', ', dtype = int) # capture array data x = data [:, 0] y = data [:, 1] plt. figure (num = 1, figsize = (8, 6) ax = plt. subplot (111) ax. xaxis. set_major_locator (xmajorLocator) ax. yaxis. set_major_locator (ymajorLocator) ax. xaxis. grid (True, which = 'major') # Use the primary scale ax for the x axis grid. yaxis. grid (True, which = 'major') # Use the primary scale plt for the x axis grid. xlabel ('time Index') plt. ylabel ('activity frequency ') plt. title ('line my') plt. xlim (0, 1152) plt. ylim (0, 2200) # plt. plot (x, y, 'rs-') line1 = ax. plot (x, y, 'B. -') ax. legend (line1, ('weibo ',) # Add a comma plt. show ()

The display effect is as follows:

Summary

The above is all about this article. I hope this article will help you learn or use python. If you have any questions, please leave a message, thank you for your support.

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.