Matplotlib: How to plot this? Is it possible? - python

this is a draft of the output i would like, made by me on draw.io
Basically my conditions are:
(1) A bar may not start on the first value of the axis Y
(2) The bars are calculated for a interval, so if possible drawing the bars 'inside' that interval
(3) A bar may not have values for a interval, so it's not drawn
Finally, i believe 2 and 3 are possible, but 1 is it? And shall i approach it as a histogram, a bar chart or a box plot? Or maybe a mix? (i am pretty newbie on matplolib)
Any question ask, and thanks.

You can manually draw rectangles with matplotlib, so it's not a problem to support (1), (2) and (3) at the same time :)
https://matplotlib.org/api/_as_gen/matplotlib.patches.Rectangle.html

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])

Why is part of my contour plot showing white?

I am using Python's matplotlib.pyplot.contourf to create a contour plot of my data with a color bar. I have done this successfully countless times, even with other layers of the same variable. However, when the values get small (on the order of 1E-12), parts of the contour show up white. The white color does not show up in the color bar either. Does anyone know what causes this and how to fix this? The faulty contour is attached below.
a1 = plt.contourf(np.linspace(1,24,24),np.linspace(1,20,20),np.transpose(data[:,:,15]))
plt.colorbar(a1)
plt.show()
tl;dr
Given the new information, matplotlib couldn't set the right number of levels (see parameters in the documentation) for your data leaving data unplotted. To fix that you need to tell matplotlib to extend the limits with either plt.contourf(..., extend="max") or plt.contourf(..., extend="both")
Extensive answer
There are a few reasons why contourf() is showing white zones with a colormap that doesn't include white.
NaN values
NaN values are never plotted.
Masked data
If you mask data before plotting, it won't appear in the plot. But you should know if you masked your data.
Although, you may have unnoticed mask your data if you use something like Tick locator = LogLocator().
Matplotlib couldn't set the right levels for your data
Sometimes matplotlib doesn't set the right levels, leaving some of your data without plotting.
To fix that you can user plt.contourf(..., extend=EXTENDS) where EXTENDS can be "neither", "both", "min", "max"
Coarse grid
contourf plots whitespace over finite data. Past answers do not correct
One remark, white section in the plot can also occur if the X and Y vectors data points are not equally spaced. In that case best to use function tricontourf().
I was facing the same problem recently, when there was data available even higher/lower than the levels I have set. So, the plt.contourf fills the contours exclusively given by you, and it neglects any other higher or lower values present in your data.
I solved this by adding a key word argument extend="both", which for your case would be something like this:
a1 = plt.contourf(np.linspace(1,24,24),np.linspace(1,20,20),np.transpose(data[:,:,15]), extend="both")
or in general form:
a1 = plt.contourf(x,y,variable[:,:,15],extend="both")
By doing this, you're instructing the module to plot the higher(/lower) values according to the highest(/lowest) filled contour.
If you want only to extend in the lower or higher range, you can change the keyword argument to
extend="min" or extend ="max"

In Plotly 3D Scatterplot, is there a way to freeze rotation of one axis?

I am making a 3-D Scatterplot with plotly in Python 3 and the Z-axis represents time. I would like to freeze that so that, when the using clicks around to rotate the plot, it spins on that axis, but that axis stays up and down. I'm not sure if it's possible, but that would be a great feature.
As it is, the oldest points are at the top (this is a genetic tree) and the newest are at the bottom. However, it's very easy to get this turned when clicking around the plot to rotate and then the time axis is going left-to-right or diagonal or something and it's a bit disorienting, especially for people who are not used to looking at complex visuals like this (i.e. my intended audience).
A first draft example: https://plot.ly/~seth127/6
Any help is greatly appreciated!
Thanks,
Seth

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)?

Python Turtle: Drawing pie chart with one segment seperated

There is this example in my workbook that gives me a list of information and asks to draw a pie chart representing this information. The ONLY module that you can import is turtle. I can draw the pie chart like in figure 1 but I do not know how to separate one segment from all the rest like in figure 2.
Figure 1: http://msenux.redwoods.edu/math/python/graphics/pie2.png
Figure 2: http://www.aecbytes.com/illustrations/viewpoint/2013/issue_67-images/fig11.png
(I realise that figure 2 was created in word)
All I need to know is how to separate just one of the segments with it still being in-line with the whole circle.
Any help would be great
Depends on your design. If the center of the diagram is at the origin, you can add an offset to the coordinates of the section before it's drawn. If, for example, the segment were one quarter of the piechart from 0 to 90 degrees on the unit circle, you could offset the segment along the vector that bisects the section (1,1).
But, if you want some decent feedback, post minimal working code so we can go over it.
Also, if the image you linked is yours, you are waaayyy beyond color-blind.

Categories

Resources