Coloring Individual Points of a Scatter Plot in Python3 [duplicate] - python

This question already has answers here:
Scatter plot and Color mapping in Python
(4 answers)
Closed 3 years ago.
I am currently working on a project that deals with a scatter plot, made using matplotlib and numpy,
I was wondering whether I could assign color to each point based on their (x,y) coordinates, assume that I have a function that maps (x,y) to (r,g,b). There are a lot of points (~250,000).
Is there any possible way to achieve this?

Matplotlib's scatter plot supports this via the color parameter, check this example. You have to prepare your colors beforehand and pass it to the color. As mentioned, color can be a sequence.
Another way, maybe slightly faster, would be to use seaborn's scatter plot.

Related

matplotlib 3D plotting. Drawing edges parallel to axes [duplicate]

This question already has an answer here:
3D figures from Matplotlib visibility of pane edge
(1 answer)
Closed 11 hours ago.
In 3D plot by default matplotlib draws axes like the first image. How can we draw more edges parallel to the axes like the second or third images. Found out Scidavis default plot type is like the third image. https://scidavis.sourceforge.net/manual/x528.html
Finally, found a way here myself. A interesting work around to this problem.
Missing spine in 3d plot

how to draw a rectangle over a matplotlib figure, also overlaying the axes [duplicate]

This question already has answers here:
How to draw rectangle outside of the plot frame in Matplotlib
(2 answers)
How to position a matplotlib patch outside of the axes range (so that it could be next to the title, or legend, or anywhere on the figure)
(2 answers)
Closed 5 months ago.
I would like to draw a rectangle over a matplotlib figure, in a way that allows overlaying the axes.
The answers I found online only allow drawing a rectange inside the axes, but not overlaying them.
EDIT: This answer allows drawing outside the plot frame. However it does not overlay the axis. Namely, the axis is kept visible. See the example where the axis is still visible behind the red rectangle
See an example below for what I wish to achieve (Code for the bar plot can be taken from here. For the example, the figure was edited with a simple paint software).
It is drawn via matplotlib.pyplot.hist() function in matplotlib

How to change the default colors for multiple plots in matplotlib? [duplicate]

This question already has answers here:
How to set the default color cycle for all subplots with matplotlib?
(3 answers)
How to set default colormap in Matplotlib
(2 answers)
Closed 5 years ago.
I want to use the same colormap/color cycle/palette to every plot in a Jupyter notebook.
With the seaborn package, I can use:
seaborn.set_palette('Set1')
Is there a way to do the same using only matplotlib, without using seaborn?
I know how to define the colormap to each plot separately and I am aware of the predefined style (e.g, ggplot), but I can't find a way to define only the colormap to all the plots at once.
My intention is simplify the code for my students, thus using the intricate code behind set_palette() is not an option.
Edit: as the accepted answer shows, I was confusing colormap with color cycle.
The default colormap in matplotlib is "viridis". This is set at the rcParam "image.cmap".
The default colorcycle can be changed via the "axes.prop_cycle" rcParam.
import matplotlib.pyplot as plt
# to change default colormap
plt.rcParams["image.cmap"] = "Set1"
# to change default color cycle
plt.rcParams['axes.prop_cycle'] = plt.cycler(color=plt.cm.Set1.colors)

How to make a bi-variate or 2-dimension colormap with matplotlib?

I am trying to make a geographical map with colors, in which the colors of pixels will depend on 2-dimensions of (x,y), like this one extracted from one publication:
I searched online and this is where I arrive:
https://gist.github.com/ChaoYue/81eb01b558f068f11ee741c56557a6a2
To put it short, I know how to pick up color by x-axis value, and use y-axis value as an indicator of either saturation, or brightness or transparency of the color. Or somehow to convert this (x,y) information back to linear space and select color from a matplotlib colormap. But the question is, for example, suppose I want the color as lowerleft=blue,lowerright=green; upperleft=red,upperright=orange. How can I interpolate the color of pixels in between? Or maybe this example is in contradiction with color theory and it is not possible? The equivalent question is: how can I make a colorbar legend like the figure shown in the above?
These are the online sources that help me, but they don't directly give the instructions I am looking for:
Is there any way to use bivariate colormaps in matplotlib?
https://github.com/matplotlib/matplotlib/issues/4369

Align x-axis ticks in bar plot [duplicate]

This question already has an answer here:
Individually labeled bars for bar graphs in matplotlib / Python
(1 answer)
Closed 8 years ago.
I have a bar plot that looks like this:
How can I shift the x-axis labels so that they are centered under the corresponding bars? I'm trying to do this in a subplot.
You need to either use the set_xticks() function for your axis with a properly spaced number array (which you probably have in your code already, but not set properly), or you can use the ax.bar(x, y, align='center') command when setting up the bar chart. See the following answer:
Individually labeled bars for bar graphs in matplotlib / Python

Categories