newsworthycharts 1.71.3__py3-none-any.whl → 1.71.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- __version__ = "1.71.3"
1
+ __version__ = "1.71.5"
2
2
 
3
3
  from .chart import Chart
4
4
  from .choroplethmap import ChoroplethMap
@@ -15,11 +15,18 @@ class BumpChart(SerialChart):
15
15
  """
16
16
 
17
17
  def __init__(self, *args, **kwargs):
18
+ label_placement = kwargs.get("label_placement", None)
19
+ if label_placement not in ["legend", "outside"]:
20
+ self.label_placement = "none"
21
+
18
22
  super(BumpChart, self).__init__(*args, **kwargs)
19
23
 
20
24
  if self.line_width is None:
21
25
  self.line_width = 0.9
22
- self.label_placement = 'none'
26
+ if self.line_marker_size is None:
27
+ self.line_marker_size = 6
28
+ if not label_placement:
29
+ self.label_placement = 'right'
23
30
  self.type = "line"
24
31
  self.decimals = 0
25
32
  self.revert_value_axis = True
@@ -29,7 +36,6 @@ class BumpChart(SerialChart):
29
36
  self.accentuate_baseline = False
30
37
 
31
38
  self.line_marker = "o-"
32
- self.line_marker_size = 5
33
39
 
34
40
  def _get_line_colors(self, i, *args):
35
41
  if not self.data:
@@ -39,17 +45,77 @@ class BumpChart(SerialChart):
39
45
  return self._nwc_style["strong_color"]
40
46
  elif self.colors and i < len(self.colors):
41
47
  return self.colors[i]
42
- return self._nwc_style["neutral_color"]
48
+ # alternate between dark gray and light gray
49
+ if i % 2 == 0:
50
+ return "darkgray"
51
+ else:
52
+ return "lightgray"
53
+
54
+ """
55
+ def _get_marker_fill(self, i):
56
+ pass
57
+ """
43
58
 
44
59
  def _after_add_data(self):
45
60
  # Print out every rank
46
61
  if self.data.max_val < 30:
47
62
  _range = list(range(1, int(self.data.max_val) + 1))
48
63
  self.ax.yaxis.set_ticks(_range, _range)
64
+
65
+ # Recolor markers with more than one line passing through
66
+ # (MPL does not allow access to individual markers, so we'll overwrite them)
67
+ for date in self.data.x_points:
68
+ value_colors = {}
69
+ for i, serie in enumerate(self.data):
70
+ values = np.array(self.serie_values[i], dtype=np.float64)
71
+ dates = [x[0] for x in serie]
72
+ color = self._get_line_colors(i)
73
+ if date not in dates:
74
+ continue
75
+ idx = dates.index(date)
76
+ if np.isnan(values[idx]):
77
+ continue
78
+ val = int(values[idx])
79
+ if val not in value_colors:
80
+ value_colors[val] = []
81
+ value_colors[val].append(color)
82
+ for val, colors in value_colors.items():
83
+ if len(colors) < 2:
84
+ continue
85
+ elif len(colors) == 2:
86
+ self.ax.plot(
87
+ to_date(date),
88
+ val,
89
+ self.line_marker,
90
+ markersize=self.line_marker_size,
91
+ color="None",
92
+ fillstyle="left",
93
+ markeredgewidth=0,
94
+ markerfacecolor=colors[0],
95
+ markerfacecoloralt=colors[1],
96
+ zorder=100,
97
+ )
98
+ else:
99
+ self.ax.plot(
100
+ to_date(date),
101
+ val,
102
+ self.line_marker,
103
+ markersize=self.line_marker_size,
104
+ color=self._nwc_style["neutral_color"],
105
+ markeredgewidth=0,
106
+ zorder=100,
107
+ )
49
108
  # Add labels
50
- slots_occupied = {
109
+ slots_occupied_right = {
51
110
  to_date(k): [] for k in self.data.x_points
52
111
  }
112
+ slots_occupied_left = {
113
+ to_date(k): [] for k in self.data.x_points
114
+ }
115
+ # distance between rank ticks
116
+ # y1 = self.ax.transData.transform((0, 1))
117
+ # y2 = self.ax.transData.transform((0, 2))
118
+ # dist = abs(y1[1] - y2[1])
53
119
  for i, serie in enumerate(self.data):
54
120
  values = np.array(self.serie_values[i], dtype=np.float64)
55
121
  dates = [to_date(x[0]) for x in serie]
@@ -59,20 +125,71 @@ class BumpChart(SerialChart):
59
125
  (d, values[idx])
60
126
  for (idx, d) in enumerate(dates) if idx == len(dates) - 1 or np.isnan(values[idx + 1])
61
127
  ]
62
- for ep in endpoints:
63
- position = ep[1]
64
- while position in slots_occupied[ep[0]]:
65
- position += 1
66
- slots_occupied[ep[0]].append(position)
67
- self._annotate_point(
68
- self.labels[i],
69
- (ep[0], position),
70
- "right",
71
- offset=15,
72
- color=color,
73
- va="center",
74
- # arrowprops=dict(arrowstyle="->", color=color),
75
- )
128
+
129
+ startpoints = []
130
+ if self.label_placement == "both":
131
+ startpoints = [
132
+ (d, values[idx])
133
+ for (idx, d) in enumerate(dates)
134
+ if (idx == 0 or np.isnan(values[idx - 1]) # only if 2 consecutive values come after
135
+ and (idx < len(dates) - 2)
136
+ and not np.isnan(values[idx + 1])
137
+ and not np.isnan(values[idx + 2]))
138
+ ]
139
+ elif self.label_placement == "left":
140
+ startpoints = [
141
+ (d, values[idx])
142
+ for (idx, d) in enumerate(dates)
143
+ if idx == 0 or np.isnan(values[idx - 1])
144
+ ]
145
+
146
+ if self.label_placement in ["left", "both"]:
147
+ # We need to move y spine to the left to make room for labels
148
+ # To save time we'll only measure the with of the longest text string
149
+ # There will be edge cases where shorter strings taker up more space
150
+ longest_sp_label = max([len(str(x[1])) for x in startpoints])
151
+ dummy_text = self.ax.text(0, 0, longest_sp_label, fontsize=self._nwc_style["annotation.fontsize"])
152
+ _bbox = dummy_text.get_window_extent(renderer=self._fig.canvas.get_renderer())
153
+ self.ax.spines['left'].set_position(('outward', _bbox.width * 1.15 + 15)) # Add a bit extra space
154
+ dummy_text.remove()
155
+
156
+ if self.label_placement in ["right", "both"]:
157
+ for ep in endpoints:
158
+ position = ep[1]
159
+ while position in slots_occupied_right[ep[0]]:
160
+ position += 1
161
+ # pos_diff = position - ep[1]
162
+ slots_occupied_right[ep[0]].append(position)
163
+ self._annotate_point(
164
+ self.labels[i],
165
+ # (ep[0], ep[1]),
166
+ (ep[0], position),
167
+ "right",
168
+ offset=15,
169
+ color=color,
170
+ va="center",
171
+ # xytext=(15, -abs(y1[1] - y2[1]) * pos_diff),
172
+ # arrowprops=dict(arrowstyle="->", color=color) if pos_diff > 1 else None,
173
+ zorder=99,
174
+ )
175
+ if self.label_placement in ["left", "both"]:
176
+ for sp in startpoints:
177
+ position = sp[1]
178
+ while position in slots_occupied_left[sp[0]]:
179
+ position += 1
180
+ # pos_diff = position - sp[1]
181
+ slots_occupied_left[sp[0]].append(position)
182
+ self._annotate_point(
183
+ self.labels[i],
184
+ (sp[0], position),
185
+ "left",
186
+ offset=15,
187
+ color=color,
188
+ va="center",
189
+ zorder=99,
190
+ )
191
+ # Add space for labels on the left
192
+
76
193
  """
