Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

# coding: utf-8 

# Copyright (c) Pymatgen Development Team. 

# Distributed under the terms of the MIT License. 

 

from __future__ import division, unicode_literals 

 

""" 

This module provides plotting capabilities for battery related applications. 

""" 

 

 

__author__ = "Shyue Ping Ong" 

__copyright__ = "Copyright 2012, The Materials Project" 

__version__ = "0.1" 

__maintainer__ = "Shyue Ping Ong" 

__email__ = "shyuep@gmail.com" 

__date__ = "Jul 12, 2012" 

 

 

from collections import OrderedDict 

from pymatgen.util.plotting_utils import get_publication_quality_plot 

 

 

class VoltageProfilePlotter(object): 

""" 

A plotter to make voltage profile plots for batteries. 

 

Args: 

xaxis: The quantity to use as the xaxis. Can be either capacity (the 

default), or the frac_x. 

""" 

 

def __init__(self, xaxis="capacity"): 

self._electrodes = OrderedDict() 

self.xaxis = xaxis 

 

def add_electrode(self, electrode, label=None): 

""" 

Add an electrode to the plot. 

 

Args: 

electrode: An electrode. All electrodes satisfying the 

AbstractElectrode interface should work. 

label: A label for the electrode. If None, defaults to a counting 

system, i.e. 'Electrode 1', 'Electrode 2', ... 

""" 

if not label: 

label = "Electrode {}".format(len(self._electrodes) + 1) 

self._electrodes[label] = electrode 

 

def get_plot_data(self, electrode): 

x = [] 

y = [] 

cap = 0 

most_discharged = electrode[-1].frac_discharge 

norm = most_discharged / (1 - most_discharged) 

for vpair in electrode: 

if self.xaxis == "capacity": 

x.append(cap) 

cap += vpair.mAh / electrode.normalization_mass 

x.append(cap) 

else: 

x.append(vpair.frac_charge / (1 - vpair.frac_charge) / norm) 

x.append(vpair.frac_discharge / (1 - vpair.frac_discharge) 

/ norm) 

y.extend([vpair.voltage] * 2) 

 

x.append(x[-1]) 

y.append(0) 

return x, y 

 

def get_plot(self, width=8, height=8): 

""" 

Returns a plot object. 

 

Args: 

width: Width of the plot. Defaults to 8 in. 

height: Height of the plot. Defaults to 6 in. 

 

Returns: 

A matplotlib plot object. 

""" 

plt = get_publication_quality_plot(width, height) 

for label, electrode in self._electrodes.items(): 

(x, y) = self.get_plot_data(electrode) 

plt.plot(x, y, '-', linewidth=2, label=label) 

 

plt.legend() 

if self.xaxis == "capacity": 

plt.xlabel('Capacity (mAh/g)') 

else: 

plt.xlabel('Fraction') 

plt.ylabel('Voltage (V)') 

plt.tight_layout() 

return plt 

 

def show(self, width=8, height=6): 

""" 

Show the voltage profile plot. 

 

Args: 

width: Width of the plot. Defaults to 8 in. 

height: Height of the plot. Defaults to 6 in. 

""" 

self.get_plot(width, height).show() 

 

def save(self, filename, image_format="eps", width=8, height=6): 

""" 

Save the plot to an image file. 

 

Args: 

filename: Filename to save to. 

image_format: Format to save to. Defaults to eps. 

""" 

self.get_plot(width, height).savefig(filename, format=image_format)