Pandas & Matplot -> Independent marker out of scale [duplicate] - python

This question already has answers here:
pyplot scatter plot marker size
(6 answers)
Closed 3 years ago.
I have a scatter chart where i the X-axis is the latitude, the Y-axis is the longitude. Each dot represents a restaurant. The marker size should represent the gross income of that restaurant.
In some areas these values vary wildly, in order of about 100x times, so these guys (rich ones) completely "hide" small nearby restaurants...
So I thought of using a log scale on the marker size... here is the code:
groups.plot.scatter(x='lon', y='lat', s=groups.weight.apply(lambda x: math.log(x)))
plt.plot(sLon, sLat, marker='o', color='red', markersize=math.log(aux.__len__()))
The thing is: i know for a fact that aux.__len__() equals several of the weights on the groups. Here is a image:
The red dot should be very close in size to the ones on its right...
so my question is: Why is the plot from the second command not scaled as the rest?

Its different because you are using scatter and plot which use different sizes. The markersize of plot scales linearly and is more sensitive than the s for a scatter which scales with the sqrt.
See this link for a similar discussion:

Related

Fill a plot with color from a y upwards to infinity [duplicate]

This question already has answers here:
How to use matplotlib fill_between for default ylim
(1 answer)
How to highlight specific x-value ranges
(2 answers)
Closed 2 months ago.
I am trying to indicate a "dangerous" zone in my sns boxplot.
So far this is what i came up with:
plt.fill_between([0, 6], [danger, danger], danger * 1.2, color='salmon', alpha=0.5)
But since I use sns boxplot, the x values are totally messed up and i had to play with it to get the following result:
I cant really increase the polygon size as the plot grows with it (understandingly).
Ideally i would have wanted to simply paint everything above this threshold as red like so:

Reduce spacing between bars in seaborn hist plot [duplicate]

This question already has an answer here:
python matplot.hist - remove gaps between bars
(1 answer)
Closed 4 months ago.
I am plotting some data with
g = seaborn.displot(data, x=var, stat='probability')
# Set appropriate ticks
plt.xticks(np.arange(0, int(data[var].max() + 1), 1.0))
plt.show()
which is giving me the desired plot:
But I would like to compress the axis to remove the unnecessary space and I'm struggling to work out how.
You can increase the margins that determine the space added at both sides of the data limit to get the view limit. Default is 0.05.
plt.margins(x=0.5)
As pointed out by JohanC in the comment below: if your data are discrete values you may specify discrete=True or cast your data to strings to plot them as categories (in the latter case you don't need to set the ticks manually).
sns.displot(data[var].astype(str), stat='probability')

How to give two different colors in one scatter plot in Matplotlib based on values of y axis? [duplicate]

This question already has answers here:
How to change outliers to some other colors in a scatter plot
(2 answers)
Plot: color all larger than different color
(1 answer)
Scatter plot with same color for values below a threshold
(1 answer)
How to color scatterplot in matplotlib based on the values of y axis
(1 answer)
Closed 1 year ago.
So I want to plot a scatter plot with one dataset and have two different colors based on the condition that if a point is above a certain y-axis value, it is colored red (let's say) and below is colored differently.

How do I make the markersize in pyplot scatter not depend on the scale of the graph? [duplicate]

This question already has answers here:
How to plot a circle for each point scatter plot while each has particular radius size
(1 answer)
pyplot scatter plot marker size
(6 answers)
Scale matplotlib.pyplot.Axes.scatter markersize by x-scale
(1 answer)
Closed 3 years ago.
I'm making a simulation, which I am trying to display with pyplot. In the simulation, there are circles moving around, and things happen when they overlap. When I try to show this with pyplot, the markers do not appear the right size.
I have tried altering the marker size, but it did not fix the problem. After some testing, I realized that the marker size was independent of the scale of the axes.
This is the code I used for testing
import matplotlib.pyplot as plt
plt.xlim(-1, 1)
plt.ylim(-1, 1)
markersize = 10 # this is here so that it can be changed easily
plt.text (-0.9,-0.9,str(markersize))
plt. scatter([0],[0],[markersize], marker = "o")
plt.show()
The markersize was changed, and the plot was shown
I found out that not only was the size of the marker not related to the scales but when I zoomed in or out, the size of the marker on my screen stayed the same, even if it took more or less on the scale of the axes. To test this out, run the above code with markersize 10000. Put the window on full screen, and depending on your monitor, the marker will probably go from -0.25 to 0.25. Now the same thing, when you make the window smaller, you can get the marker to go from -0.5 to 0.5.
Zoomed out:
Zoomed in:
I am ultimately looking for a way to make the markersize dependent on the scales of the axes, so in that example, the marker would be from -0.25 to 0.25 no matter how you zoomed in or out. This way, when in the code two circles overlap, they will overlap in the simulation too.

Creating ticks in a matplotlib heatmap based on range of values [duplicate]

This question already has answers here:
How to relabel axis ticks for a matplotlib heatmap
(3 answers)
Closed 1 year ago.
I am creating a heatmap in matplotlib where on the x and y axis is some parameter of a measurement and the color represents the value of the measurement. Matplotlib automatically gives the axes ticks based on the index of the value. For example if on the x axis I am measuring at 50 different values the ticks will be from 0 to 50. However the real value of this parameter is for example from -30 to 80 and I would like matplotlib to create the ticks based on this minimum and maximum.
I have tried using set_xticks but this requires the positions of the ticks as well as their labels. I am thinking that I should be able to just give matplotlib a min of -34 and max of 67 and have it create nice looking ticks placed at the proper positions but I haven't been able to find how.
After some digging in examples on the matplotlib website I found this option in imshow called extent in which you can replace the default zero-based coordinates with your own values for the min and max of both axes.
Wouldn't pyplot.xlim() or pyplot.figure.set_xlim() work in this case? Just say something like:
import matplotlib.pyplot as plt
plt.xlim(-30,80)
plt.ylim(0,100) #Or whatever
As far as I know the set_xticks function is too sophisticated for this. With that one you can specify what to put as your tick labels etc. For example if you want to associate a numerical series with a series of letters. For example:
x = [-8,-6,-4,-2,0,2,4,6,8]
labels = ['K2','K4','K6','K8','M0','M2','M4','M6','M8']
plt.xticks(x, labels)
Is one I used personally to translate integers into stellar spectral types (which is relevant, since I'm an astronomer ;p).
Hope this helps.
`

Categories

Resources