Skip to content

Instantly share code, notes, and snippets.

@xjcl
Created June 14, 2017 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xjcl/48dee3ea354c1c54a6231f99a4b78884 to your computer and use it in GitHub Desktop.
Save xjcl/48dee3ea354c1c54a6231f99a4b78884 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
data = [
['not Trump', 128-91],
['Trump', 91],
]
data_x = [d[0] for d in data][::-1]
data_y = [d[1] for d in data]
ADJUST = 3
y_pos = (np.arange(len(data)+ADJUST))[-len(data):]
y_tickpos = (np.arange(len(data)+ADJUST))[::-1]
print(y_pos)
fig = plt.gcf()
# fix ugly default layout
ax = plt.axes()
ax.xaxis.tick_top()
ax.xaxis.set_ticks_position('none')
ax.yaxis.set_ticks_position('none')
ax.xaxis.set_label_position('top')
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
pos1 = ax.get_position() # get the original position
pos2 = [pos1.x0, pos1.y0 + .07, pos1.width, pos1.height]
ax.set_position(pos2)
plt.barh(y_pos, data_y, color=['lightskyblue', 'lightcoral'], align='center', linewidth=0, alpha=0.5, height=.7)
plt.yticks(y_tickpos, data_x)
plt.xlabel('Topic of "A Closer Look" segment on "Late Night with Seth Meyers" (all eps)')
# https://stackoverflow.com/questions/7965743
def forceAspect(ax,aspect=1):
im = ax.get_images()
extent = im[0].get_extent()
ax.set_aspect(abs((extent[1]-extent[0])/(extent[3]-extent[2]))/aspect)
# forceAspect(ax, 1)
# ax.set_aspect(.2) # doesn't work
# ax.imshow(X=np.array([[31], [91]]), alpha=0, aspect=.15, ) # force dumb aspect ratio
# # i decided to just crop this manually in inkscape...
plt.tight_layout()
# plt.savefig('out.svg', bbox_inches='tight')
plt.savefig('seth.svg')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment