How to change tick labels for plotly.graph_objects.Figure? - python

I have a figure that I create with:
import plotly.graph_objects.Figure as go
go.Figure(data)
Now, I want to change the tick labels using a custom defined lambda function, just like I would do it for a standard matplotlib figure like this:
from matplotlib.ticker import FuncFormatter
formatter = FuncFormatter(*some lambda function*)
ax.set_xticklabels(ax.get_xticks(), rotation = 90)
ax.xaxis.set_major_formatter(formatter)
(In my particular use case, I have a matplotlib figure where my tick labels are integers that represent hours after a given start date, and I want to be able to convert them do strings in date format with my custom made lambda function)
How to do this? I have been googling for this functionallity for a long time now and found nothing that helps, while I really can't believe that there wouldn't be a simple, elegant solution for this.

I am afraid it is a little difficult to directly apply lambda function to format plotly axis since the tick label formatting rule uses d3 formatting mini-languages.
I think one way is to create the ticks by yourself and mapping that via Tickmode - Array.

Related

Matplotlib tweek label algorithm?

Matplotlib has some pretty sophisticated code figuring out how to show labels, but sometimes it cramps its labels more than looks good on presentations. Is there any way to tweek it?
For example, suppose we're plotting something against date:
figure = plt.figure(figsize=(8,1))
ax = plt.gca()
ax.set_xlim(xmin=np.datetime64('2010'), xmax=np.datetime64('2020-04-01'))
We get an x-axis like this:
But supposing we want it to show more spaced years, like this:
We can kludge it in any given case by editing the labels 'mechanically'. E.g.:
ax.set_xticks([tick for i, tick in enumerate(ax.get_xticks()) if i%2==0]) # Every other year.
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%Y"))
But that's fragile, and it breaks whenever the x limits change.
Is there any way to force more spacing in the tick setup algorithm?
Oh! Found the matplotlib source code and it led me to AutoDateLocator:
ax.xaxis.set_major_locator(matplotlib.dates.AutoDateLocator(maxticks=8))
The corresponding locator for non-dates is MaxNLocator .

In a pairplot, how can I not show confidence intervals but display grid lines instead? [duplicate]

I'm plotting two data series with Pandas with seaborn imported. Ideally I would like the horizontal grid lines shared between both the left and the right y-axis, but I'm under the impression that this is hard to do.
As a compromise I would like to remove the grid lines all together. The following code however produces the horizontal gridlines for the secondary y-axis.
import pandas as pd
import numpy as np
import seaborn as sns
data = pd.DataFrame(np.cumsum(np.random.normal(size=(100,2)),axis=0),columns=['A','B'])
data.plot(secondary_y=['B'],grid=False)
You can take the Axes object out after plotting and perform .grid(False) on both axes.
# Gets the axes object out after plotting
ax = data.plot(...)
# Turns off grid on the left Axis.
ax.grid(False)
# Turns off grid on the secondary (right) Axis.
ax.right_ax.grid(False)
sns.set_style("whitegrid", {'axes.grid' : False})
Note that the style can be whichever valid one that you choose.
For a nice article on this, refer to this site.
The problem is with using the default pandas formatting (or whatever formatting you chose). Not sure how things work behind the scenes, but these parameters are trumping the formatting that you pass as in the plot function. You can see a list of them here in the mpl_style dictionary
In order to get around it, you can do this:
import pandas as pd
pd.options.display.mpl_style = 'default'
new_style = {'grid': False}
matplotlib.rc('axes', **new_style)
data = pd.DataFrame(np.cumsum(np.random.normal(size=(100,2)),axis=0),columns=['A','B'])
data.plot(secondary_y=['B'])
This feels like buggy behavior in Pandas, with not all of the keyword arguments getting passed to both Axes. But if you want to have the grid off by default in seaborn, you just need to call sns.set_style("dark"). You can also use sns.axes_style in a with statement if you only want to change the default for one figure.
You can just set:
sns.set_style("ticks")
It goes back to normal.

python matplotlib - retrieve xaxis_major_locator as time value

I am plotting a timeserie with matplotlib (timeserie looks like the following):
Part of the code that i use sets major locator for each day at 0AM:
fig, ax = plt.subplots(figsize=(20, 3))
mpf.candlestick_ohlc(ax,quotes, width=0.01)
ax.xaxis_date()
ax.xaxis.set_major_locator(mpl.dates.DayLocator(interval=1) )
I would like to plot a darker background on the chart for each day between 16pm and 8am, and planning to use axvspan for that task. Considering that axvspan takes as argument axvspan(xmin, xmax) I was wondering if it would be possible to retrieve the xaxis_major_locator as a x value in order to pass it to axvspan as axvspan(xmin=major_locator-3600s, xmax=major_locator+3600s)
Edit: I found that function in the docs: http://matplotlib.org/2.0.0rc2/api/ticker_api.html#matplotlib.ticker.Locator
If anyone knows how to returns a list of ticker location from the Xaxis_major with it let me know. Thanks.
Edit2: if i use print(ax.xaxis.get_major_locator()) i receive as a return <matplotlib.dates.DayLocator object at 0x7f70f3b34090> How do i extarct a list of tick location from that?
ok found it...
majors=ax.xaxis.get_majorticklocs()

