Jupyter same color displaying differently in different notebooks - colors

Below, you can see two barplots I've superimposed in order to better see the contrast. Both barplots were generated using identical code (different data, but this is unimportant) in two simultaneously opened Jupyter Notebooks. The code for both calls 'C3' as the color, but as you can clearly see, this displays differently in both notebooks. This is the case with both the inline and the plt.savefig versions of the images. I have also tried other colors such as 'C0' and get similar slight differences.
Any ideas what might be causing this and how I might remedy it? Thanking you all in advance.

Per the color documentation the CN syntax is used to index into the current color cycler. Since RGB codes work and CN doesn't, it leads me to the conclusion that somehow the different notebooks are using different color cycles. Try the following snippet in each notebook and see if it gives you a different answer:
import matplotlib as mpl
print(mpl.rcParams["axes.prop_cycle"])
I think it's likely that somewhere in one of the notebooks there's been a call to set_prop_cycle() that has somehow changed the cycler that is being used. Also check out this post for more info on selecting the colors via the cycler.
The short answer would be to avoid the 'CN' notation.

Related

Graphviz labels appearing if you hover over them?

I am currently using graphviz through Python 3.x to visualize a digraph with info on the nodes and edges as graphviz-labels. This gets crowded, quite fast.
Therefore, I was wondering whether there is a possibility to have the label info appear when you hover your cursor over the corresponding node or edge?
With kind regards,
Tobi
Edit: I have given up on that issue for now. I looked into the python-graphviz-package, but to me it seemed as if the tooltip-parameter does not get passed on properly.
You did not mention your desired output format. If you are producing SVG output, read the attribute documentation about tooltip.
somenode [label="small" tooltip="thanks for asking, over the last few years..."]

Python Matplotlib string-formatting in plots with bold and Italic font

I have a little problem with bold and italic fonts in a plot at the same time.
I tried:
plt.text(0.5,0.5,r'\it{italic} + \bf{bold}' = \it{\bf{both}}')
this gives: italic and bold = both
and with:
plt.text(0.5,0.5,r'\it{italic} + \bf{bold}' = \bf{\it{both}}')
I get: italic and bold = both
But what I want to get is both
I also tried to use the latex version with \textbf{...} and \textit{...} and the commands \mathit{...} and \mathbf{...}.
They work as they should but not if they are used combined. The combination of different "technologies" like \mathbf{\textbf{...}} does not help. It is alway only the last style that stays.
I saw a few different questions on that topic but no correct answer, so I hope that asking espacially for this contextless case helps to find an answer. I am looking for a generall solution, not for a workaround for a special case, e.g. using a special font that already looks bold.
I have found the answer. Yoi have to use a different font. The work around I mentioned is the answer. If you use:
from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
you will get the desired result. The default font in matplotlib seems to not support the bold and italic edits at the same time. Maybe this helps in many other situations you will have with other edits in case you are looking for different combinations.

Italicize and color legend manually for different graphs using same labels and colors

I am trying to create multiple graphs that share the same legend.
I have found many ways to combine multiple graphs and it seems that ggarrange has the ability to create one shared legend for all that is supposed to be unique.
However I am having some problems when graphing since a few of the graphs do not have the same phyla (what defines the legend colors) present, but I would like them all to be the same colors throughout all of the graphs so the combined legend will have the correct colors.
For just one graph I would assign a color to the label manually like below
labs<-c("Arthropoda"="#FF66CC"
,"Cercozoa"="#FF6000")
and plot with the addition of scale_fill_manual(values=labs) and this seems to work
then I modified it so I could have portions of them italicized.
labsPhylum <-c('expression(paste(italic("Arthropoda")))'="#CC0000"
,'expression(paste(italic("Cercozoa")))'= "#FF6000"
,'expression (paste("unknown", ~italic("Eukaryota")))'= "#990000")`
However when I create a plot using ggplot and scale_color_manual() using the labsPhylum that I think should be italicized and colored I plot an empty graph with this warning so there is something vital I am not understanding here.
ggplot(data=sigtab_dil, aes(x=Species, y=log2FoldChange, color=Phylum))+
geom_point(size=2) +
scale_color_manual(values=labsPhylum)
Warning message:
Removed 9 rows containing missing values (geom_point).
Could someone please help me figure out where I am going wrong?
Thank you
Answered my own question
I realized I had to make separate vectors for breaks, labels, and values rather than combining them.
In short
colsPhylum <-c("Arthropoda"="#CC0000"
,"Cercozoa"= "#FF6000"
,"Chlorophyta"= "#CC9900"
labsPhylum <-c(expression(paste(italic("Arthropoda")))
,expression(paste(italic("Cercozoa")))
,expression(paste(italic("Chlorophyta ")))
breaksPhylum <-c("Arthropoda", "Cercozoa","Chlorophyta", "Choanozoa"
,"Ciliophora"
,"Cryptista"

Holoviews: separate figures with same coloring and scaling

Let's say that I have two Raster objects (or any other Holoviews object really). I can easily visualize one with appropriate color scaling, and I can do a layout to get both figures with the same scaling and coloring. What if I want to do two figures (e.g. because I need them on different pages), but with the same coloring and scaling so that the figures are comparable.
If there's no way to do this automatically, is there any way to access the relevant settings and then feed them manually to the second figure?
If you're using a notebook: The %opts line magic : IPython specific syntax applied globally [string format]http://holoviews.org/user_guide/Customizing_Plots.html and I think hv.opts works globally in script.
For both backends, you can do hv.renderer('bokeh').get_plot(your_element_variable).state (or replace bokeh with matplotlib) and get the original bokeh/matplotlib items.
Then you can use matplotlib's plt.getp() or bokeh's attribute calling (as I've done here https://github.com/ahuang11/holoext/blob/master/holoext/xbokeh.py#L501-L508) to get the base item's color/font/labels/etc.

PIL -> PyGame image conversion: Partial data loss

I'm using the C-based screenshot concept from JHolta's answer in Take a screenshot via a python script. [Linux] in order to generate screenshots I'd like to display in PyGame. With some minor tweaks (prepending extern "C" to the functions and importing Xutil instead of Xlib) the provided code works amazingly well. In short, it uses Image.frombuffer on a byte array returned by the C library. With show(), the image and anything about it I manipulate is displayed by ImageMagick.
However, if I convert it to Python 3's PyGame as per PIL and pygame.image, I only get a black surface. It's not a straightforward issue, though: If I draw onto the image before converting it into a PyGame image (like in the OP of the latter link), that does show on a black background when blitting the result. Furthermore, printing the byte objects from PILImage.tobytes and pygame.image.tostring shows they both contain data and their len is identical.
What am I doing wrong here? I'll gladly provide code if necessary, but I think it's more of a conceptual issue and I didn't really change the snippets from these answers a lot.
(Similar issue in Python 2, by the way, but there PyGame uses str instead of byte for tostring / fromstring and printing the tostring appears to yield an empty string.)
It turns out that a buggy trigger caused the screenshoot to be taken again while the fullscreen window displaying it was opening. I suppose there are a few milliseconds of blackness or of an undefined state (in the context of the screenshot function) at that moment, and the library is fast enough to catch that.
I'm not sure if this should stay up because it's basically a reminder to check for things that a human can't perceive. Feel free to delete if it's not appropriate.

Resources