日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

python绘制折线图中文图例不显示_python使用matplotlib绘图时图例显示问题的解决...

發(fā)布時間:2025/4/5 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python绘制折线图中文图例不显示_python使用matplotlib绘图时图例显示问题的解决... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前言

matplotlib是基于Python語言的開源項目,旨在為Python提供一個數(shù)據(jù)繪圖包。在使用Python matplotlib庫繪制數(shù)據(jù)圖時,需要使用圖例標(biāo)注數(shù)據(jù)類別,但是傳參時,會出現(xiàn)圖例解釋文字只顯示第一個字符,需要在傳參時在參數(shù)后加一個逗號(應(yīng)該是python語法,加逗號,才可以把參數(shù)理解為元組類型吧),就可解決這個問題,

示例如下

import numpy as np

import matplotlib.pyplot as plt

from matplotlib.ticker import MultipleLocator

from pylab import mpl

xmajorLocator = MultipleLocator(24 * 3) #將x軸主刻度標(biāo)簽設(shè)置為24 * 3的倍數(shù)

ymajorLocator = MultipleLocator(100 * 2) #將y軸主刻度標(biāo)簽設(shè)置為100 * 2的倍數(shù)

# 設(shè)置中文字體

mpl.rcParams['font.sans-serif'] = ['SimHei']

# 導(dǎo)入文件數(shù)據(jù)

data = np.loadtxt('H:/dataset/爸爸去哪兒/統(tǒng)計數(shù)據(jù)_每小時_ba.csv', delimiter=',', dtype=int)

# 截取數(shù)組數(shù)據(jù)

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') #x坐標(biāo)軸的網(wǎng)格使用主刻度

ax.yaxis.grid(True, which='major') #x坐標(biāo)軸的網(wǎng)格使用主刻度

plt.xlabel('時間索引')

plt.ylabel('活動頻數(shù)')

plt.title('折線圖')

plt.xlim(0, 1152)

plt.ylim(0, 2200)

#plt.plot(x, y, 'rs-')

line1 = ax.plot(x, y, 'b.-')

ax.legend(line1, ('微博'))

plt.show()

顯示效果如下

代碼修改

from pylab import mpl

xmajorLocator = MultipleLocator(24 * 3) #將x軸主刻度標(biāo)簽設(shè)置為24 * 3的倍數(shù)

ymajorLocator = MultipleLocator(100 * 2) #將y軸主刻度標(biāo)簽設(shè)置為100 * 2的倍數(shù)

# 設(shè)置中文字體

mpl.rcParams['font.sans-serif'] = ['SimHei']

# 導(dǎo)入文件數(shù)據(jù)

data = np.loadtxt('H:/dataset/爸爸去哪兒/統(tǒng)計數(shù)據(jù)_每小時_ba.csv', delimiter=',', dtype=int)

# 截取數(shù)組數(shù)據(jù)

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') #x坐標(biāo)軸的網(wǎng)格使用主刻度

ax.yaxis.grid(True, which='major') #x坐標(biāo)軸的網(wǎng)格使用主刻度

plt.xlabel('時間索引')

plt.ylabel('活動頻數(shù)')

plt.title('折線圖')

plt.xlim(0, 1152)

plt.ylim(0, 2200)

#plt.plot(x, y, 'rs-')

line1 = ax.plot(x, y, 'b.-')

ax.legend(line1, ('微博',)) # 多加一個逗號

plt.show()

顯示效果如下

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家學(xué)習(xí)或者使用python能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

總結(jié)

以上是生活随笔為你收集整理的python绘制折线图中文图例不显示_python使用matplotlib绘图时图例显示问题的解决...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。