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

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)

Related

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

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

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.

Remove dashes from grid [duplicate]

This question already has an answer here:
Remove the x-axis ticks while keeping the grids (matplotlib) [duplicate]
(1 answer)
Closed 5 years ago.
If I run this code
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.xaxis.set_tick_params(length=0,labelsize=0)
ax.grid(True)
I get the following:
The xaxis ticks and labels don't show (as expected), but some dashes appear on the bottom of the plot (the first three of which I have circled in red).
How can I remove them? I have looked at the documentation for grids, but can't find anything.
An answer which teaches me how I could have figured out how to do this by looking at the documentation would be particularly useful.
What you see are the ticklabels, which have size 0. Even zero sized ticks appear as a single dot because of antigraining.
You probably want to set the label off completely
ax.xaxis.set_tick_params(length=0,labelbottom=False)
You find out about this by looking at the available arguments in the documentation.

smoothing imshow plot with matplotlib [duplicate]

This question already has answers here:
How to 'turn off' blurry effect of imshow() in matplotlib?
(2 answers)
Closed 7 years ago.
I am plotting a density of counts with imshow from matplotlib.pyplot but I'd like to have a smoother plot.
Can I apply any filter on this?
Try using the interpolation argument:
ax.imshow(grid, interpolation=interp_method)
matplotlib demo
matplotlib api
If you manually want to handle how strong the filter is you could
do something along the lines of (scipy.ndimage has a lot of filters)
from scipy.ndimage.filters import gaussian_filter
arr=np.zeros((20,20))
arr[0,:]=3
arr[0,0]=20
arr[19,19]=30
arr[10:12,10:12]=10
filtered_arr=gaussian_filter(arr, sigma)
plt.imshow(filtered_arr)
to get (from top left: raw image, sigma=1,2,3):

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