77
194
  labels = []
78
195
  for i, serie in enumerate(self.data):
@@ -52,7 +52,9 @@ class SerialChart(Chart):
52
52
  self.colors = None
53
53
 
54
54
  # Optional: where to place series label
55
- self.label_placement = "legend" # legend|inline|outside|line
55
+ # Could be set already by subclass
56
+ if not hasattr(self, "label_placement"):
57
+ self.label_placement = "legend" # legend|inline|outside|line
56
58
 
57
59
  # Optional: annotate each point with a value label
58
60
  self.value_labels = False
@@ -254,6 +256,9 @@ class SerialChart(Chart):
254
256
  color = self._nwc_style["strong_color"]
255
257
  else:
256
258
  color = self._nwc_style["neutral_color"]
259
+ marker_fill = None
260
+ if hasattr(self, "_get_marker_fill"):
261
+ marker_fill = self._get_marker_fill(i)
257
262
 
258
263
  line, = self.ax.plot(
259
264
  dates,
@@ -261,6 +266,8 @@ class SerialChart(Chart):
261
266
  self.line_marker,
262
267
  markersize=self.line_marker_size,
263
268
  color=color,
269
+ markerfacecolor=marker_fill,
270
+ markeredgewidth=0,
264
271
  zorder=zo,
265
272
  lw=lw,
266
273
  )
