python画图

画图中文乱码

Python3 matplotlib 画图时中文会显示成乱码
解决方法一:
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False

解决方法二(windows):

  1. 先找打matplotlib配置文件matplotlibrc的位置。
  2. 修改matplotlibrc文件
    1. 将 #font.family : sans-serif 这一行前面的'#‘去掉。
    2. 将#font.sans-serif : Microsoft YaHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif, 前面的“#”去掉,加上Microsoft YaHei,

参考例子

25个Matplotlib图的汇总(https://mp.weixin.qq.com/s?__biz=MzI4MjM1NzQ0OA==&mid=2247485327&idx=1&sn=4337b42d8bf5e9904dadb1dd2aaee79b&chksm=eb9a7c28dcedf53e34d77763674f8a3d09a3440306ca2541d4c32bd64e2587e90340b6db2df7&scene=0&xtrack=1&key=adf3334aef273b737154864e0494b3ce33c1b6609e5ed2b550471c5feb8c25e46009f2180e592897339fa778b7a08cb4c5e3582b07abc9cec9e93d31cf3a8175bcbeba0bc8d26345d263a3080f09e480&ascene=14&uin=Mjg0MzIwMTcxNg%3D%3D&devicetype=Windows+10&version=62080079&lang=zh_CN&exportkey=A0wBwIk18VoS5nhJ%2BqEU7Bk%3D&pass_ticket=9njrEQAlT4fIK3OVzkD0xEXD3VFJvAib8qlxh1klg6hMnNEZUXM9%2FrLWRPMPD6St

多图

# %matplotlib qt 在IPython运行窗口运行,弹出图

from matplotlib import pyplot as plt
import random

dat1 = pd.DataFrame({'y1':[random.uniform(0,1) for i in range(10)], 'y2': [random.uniform(0,1) for i in range(10)]})
dat2 = pd.DataFrame({'y1':[random.uniform(0,1) for i in range(10)], 'y2': [random.uniform(0,1) for i in range(10)]})
dat3 = pd.DataFrame({'y1':[random.uniform(0,1) for i in range(10)], 'y2': [random.uniform(0,1) for i in range(10)]})
dat4 = pd.DataFrame({'y1':[random.uniform(0,1) for i in range(10)], 'y2': [random.uniform(0,1) for i in range(10)]})

plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False

fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
ax4 = fig.add_subplot(2,2,4)

plt.subplots_adjust(left=0.05, bottom=0.1, right=0.95, top=0.92,
                        wspace=0.15, hspace=0.25)   # 调整子图布局
ax1.plot(dat1[['y1', 'y2']]/500)
ax1.legend(['y1','y2'], loc='bottomright', frameon=False)  # 图例
ax1.set_title("误差1")   # 子图标题
ax1.set_ylim(0, 1)  # y轴坐标刻度范围

ax2.plot(dat2[['y1', 'y2']])
ax2.set_title("误差2")
ax2.set_ylim(0, 1)

ax3.plot(dat3[['y1', 'y2']])
ax3.set_title("误差3")
ax3.set_ylim(0, 1)

ax4.plot(dat4[['y1', 'y2']])
ax4.set_title("误差2")
ax4.set_ylim(0, 1)

fig.show()
image.png

画带权有向图

import networkx as nx
import matplotlib.pyplot as plt

x = [('A1', 'B1',{'weight':4}), ('A2', 'B1',{'weight':3}), ('A1', 'B2',{'weight':2}),]
G = nx.DiGraph()
G.add_edges_from(x)
val_map = {'A1': 0.8,
           'A2': 0.5714285714285714,
           'B1': 0.0,
           'B2': 1}  

values = ['r' if node in ['A1','A2'] else 'g' for node in G.nodes()]

# 边设置
edge_colours = ['green' for edge in G.edges()]
edge_labels=dict([((u,v,),d['weight']) for u,v,d in G.edges(data=True)])

pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos, cmap=plt.get_cmap('jet'), 
                       node_color = values, node_size = 500)   # 绘制节点
nx.draw_networkx_labels(G, pos)  # 节点label
nx.draw_networkx_edges(G, pos, edgelist=x, edge_color='r', arrows=True)  # 绘制边,带箭头
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels) # 绘制图中边的权重
plt.show()
image.png

你可能感兴趣的:(python画图)