python:透明背景图
图形是白色(颜色、字体可调),背景是透明的。适用于暗色调背景。
代码:
#encoding=utf-8
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
#输入因变量
y1 = pd.read_csv('11.csv')
y1 = np.array(y1)
y2 = pd.read_csv('12.csv')
y2 = np.array(y2)
#assert y1.shape[0]==y2.shape[0], '两个因变量个数不相等!'
fig,ax=plt.subplots(figsize=(10,9))
#设置自变量的范围和个数
x = np.linspace(1, 10, y1.shape[0])
#画图
ax.plot(x,y1,'r-',label='x', color='red', linestyle='-', marker='*', markerfacecolor='coral', markersize='10')
ax.plot(x,y2,'b-',label='y', color='blue', linestyle='--', marker='o', markerfacecolor='deeppink', markersize='10')
#设置坐标轴
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['left'].set_color('white')
ax.spines['left'].set_linewidth(3)
ax.spines['bottom'].set_color('white')
ax.spines['bottom'].set_linewidth(3)
#ax.set_xlim(0, 9.5)
#ax.set_ylim(0, 1.4)
ax.set_xlabel('time(s)', color='white', fontsize=15)
ax.set_ylabel('distance(m)', color='white', fontsize=15)
#设置刻度
ax.tick_params(axis='both', labelcolor='white', labelsize=15, width=3, color='white')
#显示网格
#ax.grid(True, linestyle='-.', color='white')
ax.yaxis.grid(True, which='major', linestyle='-.', color='gray')
#添加图例
legend = ax.legend(loc='best', fontsize=15)
#设置图例的颜色和透明度
for leg_text in legend.get_texts():
leg_text.set_color('white')
frame = legend.get_frame()
frame.set_facecolor('none')
frame.set_alpha(0)
plt.show()
fig.savefig('1.png', transparent=True)
11.csv:
data
1.2
2.9
7.0
3.4
4.0
9.3
5.1
6.7
8.8
3.8
12.csv:
data
1.9
2.2
3.6
4.1
7.3
0.1
9.4
5.5
8.3
7.2
代码:
#coding=utf-8
import numpy as np
import matplotlib.pyplot as plt
from pylab import mpl
import scipy.stats as stats
#在每个条上显示y坐标值
def autolabel(rects):
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2, height, '%.2f'%height, ha='center', va='bottom', color='white', fontsize=15)
datas = [[0.87, 0.40, 0.56, 0.21],
[0.97, 0.50, 0.33, 0.39],
[0.88, 0.30, 0.44, 0.52],
[0.25, 0.23, 0.17, 0.19],
[0.73, 0.33, 0.45, 0.28],
[0.45, 0.28, 0.91, 0.31]]
#条的宽度
width = 0.5
#每组条的起始横坐标
ind = np.arange(width, width*(1+len(datas))*len(datas[0]), width*(1+len(datas)))
colors = ['red','orange','yellow','green','cyan','blue','purple','aliceblue','antiquewhite','aqua','aquamarine','azure']
#画图
pics=[]
fig,ax=plt.subplots(figsize=(20,9))
for i in range(len(datas)):
pics.append(ax.bar(ind+i*width, datas[i], width, color=colors[i], edgecolor='white'))
autolabel(pics[i])
#设置坐标轴
ax.spines['left'].set_color('white')
ax.spines['left'].set_linewidth(3)
ax.spines['bottom'].set_color('white')
ax.spines['bottom'].set_linewidth(3)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
#ax.set_xlim(0, 9.5)
#ax.set_ylim(0, 1.4)
ax.set_ylabel('distance(m)', color='white', fontsize=15)
#设置网格
#ax.grid(True, linestyle='-.', color='white')
ax.yaxis.grid(True, which='major', linestyle='-.', color='white')
#设置刻度
ax.tick_params(axis='both', labelcolor='white', labelsize=15, width=3, color='white')
#横坐标标签
ax.set_xticks(ind + width*(len(datas)-1)/2)
xNames = []
for i in range(len(datas[0])):
xNames.append('section'+str(i+1))
xNames=tuple(xNames)
ax.set_xticklabels(xNames, color='white', fontsize=15)
#添加图例
legendNames = []
for i in range(len(datas)):
legendNames.append('test'+str(i+1))
legendNames=tuple(legendNames)
legend = ax.legend(pics, legendNames, fontsize=15)
#设置图例的颜色和透明度
for leg_text in legend.get_texts():
leg_text.set_color('white')
frame = legend.get_frame()
frame.set_facecolor('none')
frame.set_alpha(0)
plt.show()
#是否透明
fig.savefig('1.png', transparent=True)
[致谢]