{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# [Goulib](../notebook.ipynb).colors\n", "color toolbox\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%matplotlib notebook \n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from Goulib.notebook import *\n", "from Goulib.colors import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Color objects" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "~blue" ], "text/plain": [ "Color('~blue')" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Color('#3C14DC')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "red=Color('red') #Color objects can be init'ed by a name (as in matplotlib or html)\n", "green=Color('#00ff00') # by RGB hex string,\n", "blue=Color((0,0,1)) # by RGB triplet\n", "\n", "cmyk=Color((.45,.12,.67,.05),'cmyk') # or by specifying the colorspace used\n", "lab=Color((47.0, 68.0, 45.0),'Lab')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "red lime blue ~yellowgreen ~crimson" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "h(red,green,blue,cmyk,lab) #colors automagically recieve a name and have an HTML representation" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "~palevioletred ~seagreen ~mediumorchid ~palevioletred ~midnightblue ~royalblue ~purple ~blue ~thistle ~mediumvioletred" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from random import random\n", "r=[Color((random(),random(),random())) for _ in range(10)]\n", "h(*r) # unknown colors recieve name of the nearest known color with a tile before" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "cyan magenta yellow" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#colors can be added\n", "cyan=blue+green\n", "magenta=blue+red\n", "yellow=red+green\n", "h(cyan, magenta, yellow) # see ? the names of calculated colors are correct !" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "black" ], "text/plain": [ "Color('black')" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "magenta-cyan-yellow #colors can be substracted too" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "~darkviolet" ], "text/plain": [ "Color('~darkviolet')" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "-cmyk # unary minus gives the complementary color" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "teal #008080" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "c= Color('gray')-red # colors can be substracted too. \n", "h(c,c.hex) # notice RGB values were bounded" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Colorspaces and conversion" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('CMYK', 'XYZ', 'xyY', 'Lab', 'Luv', 'HSV', 'RGB', 'HEX')" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Colors can be initialized and converted to-from any of the following colorspaces\n", "colorspaces" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "rgb hex cmyk lab\n" ] } ], "source": [ "#each Color keeps track of the colorspace used at construction\n", "print(blue.space,red.space,cmyk.space,lab.space) " ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CMYK = (0.0, 1.0, 1.0, 0.0)\n", "XYZ = (0.412453, 0.212671, 0.019334)\n", "xyY = (0.6399998137970202, 0.32999978276319014, 0.212671)\n", "Lab = (53.2405879437449, 80.0923082256922, 67.2027510444287)\n", "Luv = (53.2405879437449, 175.0144735628877, 37.75617373935089)\n", "HSV = (0.0, 1.0, 1.0)\n", "RGB = (1.0, 0.0, 0.0)\n", "HEX = #ff0000\n" ] } ], "source": [ "for s in colorspaces:\n", " print(s,'=',red.convert(s))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(78.0294899763116, -46.93394514696031, 56.55175935198822) (78.0294899763116, -46.93394514696031, 56.55175935198822)\n", "(0.45, 0.12, 0.67, 0.05) (0.37500000000000044, 0.0, 0.6250000000000002, 0.1639999999999998)\n" ] } ], "source": [ "c=Color(cmyk.lab,'lab')\n", "print(cmyk.lab,c.lab)\n", "print(cmyk.cmyk,c.cmyk)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "scrolled": false }, "outputs": [], "source": [ "#converters #will produce a nice graph of converters but it's still ugly" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Color ranges" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "[red,~darkorange,~yellow,~lime,~lime,~aquamarine,~dodgerblue,blue]" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "h(color_range(8,'red','blue')) # ranges are generated in by linear interpolation in 'hsv' by default" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "[red,~deeppink,~mediumvioletred,~mediumvioletred,~darkorchid,~darkviolet,~blue,blue]" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "h(color_range(8,'red','blue','xyz')) # but another colorspace can be specified" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Palettes" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
 
 
 
 
 
 
 
 
 
 
 
 
" ], "text/plain": [ "Palette of 12 colors" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Palette(color_range(12,'red','blue','lab')) # Palette can be inited from a color list, \n", "#palettes have a responsive HTML repr with popups (hover the cursor over the bar to see it)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
" ], "text/plain": [ "Palette of 256 colors" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Palettes can also be inited from Matplotlib colormaps\n", "from matplotlib import cm\n", "Palette(cm.nipy_spectral) # discretized to 256 levels by default" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "color contains 144 web colors indexed by name : blue" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "color_lookup contains 138 web colors indexed by hex : olive" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "acadcolors contains 256 Autocad ACI colors indexed by int : [black,red,yellow,lime,cyan,blue,magenta,white,8,gray,red,11,12,13,14,15]" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "pantone contains 907 Pantone colors indexed by name : 1795C" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# several Paletets of standard colors are predefined:\n", "h('color contains',len(color),'web colors indexed by name :',color['blue'])\n", "h('color_lookup contains',len(color_lookup),'web colors indexed by hex :',color_lookup['#808000'])\n", "h('acadcolors contains',len(acadcolors),'Autocad ACI colors indexed by int :',acadcolors[0:16])\n", "h('pantone contains',len(pantone),'Pantone colors indexed by name :',pantone['1795C'])" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
" ], "text/plain": [ "Palette of 907 colors" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pantone #responsive multiline, with popups" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
100C101C102C103C104C105C106C107C108C109C110C111C112C113C114C
115C116C117C118C119C120C121C122C123C124C125C126C127C128C129C
130C131C132C133C134C135C136C137C138C139C140C141C142C143C144C
145C146C147C148C149C150C151C152C153C154C155C156C157C158C159C
160C161C162C163C164C165C166C167C168C169C170C171C172C173C174C
175C176C177C178C179C180C181C182C183C184C185C186C187C188C189C
190C191C192C193C194C196C197C198C199C200C201C202C203C204C205C
206C207C208C209C210C211C212C213C214C215C216C217C218C219C220C
221C222C223C224C225C226C227C228C229C230C231C232C233C234C235C
236C237C238C239C240C241C242C243C244C245C246C247C248C249C250C
251C252C253C254C255C256C257C258C259C260C261C262C263C264C265C
266C267C268C269C270C271C272C273C274C275C276C277C278C279C280C
281C282C283C284C285C286C287C288C289C290C291C292C293C294C295C
296C297C298C299C300C301C302C303C304C305C306C307C308C309C310C
311C312C313C314C315C316C317C318C319C320C321C322C323C324C325C
326C327C328C329C330C331C332C333C334C335C336C337C338C339C340C
341C342C343C344C345C346C347C348C349C350C351C352C353C354C355C
356C357C358C359C360C361C362C363C364C365C366C367C368C369C370C
371C372C373C374C375C376C377C378C379C380C381C382C383C384C385C
386C387C388C389C390C391C392C393C394C395C396C397C398C399C400C
401C402C403C404C405C406C408C409C410C411C412C413C414C415C416C
417C418C419C420C421C422C423C424C425C426C427C428C429C430C431C
432C433C434C435C436C437C438C439C440C441C442C443C444C445C446C
447C448C449C450C451C452C453C454C455C456C457C458C459C460C461C
462C463C464C465C466C467C468C469C470C471C472C473C474C475C476C
477C478C479C480C481C482C483C484C485C486C487C488C489C490C491C
492C494C495C496C497C498C499C500C501C502C503C504C505C506C507C
508C509C510C511C512C513C514C515C516C517C518C519C520C521C522C
523C524C525C526C527C528C529C530C531C532C533C534C535C536C537C
538C539C540C541C542C543C544C545C546C547C548C549C550C551C552C
553C554C555C556C557C558C559C560C561C562C563C564C565C566C567C
569C570C571C572C573C574C575C576C577C578C579C580C581C582C583C
584C585C586C587C600C601C602C603C604C605C606C607C608C609C610C
611C612C613C614C615C616C617C618C619C620C621C622C623C624C625C
626C627C628C629C630C631C632C633C634C635C636C637C638C639C640C
641C642C643C644C645C646C647C648C649C650C651C652C653C654C655C
656C657C658C659C660C661C662C663C664C665C666C667C668C669C670C
671C672C673C674C675C676C677C678C679C680C681C682C683C684C685C
686C687C688C689C690C691C692C693C694C695C696C697C698C699C700C
701C702C703C704C705C706C707C708C709C710C711C712C713C714C715C
716C717C718C719C720C721C722C723C724C725C726C727C728C729C730C
731C732C801C802C803C804C805C806C807C808C809C810C811C812C813C
814C1205C1215C1225C1235C1245C1255C1265C1345C1355C1365C1375C1385C1395C1405C
1485C1495C1505C1525C1535C1545C1555C1565C1575C1585C1595C1605C1615C1625C1635C
1645C1655C1665C1675C1685C1765C1767C1775C1777C1785C1787C1788C1795C1797C1805C
1807C1815C1817C1895C1905C1915C1925C1935C1945C1955C2365C2375C2385C2395C2405C
2415C2425C2562C2563C2567C2572C2573C2577C2582C2583C2587C2592C2593C2597C2602C
2603C2607C2612C2613C2617C2622C2623C2627C2635C2645C2655C2665C2685C2695C2705C
2706C2707C2708C2715C2716C2717C2718C2725C2726C2727C2728C2735C2736C2738C2745C
2746C2747C2748C2755C2756C2757C2758C2765C2766C2767C2768C2905C2915C2925C2935C
2945C2955C2965C2975C2985C2995C3005C3015C3025C3035C3105C3115C3125C3135C3145C
3155C3165C3242C3245C3248C3252C3255C3258C3262C3265C3268C3272C3275C3278C3282C
3285C3288C3292C3295C3298C3302C3305C3308C3375C3385C3395C3405C3415C3425C3435C
3935C3945C3955C3965C3975C3985C3995C4485C4495C4505C4515C4525C4535C4545C4625C
4635C4645C4655C4665C4675C4685C4695C4705C4715C4725C4735C4745C4755C4975C4985C
4995C5005C5015C5025C5035C5115C5125C5135C5145C5155C5165C5175C5185C5195C5205C
5215C5225C5235C5245C5255C5265C5275C5285C5295C5305C5315C5395C5405C5415C5425C
5435C5445C5455C5463C5467C5473C5477C5483C5487C5493C5497C5503C5507C5513C5517C
5523C5527C5535C5545C5555C5565C5575C5585C5595C5605C5615C5625C5635C5645C5655C
5665C5743C5747C5753C5757C5763C5767C5773C5777C5783C5787C5793C5797C5803C5807C
5815C5825C5835C5845C5855C5865C5875C
\n" ], "text/plain": [ "Table(len=61,titles=[],data=[[100C, 101C, 102C, 103C, 104C, 105C, 106C, 107C, 108C, 109C, 110C, 111C, 112C, 113C, 114C], [115C, 116C, 117C, 118C, 119C, 120C, 121C, 122C, 123C, 124C, 125C, 126C, 127C, 128C, 129C], [130C, 131C, 132C, 133C, 134C, 135C, 136C, 137C, 138C, 139C, 140C, 141C, 142C, 143C, 144C], [145C, 146C, 147C, 148C, 149C, 150C, 151C, 152C, 153C, 154C, 155C, 156C, 157C, 158C, 159C], [160C, 161C, 162C, 163C, 164C, 165C, 166C, 167C, 168C, 169C, 170C, 171C, 172C, 173C, 174C]])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ColorTable(pantone,lambda c:int(c.name[:-1]),15) #another representation is available" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "def ColorCoords(colors,mode,cmode='rgb'):\n", " \"\"\"coordinates of a color map\"\"\"\n", " x,y,z,c,l=[],[],[],[],[]\n", " for id in colors:\n", " p=colors[id]\n", " v=p.convert(mode)\n", " x.append(v[1])\n", " y.append(v[2])\n", " z.append(v[0])\n", " c.append(p.convert(cmode))\n", " l.append(p.name)\n", " return x,y,z,c,l\n", "\n", "def ColorCube(colors,mode='xyY',s=50):\n", " \"\"\"draw an interactive color cube of colors\"\"\"\n", " x,y,z,c,l=ColorCoords(colors,mode,'hex')\n", " fig = plt.figure()\n", " from mpl_toolkits.mplot3d import Axes3D\n", " ax = fig.add_subplot(111,projection='3d')\n", " ax.scatter(x,y,z,c=c,s=s,lw=0,depthshade=False)\n", " ax.set_xlabel(mode[1])\n", " ax.set_ylabel(mode[2])\n", " ax.set_zlabel(mode[0])" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " event.shiftKey = false;\n", " // Send a \"J\" for go to next cell\n", " event.which = 74;\n", " event.keyCode = 74;\n", " manager.command_mode();\n", " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fig = plt.figure()\n", "from mpl_toolkits.mplot3d import Axes3D\n", "ax = Axes3D(fig)\n", "ax.scatter(x,y,z,c=c,s=100,lw=0,depthshade=False)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'mpld3'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mmpld3\u001b[0m \u001b[1;31m# looks much cooler ! seen on https://mpld3.github.io/examples/scatter_tooltip.html\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mmpld3\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0menable_notebook\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mfig\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfigure\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0max\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfig\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgca\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprojection\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'3d'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mscatter\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0max\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mscatter\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mlw\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mz\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'mpld3'" ] } ], "source": [ "import mpld3 # looks much cooler ! seen on https://mpld3.github.io/examples/scatter_tooltip.html \n", "mpld3.enable_notebook()\n", "fig = plt.figure()\n", "ax = fig.gca(projection='3d')\n", "scatter = ax.scatter(x,y,c=c,lw=10,s=z)\n", "#tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=l)\n", "#mpld3.plugins.connect(fig, tooltip)\n", "mpld3.display()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.1" } }, "nbformat": 4, "nbformat_minor": 1 }