解決python中matplotlib繪圖中文顯示問題__python

來源:互聯網
上載者:User
matplotlib是支援unicode編碼的,出現圖1的問題主要是沒有找到合適的中文字型,解決方案有兩個:
1.直接修改設定檔matplotlibrc
這種方法我沒有試過,因為我安裝的是python(x,y),設定檔放的地方不一定一致,所以就選擇了下面的方法
2.在代碼中動態設定(推薦方式)
這種方式不需要修改設定檔,比較方便,推薦該方法,下面是具體步驟:
首先要再python指令碼中的開頭加上後面的內容:#-*- coding: utf-8 -*-,即用utf8編碼
然後在代碼中動態設定字型,下面是主要的幾行代碼
from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as plt
font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14) 

plt.xlabel(u"電壓差(V)", fontproperties=font)
plt.ylabel(u"介質損耗角差(度)", fontproperties=font)
plt.title(u"介質損耗角和荷電狀態SOC關係圖",fontproperties=font)

下面舉個具體的例子,因為我在網上看了很多例子,都解決不了這個問題,為了方便大家,下面貼出代碼,需要的話可以直接貼過去運行:

#-*- coding: utf-8 -*-
from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as plt
font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14) 
plt.figure(figsize=(6,6))


x = [1,2,3,4,5,6,7,8]
y = []
for i in x:
    y.append(-(i*i)+i+3)


plt.plot(x, y)
plt.title(u'測試程式', fontproperties=font)
plt.xlabel(u'x軸', fontproperties=font)
plt.ylabel(u'y軸', fontproperties=font)
plt.grid(True)
plt.show()

下面是程式的輸出


文章轉載:http://blog.chinaunix.net/uid-26611383-id-3521248.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.