@@ -1,9 +1,9 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: newsworthycharts
3
- Version: 1.71.3
3
+ Version: 1.71.5
4
4
  Summary: Matplotlib wrapper to create charts and publish them on Amazon S3
5
5
  Home-page: https://github.com/jplusplus/newsworthycharts
6
- Download-URL: https://github.com/jplusplus/newsworthycharts/archive/1.71.3.tar.gz
6
+ Download-URL: https://github.com/jplusplus/newsworthycharts/archive/1.71.5.tar.gz
7
7
  Author: Jens Finnäs and Leo Wallentin, J++ Stockholm
8
8
  Author-email: stockholm@jplusplus.org
9
9
  License: MIT
@@ -179,6 +179,7 @@ _ Inherits from SerialChart _
179
179
 
180
180
  _ Inherits from SerialChart _
181
181
 
182
+ - label_placement = "right" # [left|right|both|outside|legend]
182
183
  - highlight = None # The label value to of a line to highlight
183
184
  - line_marker = "o-" # Matplotlib line marker type.
184
185
  - line_marker_size = 5 # Matplotlib line marker size
@@ -270,6 +271,16 @@ Roadmap
270
271
  Changelog
271
272
  ---------
272
273
 
274
+ - 1.71.5
275
+
276
+ - zorder fixes in BumpChart
277
+ - Add `label_placement` = [left|right|both|outside|legend] to BumpChart. Default is 'right'
278
+ - Use alternating shades of gray in BumpChart
279
+
280
+ - 1.71.4
281
+
282
+ - BumpChart: Use dual colors in line markers when rank is shared
283
+
273
284
  - 1.71.3
274
285
 
275
286
  - Revert 1.71.2 changes to rendering, to make file sizes predictable again
@@ -1,4 +1,4 @@
1
- newsworthycharts/__init__.py,sha256=_KQ5nZbWoOfbRV3YzJrGxr7xt4UnnweP_qR4OS8t-P8,1221
1
+ newsworthycharts/__init__.py,sha256=pPubTzsF-HxZnryr86J7ir5eaH5q595foyBekjtB-9s,1221
2
2
  newsworthycharts/bubblemap.py,sha256=nkocWmpiFgfjEuJGAsthjY5X7Q56jXWsZHUGXw4PwgE,2587
3
3
  newsworthycharts/categoricalchart.py,sha256=Vr-0yFms0hEVCeUa3vLt3FYBqpX4xLQ8YGPc4LGQN_A,18368
4
4
  newsworthycharts/chart.py,sha256=eXgrNCmfE52rFsvTDkMpCZsmL0oSezdkH---LGYsES8,35062
@@ -6,10 +6,10 @@ newsworthycharts/choroplethmap.py,sha256=Si-01213rWqDKINkhmKV6x5iSMumQveJfrlCnqY
6
6
  newsworthycharts/datawrapper.py,sha256=RRkAVTpfP4updKxUIBaSmKuBi2RUVPaBRF8HDQhlGGA,11250
7
7
  newsworthycharts/map.py,sha256=c409jEO4L8Yr780sJRC0RchR44roAlgOUDAkuk1SfRg,6057
