Matplotlib don't show all ticks/labels with bar chart? - python

When plotting a line with many points, matplotlib automatically hides some x-axis ticks/labels to make sure all labels can fit.
If I want to plot a bar chat with many points, is there anyway to make matplotlib think of the x-axis in the same as with a line plot? Right now it's trying to squeeze in all the labels.

I would suggest specifiying the list of xtick labels :
plt.xticks([80,150,300]) ### In this case these are numbers, change them to your values

Related

Change legend number range in Altair plot

I am trying to plot time series data in a kind of "climate stripes plot" using the package Altair.
The problem is that I do not know how to change the range in the legend to standardise all my plots with the same colour range and numbers in the legend. At the moment, each time I plot something the legend adapts to the range of the data.
I think the problem is with the "domain" property, maybe is not in the correct place ?
Thank you for your help :)
This is the code for the plot :
chart=alt.Chart(source).mark_rect().encode(
x=('day:O'),
y='subasins:N',
color=alt.Color('90%:Q',legend=alt.Legend(title='CH4'), bin=alt.Bin(maxbins=20),
scale=alt.Scale(scheme='blueorange'),domain=[1830,2000])
).properties(width=100).facet(column=alt.Column('month'))
chart.show()
Plots that I get now with different scales in the legend
You're using the right approach with domain, it just needs to be put inside alt.Scale:
scale=alt.Scale(scheme='blueorange', domain=[1830, 2000])
When you're using a bin transform, one way to ensure the scale is consistent is to specify the bin extent:
bin=alt.Bin(maxbins=20, extent=[1830, 2000])

shared_xaxis with plotly, but can I still display the axis on each plot?

if you share the x_axis between 2 plots, they will get aligned, as expected.
however, the axis is displayed only once, on a single plot and not on both anymore.
Is it possible to display the axis on each plot?
If you're using Plotly Express, there is no way to do this at the moment. If you're using the underlying make_subplots method, however, this is the default behaviour: https://plot.ly/python/subplots/

Weird artefacts in a matplotlib bar plot

I am using the pandas plot facilities, to plot a bar plot:
spy_price_data.iloc[40:,1].plot(kind='bar')
The bar data is plotted correctly, but the figure contains weird artefacts in the form of additional horizontal bars below the actual figure:
What could be the problem here?
The 'weird artefacts' are your ticklabels. You can even (almost) read them at the end:
The last value seems to say something like 2018-08-19 20:00:00.
To make the plot more readable, take a look at the answer from ImportanceOfBeingErnest to the question Matplotlib: How to increase space between tickmarks (or reduce number of tickmarks)?

Overlaying subplot on seaborn factorplot

I'm creating a plot with factorplot and then trying to add a subplot on top of each box. How can I get the x-axis locations of each individual box in the factor plot to put another line on top?
Maybe there's a way to get all the x-axis values of each box plot on the axes?
Here's my basic factor plot:
I want to add 1 subplot (the circle) in the middle of each box plot. However, I cannot figure out how to get the x-value of each box to properly space the points.
I see a lot of code for positions and offsets in the seaborn source that lays these out. However, I'm wondering if there is a more straight-forward method to get this information or at least approximate it.
As per #mwaskom's comments, you can use sns.stripplot() (and now also sns.swarmplot()) to include your data points with a data summary plot such as a box or violinplot.

How can I draw inline line labels in matplotlib?

I have the following graph, consisting of several lines:
Now, I would like to label all the lines in the plot. However, using legend() crams all the labels together in a box, which makes the plot somewhat difficult to interpret. What I'd like to to instead is to use inline labels. My ideal output would use labels like the following matplotlib contour plot, but with text labels for lines instead of numbers:
I haven't been able to find out how to do this in the matplotlib documentation. Is there a way to achieve this? If not, what other software could I use to generate this type of plot?
May I suggest another solution to your problem. Since in your case legend overlaps the charts you might just want to move the legend outside of the plot.
Method do move legend outside of plot is described here:
Moving matplotlib legend outside of the axis makes it cutoff by the figure box

Categories

Resources