画图中文乱码
Python3 matplotlib 画图时中文会显示成乱码
解决方法一:
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
解决方法二(windows):
- 先找打matplotlib配置文件matplotlibrc的位置。
- 修改matplotlibrc文件
- 将 #font.family : sans-serif 这一行前面的'#‘去掉。
- 将#font.sans-serif : Microsoft YaHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif, 前面的“#”去掉,加上Microsoft YaHei,
参考例子
多图
# %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
网友评论