-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot_pandas_data.py
183 lines (160 loc) · 5.01 KB
/
plot_pandas_data.py
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# plot_pandas_data
import matplotlib.pyplot as plt
default_format = "{:g}"
def no_right_axis(ax, right_yticks, format_str):
return None
def plot_right_axis(ax, to_fn, inv_fn,
right_yticks=[],
format_str=default_format,
right_ylabel=""):
right_axis = ax.secondary_yaxis(
'right',
functions=(to_fn, inv_fn),
ylabel=right_ylabel)
if right_yticks == []:
yticks = ax.get_yticks()
right_yticks = [
to_fn(tick) for tick in yticks]
right_axis.set_yticks([], minor=True)
right_axis.set_yticks(right_yticks, minor=False)
right_ytick_labels = [
format_str.format(tick) for tick in right_yticks]
right_axis.set_yticklabels(right_ytick_labels, minor=False)
return right_axis
def plot_caption(
fig_nbr=0,
caption=None):
if caption is not None:
figtext = f"Figure {fig_nbr}: {caption}"
plt.figtext(0.5, -0.2, figtext, ha="center", fontsize="large")
def plot_by_fn_by_group(plot_fn, x, y, by, data,
ax=None,
xlabel=None,
ylabel=None,
xticks=[],
yticks=[],
legend_title=None,
marker='+',
right_axis_fn=no_right_axis,
right_yticks=[],
format_str=default_format,
fig_nbr=0,
caption=None):
if ax is None:
fig, ax = plt.subplots()
dgb = data.groupby(by)
for name, group in dgb:
means = group.groupby(x)[y].mean()
p = plot_fn(means, marker='')
color = p[0].get_color()
plot_fn(x, y,
data=group,
marker=marker,
linestyle='',
color=color,
label=str(name))
if plot_fn == plt.semilogx:
plt.ylim([0, data[y].max() * 1.1])
if xticks == []:
xticks = data[x].sort_values().unique()
xtick_labels = [
str(int(tick)) for tick in xticks]
ax.set_xticks([], minor=True)
plt.xticks(xticks, xtick_labels, rotation=90)
if yticks != []:
ytick_labels = [
default_format.format(tick) for tick in yticks]
plt.yticks(yticks, ytick_labels)
if right_axis_fn != no_right_axis:
right_axis = right_axis_fn(ax,
right_yticks=right_yticks,
format_str=format_str)
plt.xlabel(x if xlabel == None else xlabel)
plt.ylabel(y if ylabel == None else ylabel)
plt.legend(title=legend_title)
plot_caption(fig_nbr, caption)
def loglog_by_group(x, y, by, data, **kwargs):
return plot_by_fn_by_group(
plt.loglog, x, y, by, data, **kwargs)
def semilogx_by_group(x, y, by, data, **kwargs):
return plot_by_fn_by_group(
plt.semilogx, x, y, by, data, **kwargs)
def plot_by_fn(plot_fn, x, y, data,
ax=None,
xlabel=None,
ylabel=None,
xticks=[],
yticks=[],
marker='+',
right_axis_fn=no_right_axis,
right_yticks=[],
format_str=default_format,
fig_nbr=0,
caption=None):
if ax is None:
fig, ax = plt.subplots()
means = data.groupby(x)[y].mean()
p = plot_fn(means, marker='')
color = p[0].get_color()
if plot_fn == plt.semilogx:
plt.ylim([0, data[y].max() * 1.1])
plot_fn(x, y,
data=data,
marker=marker,
linestyle='',
color=color)
if xticks != []:
xtick_labels = [
str(int(tick)) for tick in xticks]
ax.set_xticks([], minor=True)
plt.xticks(xticks, xtick_labels, rotation=90)
if yticks != []:
ytick_labels = [
format_str.format(tick) for tick in yticks]
plt.yticks(yticks, ytick_labels)
ax.set_yticks([], minor=True)
if right_axis_fn != no_right_axis:
right_axis = right_axis_fn(ax,
right_yticks=right_yticks,
format_str=format_str)
plt.xlabel(x if xlabel == None else xlabel)
plt.ylabel(y if ylabel == None else ylabel)
plot_caption(fig_nbr, caption)
return p
def plot_by_xy_labels_fn(plot_fn, x, data,
ax=None,
xticks=[],
yticks=[],
right_axis_fn=no_right_axis,
right_yticks=[],
format_str=default_format,
marker='+',
fig_nbr=0,
caption=None):
if ax is None:
fig, ax = plt.subplots()
p = plot_fn(
data,
ax=ax,
right_axis_fn=right_axis_fn,
right_yticks=right_yticks,
marker=marker)
if xticks == []:
xticks = data[x].sort_values().unique()
xtick_labels = [
str(int(tick)) for tick in xticks]
ax.set_xticks([], minor=True)
plt.xticks(xticks, xtick_labels, rotation=90)
if yticks != []:
ytick_labels = [
default_format.format(tick) for tick in yticks]
plt.yticks(yticks, ytick_labels)
ax.set_yticks([], minor=True)
plot_caption(fig_nbr, caption)
return p
def loglog(x, y, data, **kwargs):
return plot_by_fn(
plt.loglog, x, y, data, **kwargs)
def semilogx(x, y, data, **kwargs):
return plot_by_fn(
plt.semilogx, x, y, data, **kwargs)