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.