Adding errorbars in ggplot Python - python

Can't find a way of adding errorbars to a Python ggplot plot. The following issue has been neglected for over a year. Nothing in the docs.

I had this same problem and found no solution. However I did find a way around it. You can use matplotlib in the style of ggplot. From there it's much easier to use error bars. I've attached an example of some code I used.
plt.style.use('ggplot')
This is an extract of one of my codes
df2.gLongCrFiltered['mean'].plot(kind='bar', yerr=df2.gLongCrFiltered['std'])
which returned this

Related

Customizing hvplot box

I am trying to get deep into hvplot to see if I can find a good alternative to plotly, but I am baffled by the documentation.
I am trying to plot boxplots with jittered points, and a basic tutorial can be found here.
The part of the example I am trying to customize is this:
import hvplot.pandas
from bokeh.sampledata.sprint import sprint as df
boxplot = df.hvplot.box(y='Time', by='Medal', height=400, width=400, legend=False)
boxplot
However, I want to style my plot, for example by removing the borders of each box, changing opacity, box width, and so on.
I tried to search that in the hvplot page, but the documentation seem to be limited to some basic examples and not much more.
On stackoverflow I managed to find some pointers to how to style plots but I tried and failed to find out how to do the above mentioned things.
Is there a more detailed documentation somewhere or do I just need to get deeper into holoviews?
Update:
I found in the customization guide of HvPlot that one can call hvplot.help('box'). That contains all the possibile options. The fact that box_color doesn't work still eludes me (box_fill_color works) but at least that's a more detailed explanation of how it works.
Leaving it here just in case someone else needs it.

How to force AxesSubplot to use integer tick labels

I'm struggling to correctly apply Integerlabels to the AxesSubplot Object. Somehow all the other popular solutions don't work. I presume this has to do with the nature of how Pandas plots vs how Matplotlib natively plots a dataframe.
When looking at the docs, there is a mention about preventing resolution adjustments by using the optional parameter x_compat like so:
df['A'].plot(x_compat=True)
However this only throws me an error and I also can't find this parameter in the pandas 1.1.2 docs.
This is the code that produces the plot:
def count_id(id_val):
plot = df[df['ID']==id_val]['TRANSCRIPTION_STRING'].value_counts().plot(kind='bar')
plot.set_xticklabels(plot.get_xticklabels(), rotation=40, ha ='right')
print(type(plot))
I have found this answer and it wasn't helpful for the following reasons:
It uses .gca() which throws an error for my case.
It's specifically using matplotlib instead of pandas.plot, which is the same under the hood, but hardly has very different documentation and isn't obvious to work for my case. More experienced users might differ.
It was a good suggestion and I see the similarities, but I very much tried to find it helpful before asking and it just wasn't helpful.
You can use:
from matplotlib.ticker import MaxNLocator
plot.yaxis.set_major_locator(MaxNLocator(integer=True))
MaxNLocator is the default 'locator' for the ticks. You can also force some fixed multiples, e.g. set_major_locator(MultipleLocater(1)).
PS: The usual variable name for the return value of pandas plot functions is ax. This makes it clearer that the standard matplotlib functions can be used to customize the plots.
ax = df[df['ID']==id_val]['TRANSCRIPTION_STRING'].value_counts().plot(kind='bar')
ax.set_xticklabels(plot.get_xticklabels(), rotation=40, ha ='right')
ax.yaxis.set_major_locator(MaxNLocator(integer=True))

Boxplot with hover tool tip in Python

I am trying to achieve a Boxplot with hover tool tip in Python
I have tried bokeh.charts and bkcharts which are deprecated and no module is found for the same.
I even tried mpld3, but all I got was Scatter plot with the tooltip,
I even tried the bokeh box plot https://docs.bokeh.org/en/latest/docs/gallery/boxplot.html from here but it is very complex and making use of unnecessary groupby column.
If anyone has boxplot with hover tool tip in python than please help.
Similar functionality can be achieved very easily in R so why not in Python???
did you try plotly?
This example may help.
You may have to add JS code so I'm aware this may not be what you need. Switch to tab "Python & R" and look at the Python code. There are comments there.

Python- Connecting Lines with plot_date in Matplotlib

I am using matplotlib to graph out some data in which takes time over a time, therefore I have to use plot_date in order to plot my lines. But for some reason Plot_Date and Plot have completely different formatting as far as connecting lines.
Here is what It looks like when using plot(x,y)
Using plot(x,y,'bo') or plot_date(x,y,'bo')
Plot_date(x,y) looks like that ^^ too.
and using plot_date(x,y,'bo-')
How do I make it so that the result of plot_date looks like the first picture? I have looked all over the Matplotlib website and couldn't find anything.
Thanks in advance
Upon further investigation I found that in order to display a solid line without dots, I needed to use the line style 'b-', making the code plot_date(x,y,'b-').
If you want to have the markers connected with the lines, you can also use
plt.plot_date(y,x,linestyle='solid')
So the complete code for the beginners would be:
from matplotlib import pyplot as plt
plt.plot_date(y,x,linestyle='solid')
plt.show()
Do not forget you need to have matplotlib installed first. For that you need to open command line and write:
pip install matplotlib

understanding (and finding) matplotlib source code

Here it appears that matplotlib's specgram returns 4 variables including the last which is a plot:
http://matplotlib.org/examples/pylab_examples/specgram_demo.html
But here it seems there is only 3 variables returned in the tuple:
https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/mlab.py#L478
Where is the missing code to generate the specgram plot? Perhaps I am just confused on the difference between pylab and matplotlib. Either way, I can't find the source.
You're confusing the function that computes the data to be plotted with the function that plots the data.
mlab.specgram just computes the data, while the axes method specgram plots it.
Have a look at: https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/axes/_axes.py#L5786
ipython is very useful for things like this. method_name? will display the relevant documentation and the location of the source file, while method_name?? will display the relevant code, as well.
Understanding where the source for a matplotlib function is can be a bit confusing. Basically, anything in matplotlib.pyplot is auto-generated. Essentially all of the plotting methods are actually methods of the Axes object.
Hopefully that gets you started. If no one else gives a better answer, I'll elaborate more in a bit, when I have more time.

Categories