8
8
  newsworthycharts/rangeplot.py,sha256=NE1W9TnmlpK6T3RvBJOU3nd73EXqkj17OY9i5zlw_cQ,8366
9
- newsworthycharts/rankchart.py,sha256=b7MJpei08fzDAbVS1Y4qK9KSmcO4UbnRFwhND-8Ms4Y,4211
9
+ newsworthycharts/rankchart.py,sha256=1wn2ZP67xH-F_Wz-XnTOr2gAXMXMCufe6eFRHXY7Fms,9285
10
10
  newsworthycharts/scatterplot.py,sha256=weHubdMsDGaBTXejg2TqBNPTQ1K-QBpZqJiyQ8EOEc4,5084
11
11
  newsworthycharts/seasonalchart.py,sha256=rr55yqJUkaYDR9Ik98jes6574oY1U8t8LwoLE3gClW4,1967
12
- newsworthycharts/serialchart.py,sha256=aZP8QacKdRnHADiJcQ7d0UDLUlLVnlRdjJzSuGFvLFg,29773
12
+ newsworthycharts/serialchart.py,sha256=gy5BIOrzte529JFII1jFbEDDejnB7FmO_kzDHXdE21o,30105
13
13
  newsworthycharts/storage.py,sha256=myERhlpvXyExXxUByBq9eW1bWkCyfH9SwTZbsWSyy3Q,4301
14
14
  newsworthycharts/stripechart.py,sha256=9B6PX2MyLuKNQ8W0OGdKbP0-U32kju0K_NHHwwz_J68,1547
15
15
  newsworthycharts/custom/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -29,8 +29,8 @@ newsworthycharts/rc/newsworthy,sha256=yOIZvYS6PG1u19VMcdtfj9vbihKQsey5IprwqK59Kg
29
29
  newsworthycharts/translations/datawrapper_regions.csv,sha256=fzZcQRX6RFMlNNP8mpgfYNdR3Y0QAlQxDXk8FXTaWWI,9214
30
30
  newsworthycharts/translations/regions.py,sha256=Nv1McQjggD4S3JRu82rDMTG3pqUVR13E5-FBpSYbm98,239
31
31
  newsworthycharts/translations/se_municipalities.csv,sha256=br_mm-IvzQtj_W55_ATREhJ97jWnCweBFlDAVY2EBxA,7098
32
- newsworthycharts-1.71.3.dist-info/LICENSE.txt,sha256=Sq6kGICrehbhC_FolNdXf0djKjTpv3YqjFCIYsxdQN4,1069
33
- newsworthycharts-1.71.3.dist-info/METADATA,sha256=nXe9_uwI6gFGEBLs9yRC8N_IMORYb-v25Kex_ESlDlM,33287
34
- newsworthycharts-1.71.3.dist-info/WHEEL,sha256=cVxcB9AmuTcXqmwrtPhNK88dr7IR_b6qagTj0UvIEbY,91
35
- newsworthycharts-1.71.3.dist-info/top_level.txt,sha256=dn_kzIj8UgUCMsh1PHdVEQJHVGSsN7Z8YJF-8xXa8n0,17
36
- newsworthycharts-1.71.3.dist-info/RECORD,,
32
+ newsworthycharts-1.71.5.dist-info/LICENSE.txt,sha256=Sq6kGICrehbhC_FolNdXf0djKjTpv3YqjFCIYsxdQN4,1069
33
+ newsworthycharts-1.71.5.dist-info/METADATA,sha256=QBAoSQvoGUQgik45tPfmTO43h2Zwu8nc_U1xxJoBjHQ,33612
34
+ newsworthycharts-1.71.5.dist-info/WHEEL,sha256=cVxcB9AmuTcXqmwrtPhNK88dr7IR_b6qagTj0UvIEbY,91
35
+ newsworthycharts-1.71.5.dist-info/top_level.txt,sha256=dn_kzIj8UgUCMsh1PHdVEQJHVGSsN7Z8YJF-8xXa8n0,17
36
+ newsworthycharts-1.71.5.dist-info/RECORD,,