import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = {'RCC': np.random.random(5) * 20,
'MG6': np.random.random(5) * 20,
'G35': np.random.random(5) * 20,
'Und': np.random.random(5) * 20}
df = pd.DataFrame(data)
ax = plt.axis([0,5,0,20])
df.plot(kind='bar', ax=plt.gca())
# barh,line,area...
s = df['RCC']
s.plot(kind='bar', ax=plt.gca())
# barh,line,area,pie...
ax=plt.gca()
# 去掉坐标轴边框
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
s.plot(kind='bar', ax=ax)
# 坐标轴标签
ax.set_xlabel("Car")
ax.set_ylabel("Percent")
# 标题
ax.set_title('ADD TITLE')
# 一次性清理坐标轴
ax.axis('off')
# 切换坐标系
ax=plt.gca(polar=True)
# 设置颜色
s.plot(kind='bar', ax=ax, color=['#1c99fd', '#ff7070', '#ffa555', '#85da45'])
中文乱码
from matplotlib.font_manager import FontProperties
chinese_font16 = FontProperties(fname='./font/ttf-wqy-microhei.ttf', size=16)
ax.set_title('标题', fontproperties=chinese_font16)
可选商用开源字体,文泉驿微米黑以及思源黑体
图形保存
plt.figure(figsize=(12,6), dpi=80)
plt.savefig('./test.png')
多图模式
figure: 图像 直观性图形输出的窗口个体。 直观解释: 画桌
axes: 坐标对象。 直观解释: 坐标纸
三个数字,行数+列数+序号
fig = plt.Figure(figsize=(12,6))
ax = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
更复杂的形式
gs = plt.GridSpec(3, 3)
ax = fig.add_subplot(gs[0,0])
fig.add_subplot(gs[2,1:])
fig.add_subplot(gs[1,:2])
fig.add_subplot(gs[:2,2])
s=df['RCC']
ax=plt.gca()
s.plot(kind='bar', ax=ax)
for i in range(s.shape[0]):
ax.text(i, s.values[i]+0.3, '{0:.1f}'.format(s.values[i]), fontsize=24,ha='center')
先看看直接转坐标系的效果 polar=True
s=df['RCC']
ax=plt.gca(polar=True)
theta = np.arange(0., 2 * np.pi, 2 * np.pi / s.shape[0])
ax.bar(theta, s.values, width=(2*np.pi/s.shape[0]), color=['#1c99fd', '#ff7070', '#ffa555'])
# 去掉坐标轴
ax.axis('off')
# 添加文字
for i in range(s.shape[0]):
ax.text(theta[i], 20, '{0:.1f}'.format(s.values[i]), rotation=int(theta[i]*180/np.pi)%180-90, va='center', ha='center', fontsize=14)
先看看去中心的效果, bottom=10
s=df['RCC']
ax=plt.gca(polar=True)
ax.axis('off')
#计算弧度宽度
wid = s * 2 * np.pi / sum(s)
#计算弧度起始值
theta = [0]
for j in range(0, len(wid)-1):
theta.append(theta[j]+wid[j]/2+wid[j+1]/2)
#环形图
ax.bar(theta, 10, bottom=10, color=['#1c99fd', '#ff7070', '#ffa555'], width=wid)
#补充文字
for j in range(0, s.shape[0]):
ax.text(theta[j], 15, '{0:.1f}'.format(s.values[j]), rotation=int(theta[j]*180/np.pi)%180-90, va='center', ha='center', fontsize=16)
import pygal
bar_chart = pygal.Bar()
bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])
bar_chart.render_to_file('Hello.svg')
dot_chart = pygal.Dot(x_label_rotation=30)
dot_chart.title = 'V8 benchmark results'
dot_chart.x_labels = ['Richards', 'DeltaBlue', 'Crypto', 'RayTrace', 'EarleyBoyer', 'RegExp', 'Splay', 'NavierStokes']
dot_chart.add('Chrome', [6395, 8212, 7520, 7218, 12464, 1660, 2123, 8607])
dot_chart.add('Firefox', [7473, 8099, 11700, 2651, 6361, 1044, 3797, 9450])
dot_chart.add('Opera', [3472, 2933, 4203, 5229, 5810, 1828, 9013, 4669])
dot_chart.add('IE', [43, 41, 59, 79, 144, 136, 34, 102])
https://matplotlib.org/2.1.2/api/_as_gen/matplotlib.pyplot.html
http://www.pygal.org/en/stable/index.html
目前无论是 matplotlib 还是 pygal 感觉效果都并不是非常满意,也可能是我的了解有限,更多的功能就需要你们去挖掘了。
同时,除了以上,还有两个方向可以去尝试
未测试 figure frameon=False 禁止画图形框