Subscripting text in matplotlib labels

this is my first question and I am a noob at python. So probably more to follow...
I would like to create a figure with matplotlib. In the labels, I would like to include a chemical formula, which involves subscripts (I think the same would work for superscripts...).
Anyway, I have no idea, how the label would have to look like.
import numpy as nu
import pylab as plt
x = nu.array([1,2,3,4])
y = nu.array([1,2,3,4])
plt.plot(x,y, label='H2O')
plt.legend(loc=1)
plt.show()
Ok, this gives me a plot with the label "H2O". How can I subscript the "2" in the label, as is common for chemical formulae?
I searched the web, but I didn't find anything useful yet.
I figured that I could use
from matplotlib import rc
rc['text', usetex=True]
but I don't want to use it (I know how to use LaTeX, but I don't want here).
Another option is:
label='H$_2$O'
but this changes the font (math).
There MUST be a way, how does subscripting in matplotlib-legends work?
Thanks a lot!
Try to change this line
plt.plot(x,y, label='H2O')
for this:
plt.plot(x,y, label='$H_2O$')
It shows with the font math.
Or also you can use the unicode character for that: â‚‚ (0xE2 / â‚‚)
plt.plot(x,y, label=u'Hâ‚‚O')
or instead:
plt.plot(x,y, label=u"H\u2082O")
Please, note that unicode strings are noted as u"" instead than "".

Show only the n'th ticklabel in a pandas boxplot

I am new to pandas and matplotlib, but not to Python. I have two questions; a primary and a secondary one.
Primary:
I have a pandas boxplot with FICO score on the x-axis and interest rate on the y-axis.
My x-axis is all messed up since the FICO scores are overwriting each other.
I'd like to show only every 4th or 5th ticklabel on the x-axis for a couple of reasons:
in general it's less chart-junky
in this case it will allow the labels to actually be read.
My code snippet is as follows:
plt.figure()
loansmin = pd.read_csv('../datasets/loanf.csv')
p = loansmin.boxplot('Interest.Rate','FICO.Score')
I saved the return value in p as I thought I might need to manipulate the plot further which I do now.
Secondary:
How do I access the plot, subplot, axes objects from pandas boxplot.
p above is an matplotlib.axes.AxesSubplot object.
help(matplotlib.axes.AxesSubplot) gives a message saying:
'AttributeError: 'module' object has no attribute 'AxesSubplot'
dir(matplotlib.axes) lists Axes, Subplot and Subplotbase as in that namespace but no AxesSubplot. How do I understand this returned object better?
As I explored further I found that one could explore the returned object p via dir().
Doing this I found a long list of useful methods, amongst which was set_xticklabels.
Doing help(p.set_xticklabels) gave some cryptic, but still useful, help - essentially suggesting passing in a list of strings for ticklabels.
I then tried doing the following - adding set_xticklabels to the end of the last line in the above code effectively chaining the invocations.
plt.figure()
loansmin = pd.read_csv('../datasets/loanf.csv')
p=loansmin.boxplot('Interest.Rate','FICO.Score').set_xticklabels(['650','','','','','700'])
This gave the desired result. I suspect there's a better way as in the way matplotlib does it which allows you to show every n'th label. But for immediate use this works, and also allows setting labels where they are not periodic for whatever reason, if you need that.
As usual, writing out the question explicitly helped me find the answer. And if anyone can help me get to the underlying matplotlib object that is still an open question.
AxesSubplot (I think) is just another way to get at the Axes in matplotlib. set_xticklabels() is part of the matplotlib object oriented interface (on axes). So, if you were using something like pylab, you might use xticks(ticks, labels), but instead here you have to separate it into different calls ax.set_xticks(ticks), ax.set_xticklabels(labels). (where ax is an Axes object).
Let's say you only want to set ticks at 650 and 700. You could do the following:
ticks = labels = [650, 700]
plt.figure()
loansmin = pd.read_csv('../datasets/loanf.csv')
p=loansmin.boxplot('Interest.Rate','FICO.Score')
p.set_xticks(ticks)
p.set_xticklabels(labels)
Similarly, you can use set_xlim and set_ylim to do the equivalent of xlim() and ylim() in plt.

Categories