Dataset Viewer
question
stringlengths 72
20.5k
| response
stringlengths 25
15.9k
| best_answers_score
float64 0.6
0.8
| library_name
stringclasses 7
values | question_url
stringlengths 61
128
| best_answers_votes
int64 1
4.7k
| tags
listlengths 1
5
| question_length
int64 72
20.5k
| response_length
int64 25
15.9k
| tag_count
int64 1
5
|
---|---|---|---|---|---|---|---|---|---|
How do I set the figure title and axes labels font size? I am creating a figure in Matplotlib like this:
```py
from matplotlib import pyplot as plt
fig = plt.figure()
plt.plot(data)
fig.suptitle('test title')
plt.xlabel('xlabel')
plt.ylabel('ylabel')
fig.savefig('test.jpg')
```
I want to specify font sizes for the figure title and the axis labels. I need all three to be different font sizes, so setting a global font size (mpl.rcParams['font.size']=x) is not what I want. How do I set font sizes for the figure title and the axis labels individually?
|
Functions dealing with text like label, title, etc. accept parameters same as matplotlib.text.Text. For the font size you can use size/fontsize:
```
from matplotlib import pyplot as plt
fig = plt.figure()
plt.plot(data)
fig.suptitle('test title', fontsize=20)
plt.xlabel('xlabel', fontsize=18)
plt.ylabel('ylabel', fontsize=16)
fig.savefig('test.jpg')
```
For globally setting title and label sizes, mpl.rcParams contains axes.titlesize and axes.labelsize. (From the page):
```
axes.titlesize : large # fontsize of the axes title
axes.labelsize : medium # fontsize of the x any y labels
```
(As far as I can see, there is no way to set x and y label sizes separately.) And I see that axes.titlesize does not affect suptitle. I guess, you need to set that manually.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/12444716/how-do-i-set-the-figure-title-and-axes-labels-font-size
| 1,262 |
[
"python",
"matplotlib",
"axis-labels",
"yaxis",
"x-axis"
] | 555 | 784 | 5 |
When to use cla(), clf() or close() for clearing a plot Matplotlib offers these functions:
```py
cla() # Clear axis
clf() # Clear figure
close() # Close a figure window
```
When should I use each function and what exactly does it do?
|
They all do different things, since matplotlib uses a hierarchical order in which a figure window contains a figure which may consist of many axes. Additionally, there are functions from the pyplot interface and there are methods on the Figure class. I will discuss both cases below. pyplot interface pyplot is a module that collects a couple of functions that allow matplotlib to be used in a functional manner. I here assume that pyplot has been imported as import matplotlib.pyplot as plt. In this case, there are three different commands that remove stuff: See matplotlib.pyplot Functions: plt.cla() clears an axis, i.e. the currently active axis in the current figure. It leaves the other axes untouched. plt.clf() clears the entire current figure with all its axes, but leaves the window opened, such that it may be reused for other plots. plt.close() closes a window, which will be the current window, if not specified otherwise. Which functions suits you best depends thus on your use-case. The close() function furthermore allows one to specify which window should be closed. The argument can either be a number or name given to a window when it was created using figure(number_or_name) or it can be a figure instance fig obtained, i.e., usingfig = figure(). If no argument is given to close(), the currently active window will be closed. Furthermore, there is the syntax close('all'), which closes all figures. methods of the Figure class Additionally, the Figure class provides methods for clearing figures. I'll assume in the following that fig is an instance of a Figure: fig.clf() clears the entire figure. This call is equivalent to plt.clf() only if fig is the current figure. fig.clear() is a synonym for fig.clf() Note that even del fig will not close the associated figure window. As far as I know the only way to close a figure window is using plt.close(fig) as described above.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/8213522/when-to-use-cla-clf-or-close-for-clearing-a-plot
| 960 |
[
"python",
"matplotlib",
"plot"
] | 238 | 1,898 | 3 |
What does the argument mean in fig.add_subplot(111)? Sometimes I come across code such as this:
```
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
fig = plt.figure()
fig.add_subplot(111)
plt.scatter(x, y)
plt.show()
```
Which produces: I've been reading the documentation like crazy but I can't find an explanation for the 111. sometimes I see a 212. What does the argument of fig.add_subplot() mean?
|
I think this would be best explained by the following picture: To initialize the above, one would type:
```
import matplotlib.pyplot as plt
fig = plt.figure()
fig.add_subplot(221) #top left
fig.add_subplot(222) #top right
fig.add_subplot(223) #bottom left
fig.add_subplot(224) #bottom right
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/3584805/what-does-the-argument-mean-in-fig-add-subplot111
| 638 |
[
"python",
"matplotlib",
"subplot",
"figure"
] | 428 | 315 | 4 |
How to change the figure size of a seaborn axes or figure level plot How do I change the size of my image so it's suitable for printing? For example, I'd like to use an A4 paper, whose dimensions are 11.7 inches by 8.27 inches in landscape orientation.
|
You can also set figure size by passing dictionary to rc parameter with key 'figure.figsize' in seaborn set_theme method (which replaces the set method, deprecated in v0.11.0 (September 2020))
```
import seaborn as sns
sns.set_theme(rc={'figure.figsize':(11.7,8.27)})
```
Other alternative may be to use figure.figsize of rcParams to set figure size as below:
```
from matplotlib import rcParams
# figure size in inches
rcParams['figure.figsize'] = 11.7,8.27
```
More details can be found in matplotlib documentation
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/31594549/how-to-change-the-figure-size-of-a-seaborn-axes-or-figure-level-plot
| 568 |
[
"python",
"matplotlib",
"seaborn",
"figsize"
] | 252 | 520 | 4 |
How to remove xticks from a plot I have a semilogx plot and I would like to remove the xticks. I tried:
```
plt.gca().set_xticks([])
plt.xticks([])
ax.set_xticks([])
```
The grid disappears (ok), but small ticks (at the place of the main ticks) remain. How to remove them?
|
The plt.tick_params method is very useful for stuff like this. This code turns off major and minor ticks and removes the labels from the x-axis. Note that there is also ax.tick_params for matplotlib.axes.Axes objects.
```
from matplotlib import pyplot as plt
plt.plot(range(10))
plt.tick_params(
axis='x', # changes apply to the x-axis
which='both', # both major and minor ticks are affected
bottom=False, # ticks along the bottom edge are off
top=False, # ticks along the top edge are off
labelbottom=False) # labels along the bottom edge are off
plt.show()
plt.savefig('plot')
plt.clf()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/12998430/how-to-remove-xticks-from-a-plot
| 751 |
[
"python",
"matplotlib",
"axis",
"xticks"
] | 273 | 640 | 4 |
Matplotlib different size subplots I need to add two subplots to a figure. One subplot needs to be about three times as wide as the second (same height). I accomplished this using GridSpec and the colspan argument but I would like to do this using figure so I can save to PDF. I can adjust the first figure using the figsize argument in the constructor, but how do I change the size of the second plot?
|
As of matplotlib 3.6.0, width_ratios and height_ratios can now be passed directly as keyword arguments to plt.subplots and subplot_mosaic, as per What's new in Matplotlib 3.6.0 (Sep 15, 2022). f, (a0, a1) = plt.subplots(1, 2, width_ratios=[3, 1]) f, (a0, a1, a2) = plt.subplots(3, 1, height_ratios=[1, 1, 3]) Another way is to use the subplots function and pass the width ratio with gridspec_kw matplotlib Tutorial: Customizing Figure Layouts Using GridSpec and Other Functions matplotlib.gridspec.GridSpec has available gridspect_kw options
```py
import numpy as np
import matplotlib.pyplot as plt
# generate some data
x = np.arange(0, 10, 0.2)
y = np.sin(x)
# plot it
f, (a0, a1) = plt.subplots(1, 2, gridspec_kw={'width_ratios': [3, 1]})
a0.plot(x, y)
a1.plot(y, x)
f.tight_layout()
f.savefig('grid_figure.pdf')
```
Because the question is canonical, here is an example with vertical subplots.
```py
# plot it
f, (a0, a1, a2) = plt.subplots(3, 1, gridspec_kw={'height_ratios': [1, 1, 3]})
a0.plot(x, y)
a1.plot(x, y)
a2.plot(x, y)
f.tight_layout()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/10388462/matplotlib-different-size-subplots
| 668 |
[
"python",
"matplotlib",
"subplot",
"figure",
"matplotlib-gridspec"
] | 402 | 1,062 | 5 |
Display image as grayscale I'm trying to display a grayscale image using matplotlib.pyplot.imshow(). My problem is that the grayscale image is displayed as a colormap. I need it to be grayscale because I want to draw on top of the image with color. I read in the image and convert to grayscale using PIL's Image.open().convert("L")
```
image = Image.open(file).convert("L")
```
Then I convert the image to a matrix so that I can easily do some image processing using
```
matrix = scipy.misc.fromimage(image, 0)
```
However, when I do
```
figure()
matplotlib.pyplot.imshow(matrix)
show()
```
it displays the image using a colormap (i.e. it's not grayscale). What am I doing wrong here?
|
The following code will load an image from a file image.png and will display it as grayscale.
```
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
fname = 'image.png'
image = Image.open(fname).convert("L")
arr = np.asarray(image)
plt.imshow(arr, cmap='gray', vmin=0, vmax=255)
plt.show()
```
If you want to display the inverse grayscale, switch the cmap to cmap='gray_r'.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/3823752/display-image-as-grayscale
| 520 |
[
"python",
"matplotlib",
"grayscale",
"imshow"
] | 691 | 397 | 4 |
How can I convert an RGB image into grayscale in Python? I'm trying to use matplotlib to read in an RGB image and convert it to grayscale. In matlab I use this:
```
img = rgb2gray(imread('image.png'));
```
In the matplotlib tutorial they don't cover it. They just read in the image
```
import matplotlib.image as mpimg
img = mpimg.imread('image.png')
```
and then they slice the array, but that's not the same thing as converting RGB to grayscale from what I understand.
```
lum_img = img[:,:,0]
```
I find it hard to believe that numpy or matplotlib doesn't have a built-in function to convert from rgb to gray. Isn't this a common operation in image processing? I wrote a very simple function that works with the image imported using imread in 5 minutes. It's horribly inefficient, but that's why I was hoping for a professional implementation built-in. Sebastian has improved my function, but I'm still hoping to find the built-in one. matlab's (NTSC/PAL) implementation:
```
import numpy as np
def rgb2gray(rgb):
r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
return gray
```
|
How about doing it with Pillow:
```
from PIL import Image
img = Image.open('image.png').convert('L')
img.save('greyscale.png')
```
If an alpha (transparency) channel is present in the input image and should be preserved, use mode LA:
```
img = Image.open('image.png').convert('LA')
```
Using matplotlib and the formula
```
Y' = 0.2989 R + 0.5870 G + 0.1140 B
```
you could do:
```
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
img = mpimg.imread('image.png')
gray = rgb2gray(img)
plt.imshow(gray, cmap=plt.get_cmap('gray'), vmin=0, vmax=1)
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/12201577/how-can-i-convert-an-rgb-image-into-grayscale-in-python
| 488 |
[
"python",
"matplotlib"
] | 1,140 | 683 | 2 |
Is there a way to detach matplotlib plots so that the computation can continue? After these instructions in the Python interpreter one gets a window with a plot:
```
from matplotlib.pyplot import *
plot([1,2,3])
show()
# other code
```
Unfortunately, I don't know how to continue to interactively explore the figure created by show() while the program does further calculations. Is it possible at all? Sometimes calculations are long and it would help if they would proceed during examination of intermediate results.
|
Use matplotlib's calls that won't block: Using draw():
```
from matplotlib.pyplot import plot, draw, show
plot([1,2,3])
draw()
print('continue computation')
# at the end call show to ensure window won't close.
show()
```
Using interactive mode:
```
from matplotlib.pyplot import plot, ion, show
ion() # enables interactive mode
plot([1,2,3]) # result shows immediatelly (implicit draw())
print('continue computation')
# at the end call show to ensure window won't close.
show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/458209/is-there-a-way-to-detach-matplotlib-plots-so-that-the-computation-can-continue
| 265 |
[
"python",
"matplotlib",
"plot"
] | 518 | 486 | 3 |
Rotate label text in seaborn I have a simple factorplot
```
import seaborn as sns
g = sns.factorplot("name", "miss_ratio", "policy", dodge=.2,
linestyles=["none", "none", "none", "none"], data=df[df["level"] == 2])
```
The problem is that the x labels all run together, making them unreadable. How do you rotate the text so that the labels are readable?
|
I had a problem with the answer by @mwaskorn, namely that
```
g.set_xticklabels(rotation=30)
```
fails, because this also requires the labels. A bit easier than the answer by @Aman is to just add
```
plt.xticks(rotation=30)
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/26540035/rotate-label-text-in-seaborn
| 449 |
[
"python",
"matplotlib",
"seaborn",
"x-axis"
] | 359 | 229 | 4 |
Set markers for individual points on a line I have used Matplotlib to plot lines on a figure. Now I would now like to set the style, specifically the marker, for individual points on the line. How do I do this? To clarify my question, I want to be able to set the style for individual markers on a line, not every marker on said line.
|
Specify the keyword args linestyle and/or marker in your call to plot. For example, using a dashed line and blue circle markers:
```
plt.plot(range(10), linestyle='--', marker='o', color='b', label='line with marker')
plt.legend()
```
A shortcut call for the same thing:
```
plt.plot(range(10), '--bo', label='line with marker')
plt.legend()
```
Here is a list of the possible line and marker styles:
```
================ ===============================
character description
================ ===============================
- solid line style
-- dashed line style
-. dash-dot line style
: dotted line style
. point marker
, pixel marker
o circle marker
v triangle_down marker
^ triangle_up marker
< triangle_left marker
> triangle_right marker
1 tri_down marker
2 tri_up marker
3 tri_left marker
4 tri_right marker
s square marker
p pentagon marker
* star marker
h hexagon1 marker
H hexagon2 marker
+ plus marker
x x marker
D diamond marker
d thin_diamond marker
| vline marker
_ hline marker
================ ===============================
```
edit: with an example of marking an arbitrary subset of points, as requested in the comments:
```
import numpy as np
import matplotlib.pyplot as plt
xs = np.linspace(-np.pi, np.pi, 30)
ys = np.sin(xs)
markers_on = [12, 17, 18, 19]
plt.plot(xs, ys, '-gD', markevery=markers_on, label='line with select markers')
plt.legend()
plt.show()
```
This last example using the markevery kwarg is possible in since 1.4+, due to the merge of this feature branch. If you are stuck on an older version of matplotlib, you can still achieve the result by overlaying a scatterplot on the line plot. See the edit history for more details.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/8409095/set-markers-for-individual-points-on-a-line
| 552 |
[
"python",
"matplotlib"
] | 334 | 2,157 | 2 |
How to convert a NumPy array to PIL image applying matplotlib colormap I want to take a NumPy 2D array which represents a grayscale image, and convert it to an RGB PIL image while applying some of the matplotlib colormaps. I can get a reasonable PNG output by using the pyplot.figure.figimage command:
```
dpi = 100.0
w, h = myarray.shape[1]/dpi, myarray.shape[0]/dpi
fig = plt.figure(figsize=(w,h), dpi=dpi)
fig.figimage(sub, cmap=cm.gist_earth)
plt.savefig('out.png')
```
Although I could adapt this to get what I want (probably using StringIO do get the PIL image), I wonder if there is not a simpler way to do that, since it seems to be a very natural problem of image visualization. Let's say, something like this:
```
colored_PIL_image = magic_function(array, cmap)
```
|
Quite a busy one-liner, but here it is: First ensure your NumPy array, myarray, is normalised with the max value at 1.0. Apply the colormap directly to myarray. Rescale to the 0-255 range. Convert to integers, using np.uint8(). Use Image.fromarray(). And you're done:
```
from PIL import Image
from matplotlib import cm
im = Image.fromarray(np.uint8(cm.gist_earth(myarray)*255))
```
with plt.savefig(): with im.save():
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/10965417/how-to-convert-a-numpy-array-to-pil-image-applying-matplotlib-colormap
| 400 |
[
"python",
"numpy",
"matplotlib",
"python-imaging-library",
"color-mapping"
] | 777 | 419 | 5 |
Reduce left and right margins in matplotlib plot I'm struggling to deal with my plot margins in matplotlib. I've used the code below to produce my chart:
```
plt.imshow(g)
c = plt.colorbar()
c.set_label("Number of Slabs")
plt.savefig("OutputToUse.png")
```
However, I get an output figure with lots of white space on either side of the plot. I've searched google and read the matplotlib documentation, but I can't seem to find how to reduce this.
|
One way to automatically do this is the bbox_inches='tight' kwarg to plt.savefig. E.g.
```
import matplotlib.pyplot as plt
import numpy as np
data = np.arange(3000).reshape((100,30))
plt.imshow(data)
plt.savefig('test.png', bbox_inches='tight')
```
Another way is to use fig.tight_layout()
```
import matplotlib.pyplot as plt
import numpy as np
xs = np.linspace(0, 1, 20); ys = np.sin(xs)
fig = plt.figure()
axes = fig.add_subplot(1,1,1)
axes.plot(xs, ys)
# This should be called after all axes have been added
fig.tight_layout()
fig.savefig('test.png')
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/4042192/reduce-left-and-right-margins-in-matplotlib-plot
| 392 |
[
"python",
"matplotlib"
] | 447 | 562 | 2 |
Getting individual colors from a color map in matplotlib If you have a Colormap cmap, for example:
```
cmap = matplotlib.cm.get_cmap('Spectral')
```
How can you get a particular colour out of it between 0 and 1, where 0 is the first colour in the map and 1 is the last colour in the map? Ideally, I would be able to get the middle colour in the map by doing:
```
```python
do_some_magic(cmap, 0.5) # Return an RGBA tuple
#Output
#(0.1, 0.2, 0.3, 1.0)
#```
```
|
You can do this with the code below, and the code in your question was actually very close to what you needed, all you have to do is call the cmap object you have.
```
import matplotlib
cmap = matplotlib.cm.get_cmap('Spectral')
rgba = cmap(0.5)
print(rgba) # (0.99807766255210428, 0.99923106502084169, 0.74602077638401709, 1.0)
```
For values outside of the range [0.0, 1.0] it will return the under and over colour (respectively). This, by default, is the minimum and maximum colour within the range (so 0.0 and 1.0). This default can be changed with cmap.set_under() and cmap.set_over(). For "special" numbers such as np.nan and np.inf the default is to use the 0.0 value, this can be changed using cmap.set_bad() similarly to under and over as above. Finally it may be necessary for you to normalize your data such that it conforms to the range [0.0, 1.0]. This can be done using matplotlib.colors.Normalize simply as shown in the small example below where the arguments vmin and vmax describe what numbers should be mapped to 0.0 and 1.0 respectively.
```
import matplotlib
norm = matplotlib.colors.Normalize(vmin=10.0, vmax=20.0)
print(norm(15.0)) # 0.5
```
A logarithmic normaliser (matplotlib.colors.LogNorm) is also available for data ranges with a large range of values. (Thanks to both Joe Kington and tcaswell for suggestions on how to improve the answer.)
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/25408393/getting-individual-colors-from-a-color-map-in-matplotlib
| 415 |
[
"python",
"matplotlib",
"colors"
] | 441 | 1,373 | 3 |
How to do a scatter plot with empty circles in Python? In Python, with Matplotlib, how can a scatter plot with empty circles be plotted? The goal is to draw empty circles around some of the colored disks already plotted by scatter(), so as to highlight them, ideally without having to redraw the colored circles. I tried facecolors=None, to no avail.
|
From the documentation for scatter:
```
Optional kwargs control the Collection properties; in particular:
edgecolors:
The string ‘none’ to plot faces with no outlines
facecolors:
The string ‘none’ to plot unfilled outlines
```
Try the following:
```
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(60)
y = np.random.randn(60)
plt.scatter(x, y, s=80, facecolors='none', edgecolors='r')
plt.show()
```
Note: For other types of plots see this post on the use of markeredgecolor and markerfacecolor.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/4143502/how-to-do-a-scatter-plot-with-empty-circles-in-python
| 408 |
[
"python",
"matplotlib",
"geometry",
"scatter-plot",
"scatter"
] | 350 | 550 | 5 |
How to plot in multiple subplots I am a little confused about how this code works:
```
fig, axes = plt.subplots(nrows=2, ncols=2)
plt.show()
```
How does the fig, axes work in this case? What does it do? Also why wouldn't this work to do the same thing:
```
fig = plt.figure()
axes = fig.subplots(nrows=2, ncols=2)
```
|
There are several ways to do it. The subplots method creates the figure along with the subplots that are then stored in the ax array. For example:
```
import matplotlib.pyplot as plt
x = range(10)
y = range(10)
fig, ax = plt.subplots(nrows=2, ncols=2)
for row in ax:
for col in row:
col.plot(x, y)
plt.show()
```
However, something like this will also work, it's not so "clean" though since you are creating a figure with subplots and then add on top of them:
```
fig = plt.figure()
plt.subplot(2, 2, 1)
plt.plot(x, y)
plt.subplot(2, 2, 2)
plt.plot(x, y)
plt.subplot(2, 2, 3)
plt.plot(x, y)
plt.subplot(2, 2, 4)
plt.plot(x, y)
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/31726643/how-to-plot-in-multiple-subplots
| 343 |
[
"python",
"pandas",
"matplotlib",
"seaborn",
"subplot"
] | 320 | 664 | 5 |
How to set xlim and ylim for a subplot [duplicate] This question already has answers here: How to set the subplot axis range (6 answers) Closed 10 years ago. I would like to limit the X and Y axis in matplotlib for a specific subplot. The subplot figure itself doesn't have any axis property. I want for example to change only the limits for the second plot:
```
import matplotlib.pyplot as plt
fig=plt.subplot(131)
plt.scatter([1,2],[3,4])
fig=plt.subplot(132)
plt.scatter([10,20],[30,40])
fig=plt.subplot(133)
plt.scatter([15,23],[35,43])
plt.show()
```
|
You should use the object-oriented interface to matplotlib, rather than the state machine interface. Almost all of the plt.* function are thin wrappers that basically do gca().*. plt.subplot returns an axes object. Once you have a reference to the axes object you can plot directly to it, change its limits, etc.
```
import matplotlib.pyplot as plt
ax1 = plt.subplot(131)
ax1.scatter([1, 2], [3, 4])
ax1.set_xlim([0, 5])
ax1.set_ylim([0, 5])
ax2 = plt.subplot(132)
ax2.scatter([1, 2],[3, 4])
ax2.set_xlim([0, 5])
ax2.set_ylim([0, 5])
```
and so on for as many axes as you want. Or better, wrap it all up in a loop:
```
import matplotlib.pyplot as plt
DATA_x = ([1, 2],
[2, 3],
[3, 4])
DATA_y = DATA_x[::-1]
XLIMS = [[0, 10]] * 3
YLIMS = [[0, 10]] * 3
for j, (x, y, xlim, ylim) in enumerate(zip(DATA_x, DATA_y, XLIMS, YLIMS)):
ax = plt.subplot(1, 3, j + 1)
ax.scatter(x, y)
ax.set_xlim(xlim)
ax.set_ylim(ylim)
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/15858192/how-to-set-xlim-and-ylim-for-a-subplot
| 370 |
[
"python",
"matplotlib",
"plot",
"subplot"
] | 556 | 957 | 4 |
Date ticks and rotation [duplicate] This question already has answers here: Rotate axis tick labels (13 answers) Closed 2 years ago. I am having an issue trying to get my date ticks rotated in matplotlib. A small sample program is below. If I try to rotate the ticks at the end, the ticks do not get rotated. If I try to rotate the ticks as shown under the comment 'crashes', then matplot lib crashes. This only happens if the x-values are dates. If I replaces the variable dates with the variable t in the call to avail_plot, the xticks(rotation=70) call works just fine inside avail_plot. Any ideas?
```
import numpy as np
import matplotlib.pyplot as plt
import datetime as dt
def avail_plot(ax, x, y, label, lcolor):
ax.plot(x,y,'b')
ax.set_ylabel(label, rotation='horizontal', color=lcolor)
ax.get_yaxis().set_ticks([])
#crashes
#plt.xticks(rotation=70)
ax2 = ax.twinx()
ax2.plot(x, [1 for a in y], 'b')
ax2.get_yaxis().set_ticks([])
ax2.set_ylabel('testing')
f, axs = plt.subplots(2, sharex=True, sharey=True)
t = np.arange(0.01, 5, 1)
s1 = np.exp(t)
start = dt.datetime.now()
dates=[]
for val in t:
next_val = start + dt.timedelta(0,val)
dates.append(next_val)
start = next_val
avail_plot(axs[0], dates, s1, 'testing', 'green')
avail_plot(axs[1], dates, s1, 'testing2', 'red')
plt.subplots_adjust(hspace=0, bottom=0.3)
plt.yticks([0.5,],("",""))
#doesn't crash, but does not rotate the xticks
#plt.xticks(rotation=70)
plt.show()
```
|
If you prefer a non-object-oriented approach, move plt.xticks(rotation=70) to right before the two avail_plot calls, eg
```
plt.xticks(rotation=70)
avail_plot(axs[0], dates, s1, 'testing', 'green')
avail_plot(axs[1], dates, s1, 'testing2', 'red')
```
This sets the rotation property before setting up the labels. Since you have two axes here, plt.xticks gets confused after you've made the two plots. At the point when plt.xticks doesn't do anything, plt.gca() does not give you the axes you want to modify, and so plt.xticks, which acts on the current axes, is not going to work. For an object-oriented approach not using plt.xticks, you can use
```
plt.setp( axs[1].xaxis.get_majorticklabels(), rotation=70 )
```
after the two avail_plot calls. This sets the rotation on the correct axes specifically.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/11264521/date-ticks-and-rotation
| 319 |
[
"python",
"matplotlib",
"xticks"
] | 1,492 | 805 | 3 |
Format y axis as percent I have an existing plot that was created with pandas like this:
```
df['myvar'].plot(kind='bar')
```
The y axis is format as float and I want to change the y axis to percentages. All of the solutions I found use ax.xyz syntax and I can only place code below the line above that creates the plot (I cannot add ax=ax to the line above.) How can I format the y axis as percentages without changing the line above? Here is the solution I found but requires that I redefine the plot:
```
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as mtick
data = [8,12,15,17,18,18.5]
perc = np.linspace(0,100,len(data))
fig = plt.figure(1, (7,4))
ax = fig.add_subplot(1,1,1)
ax.plot(perc, data)
fmt = '%.0f%%' # Format you want the ticks, e.g. '40%'
xticks = mtick.FormatStrFormatter(fmt)
ax.xaxis.set_major_formatter(xticks)
plt.show()
```
Link to the above solution: Pyplot: using percentage on x axis
|
This is a few months late, but I have created PR#6251 with matplotlib to add a new PercentFormatter class. With this class you just need one line to reformat your axis (two if you count the import of matplotlib.ticker):
```
import ...
import matplotlib.ticker as mtick
ax = df['myvar'].plot(kind='bar')
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
```
PercentFormatter() accepts three arguments, xmax, decimals, symbol. xmax allows you to set the value that corresponds to 100% on the axis. This is nice if you have data from 0.0 to 1.0 and you want to display it from 0% to 100%. Just do PercentFormatter(1.0). The other two parameters allow you to set the number of digits after the decimal point and the symbol. They default to None and '%', respectively. decimals=None will automatically set the number of decimal points based on how much of the axes you are showing. Update PercentFormatter was introduced into Matplotlib proper in version 2.1.0.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/31357611/format-y-axis-as-percent
| 376 |
[
"python",
"pandas",
"matplotlib",
"plot"
] | 947 | 963 | 4 |
reducing number of plot ticks I have too many ticks on my graph and they are running into each other. How can I reduce the number of ticks? For example, I have ticks:
```
1E-6, 1E-5, 1E-4, ... 1E6, 1E7
```
And I only want:
```
1E-5, 1E-3, ... 1E5, 1E7
```
I've tried playing with the LogLocator, but I haven't been able to figure this out.
|
Alternatively, if you want to simply set the number of ticks while allowing matplotlib to position them (currently only with MaxNLocator), there is pyplot.locator_params,
```
pyplot.locator_params(nbins=4)
```
You can specify specific axis in this method as mentioned below, default is both:
```
# To specify the number of ticks on both or any single axes
pyplot.locator_params(axis='y', nbins=6)
pyplot.locator_params(axis='x', nbins=10)
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/6682784/reducing-number-of-plot-ticks
| 368 |
[
"python",
"matplotlib",
"xticks",
"yticks"
] | 341 | 444 | 4 |
How to plot multiple dataframes in subplots I have a few Pandas DataFrames sharing the same value scale, but having different columns and indices. When invoking df.plot(), I get separate plot images. what I really want is to have them all in the same plot as subplots, but I'm unfortunately failing to come up with a solution to how and would highly appreciate some help.
|
You can manually create the subplots with matplotlib, and then plot the dataframes on a specific subplot using the ax keyword. For example for 4 subplots (2x2):
```
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=2, ncols=2)
df1.plot(ax=axes[0,0])
df2.plot(ax=axes[0,1])
...
```
Here axes is an array which holds the different subplot axes, and you can access one just by indexing axes. If you want a shared x-axis, then you can provide sharex=True to plt.subplots.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/22483588/how-to-plot-multiple-dataframes-in-subplots
| 414 |
[
"python",
"pandas",
"matplotlib",
"seaborn",
"subplot"
] | 371 | 483 | 5 |
How do I equalize the scales of the x-axis and y-axis? How do I create a plot where the scales of x-axis and y-axis are the same? This equal ratio should be maintained even if I change the window size. Currently, my graph scales together with the window size. I tried:
```
plt.xlim(-3, 3)
plt.ylim(-3, 3)
plt.axis('equal')
```
|
Use Axes.set_aspect in the following manner:
```
from matplotlib import pyplot as plt
plt.plot(range(5))
plt.xlim(-3, 3)
plt.ylim(-3, 3)
ax = plt.gca()
ax.set_aspect('equal', adjustable='box')
plt.draw()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/17990845/how-do-i-equalize-the-scales-of-the-x-axis-and-y-axis
| 323 |
[
"python",
"matplotlib",
"axis",
"aspect-ratio"
] | 327 | 208 | 4 |
Set Colorbar Range I have the following code:
```py
import matplotlib.pyplot as plt
cdict = {
'red' : ( (0.0, 0.25, .25), (0.02, .59, .59), (1., 1., 1.)),
'green': ( (0.0, 0.0, 0.0), (0.02, .45, .45), (1., .97, .97)),
'blue' : ( (0.0, 1.0, 1.0), (0.02, .75, .75), (1., 0.45, 0.45))
}
cm = m.colors.LinearSegmentedColormap('my_colormap', cdict, 1024)
plt.clf()
plt.pcolor(X, Y, v, cmap=cm)
plt.loglog()
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.colorbar()
plt.show()
```
This produces a graph of the values v on the axes X vs Y, using the specified colormap. The X and Y axes are perfect, but the colormap spreads between the min and max of v. I would like to force the colormap to range between 0 and 1. I thought of using:
```py
plt.axis(...)
```
To set the ranges of the axes, but this only takes arguments for the min and max of X and Y, not the colormap. Edit: For clarity, let's say I have one graph whose values range (0 ... 0.3), and another graph whose values (0.2 ... 0.8). In both graphs, I will want the range of the colorbar to be (0 ... 1). In both graphs, I want this range of colour to be identical using the full range of cdict above (so 0.25 in both graphs will be the same colour). In the first graph, all colours between 0.3 and 1.0 won't feature in the graph, but will in the colourbar key at the side. In the other, all colours between 0 and 0.2, and between 0.8 and 1 will not feature in the graph, but will in the colourbar at the side.
|
Using vmin and vmax forces the range for the colors. Here's an example:
```
import matplotlib as m
import matplotlib.pyplot as plt
import numpy as np
cdict = {
'red' : ( (0.0, 0.25, .25), (0.02, .59, .59), (1., 1., 1.)),
'green': ( (0.0, 0.0, 0.0), (0.02, .45, .45), (1., .97, .97)),
'blue' : ( (0.0, 1.0, 1.0), (0.02, .75, .75), (1., 0.45, 0.45))
}
cm = m.colors.LinearSegmentedColormap('my_colormap', cdict, 1024)
x = np.arange(0, 10, .1)
y = np.arange(0, 10, .1)
X, Y = np.meshgrid(x,y)
data = 2*( np.sin(X) + np.sin(3*Y) )
def do_plot(n, f, title):
#plt.clf()
plt.subplot(1, 3, n)
plt.pcolor(X, Y, f(data), cmap=cm, vmin=-4, vmax=4)
plt.title(title)
plt.colorbar()
plt.figure()
do_plot(1, lambda x:x, "all")
do_plot(2, lambda x:np.clip(x, -4, 0), "<0")
do_plot(3, lambda x:np.clip(x, 0, 4), ">0")
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/3373256/set-colorbar-range
| 242 |
[
"python",
"matplotlib",
"graph",
"colorbar",
"colormap"
] | 1,482 | 852 | 5 |
How to put individual tags for a matplotlib scatter plot? I am trying to do a scatter plot in matplotlib and I couldn't find a way to add tags to the points. For example:
```
scatter1=plt.scatter(data1["x"], data1["y"], marker="o",
c="blue",
facecolors="white",
edgecolors="blue")
```
I want for the points in "y" to have labels as "point 1", "point 2", etc. I couldn't figure it out.
|
Perhaps use plt.annotate:
```
import numpy as np
import matplotlib.pyplot as plt
N = 10
data = np.random.random((N, 4))
labels = ['point{0}'.format(i) for i in range(N)]
plt.subplots_adjust(bottom = 0.1)
plt.scatter(
data[:, 0], data[:, 1], marker='o', c=data[:, 2], s=data[:, 3] * 1500,
cmap=plt.get_cmap('Spectral'))
for label, x, y in zip(labels, data[:, 0], data[:, 1]):
plt.annotate(
label,
xy=(x, y), xytext=(-20, 20),
textcoords='offset points', ha='right', va='bottom',
bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5),
arrowprops=dict(arrowstyle = '->', connectionstyle='arc3,rad=0'))
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/5147112/how-to-put-individual-tags-for-a-matplotlib-scatter-plot
| 384 |
[
"python",
"matplotlib"
] | 448 | 676 | 2 |
Plotting time on the independent axis I have an array of timestamps in the format (HH:MM:SS.mmmmmm) and another array of floating point numbers, each corresponding to a value in the timestamp array. Can I plot time on the x axis and the numbers on the y-axis using Matplotlib? I was trying to, but somehow it was only accepting arrays of floats. How can I get it to plot the time? Do I have to modify the format in any way?
|
Update: This answer is outdated since matplotlib version 3.5. The plot function now handles datetime data directly. See https://matplotlib.org/3.5.1/api/_as_gen/matplotlib.pyplot.plot_date.html The use of plot_date is discouraged. This method exists for historic reasons and may be deprecated in the future. datetime-like data should directly be plotted using plot. If you need to plot plain numeric data as Matplotlib date format or need to set a timezone, call ax.xaxis.axis_date / ax.yaxis.axis_date before plot. See Axis.axis_date. Old, outdated answer: You must first convert your timestamps to Python datetime objects (use datetime.strptime). Then use date2num to convert the dates to matplotlib format. Plot the dates and values using plot_date:
```
import matplotlib.pyplot as plt
import matplotlib.dates
from datetime import datetime
x_values = [datetime(2021, 11, 18, 12), datetime(2021, 11, 18, 14), datetime(2021, 11, 18, 16)]
y_values = [1.0, 3.0, 2.0]
dates = matplotlib.dates.date2num(x_values)
plt.plot_date(dates, y_values)
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/1574088/plotting-time-on-the-independent-axis
| 226 |
[
"python",
"matplotlib",
"timestamp",
"x-axis"
] | 423 | 1,048 | 4 |
How do I tell matplotlib that I am done with a plot? The following code plots to two PostScript (.ps) files, but the second one contains both lines.
```
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
plt.subplot(111)
x = [1,10]
y = [30, 1000]
plt.loglog(x, y, basex=10, basey=10, ls="-")
plt.savefig("first.ps")
plt.subplot(111)
x = [10,100]
y = [10, 10000]
plt.loglog(x, y, basex=10, basey=10, ls="-")
plt.savefig("second.ps")
```
How can I tell matplotlib to start afresh for the second plot?
|
There is a clear figure command, and it should do it for you:
```
plt.clf()
```
If you have multiple subplots in the same figure
```
plt.cla()
```
clears the current axes.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/741877/how-do-i-tell-matplotlib-that-i-am-done-with-a-plot
| 227 |
[
"python",
"matplotlib",
"plot"
] | 532 | 173 | 3 |
How do I create a second (new) plot, then later plot on the old one? I want to plot data, then create a new figure and plot data2, and finally come back to the original plot and plot data3, kinda like this:
```
import numpy as np
import matplotlib as plt
x = arange(5)
y = np.exp(5)
plt.figure()
plt.plot(x, y)
z = np.sin(x)
plt.figure()
plt.plot(x, z)
w = np.cos(x)
plt.figure("""first figure""") # Here's the part I need
plt.plot(x, w)
```
FYI How do I tell matplotlib that I am done with a plot? does something similar, but not quite! It doesn't let me get access to that original plot.
|
If you find yourself doing things like this regularly it may be worth investigating the object-oriented interface to matplotlib. In your case:
```
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(5)
y = np.exp(x)
fig1, ax1 = plt.subplots()
ax1.plot(x, y)
ax1.set_title("Axis 1 title")
ax1.set_xlabel("X-label for axis 1")
z = np.sin(x)
fig2, (ax2, ax3) = plt.subplots(nrows=2, ncols=1) # two axes on figure
ax2.plot(x, z)
ax3.plot(x, -z)
w = np.cos(x)
ax1.plot(x, w) # can continue plotting on the first axis
```
It is a little more verbose but it's much clearer and easier to keep track of, especially with several figures each with multiple subplots.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/6916978/how-do-i-create-a-second-new-plot-then-later-plot-on-the-old-one
| 197 |
[
"python",
"matplotlib",
"plot",
"figure"
] | 593 | 672 | 4 |
How to plot a high resolution graph I've used matplotlib for plotting some experimental results (discussed it in here: Looping over files and plotting. However, saving the picture by clicking right to the image gives very bad quality / low resolution images.
```
from glob import glob
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
# loop over all files in the current directory ending with .txt
for fname in glob("./*.txt"):
# read file, skip header (1 line) and unpack into 3 variables
WL, ABS, T = np.genfromtxt(fname, skip_header=1, unpack=True)
# first plot
plt.plot(WL, T, label='BN', color='blue')
plt.xlabel('Wavelength (nm)')
plt.xlim(200,1000)
plt.ylim(0,100)
plt.ylabel('Transmittance, %')
mpl.rcParams.update({'font.size': 14})
#plt.legend(loc='lower center')
plt.title('')
plt.show()
plt.clf()
# second plot
plt.plot(WL, ABS, label='BN', color='red')
plt.xlabel('Wavelength (nm)')
plt.xlim(200,1000)
plt.ylabel('Absorbance, A')
mpl.rcParams.update({'font.size': 14})
#plt.legend()
plt.title('')
plt.show()
plt.clf()
```
Example graph of what I'm looking for: example graph
|
You can use savefig() to export to an image file:
```
plt.savefig('filename.png')
```
In addition, you can specify the dpi argument to some scalar value (default is 100). For example:
```
plt.savefig('filename.png', dpi=300)
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/39870642/how-to-plot-a-high-resolution-graph
| 303 |
[
"python",
"matplotlib"
] | 1,212 | 230 | 2 |
How to display an image I tried to use IPython.display with the following code:
```
from IPython.display import display, Image
display(Image(filename='MyImage.png'))
```
I also tried to use matplotlib with the following code:
```
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
plt.imshow(mpimg.imread('MyImage.png'))
```
In both cases, nothing is displayed, not even an error message.
|
If you are using matplotlib and want to show the image in your interactive notebook, try the following:
```
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread('your_image.png')
imgplot = plt.imshow(img)
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/35286540/how-to-display-an-image
| 388 |
[
"python",
"opencv",
"matplotlib",
"imshow"
] | 404 | 270 | 4 |
Plotting a list of (x, y) coordinates I have a list of pairs (a, b) that I would like to plot with matplotlib in python as actual x-y coordinates. Currently, it is making two plots, where the index of the list gives the x-coordinate, and the first plot's y values are the as in the pairs and the second plot's y values are the bs in the pairs. To clarify, my data looks like this: li = [(a,b), (c,d), ... , (t, u)] and I want to do a one-liner that just calls plt.plot(). If I didn't require a one-liner I could trivially do:
```py
xs = [x[0] for x in li]
ys = [x[1] for x in li]
plt.plot(xs, ys)
```
How can I get matplotlib to plot these pairs as x-y coordinates? Sample data
```py
# sample data
li = list(zip(range(1, 14), range(14, 27)))
li → [(1, 14), (2, 15), (3, 16), (4, 17), (5, 18), (6, 19), (7, 20), (8, 21), (9, 22), (10, 23), (11, 24), (12, 25), (13, 26)]
```
Incorrect Plot
```py
plt.plot(li)
plt.title('Incorrect Plot:\nEach index of the tuple plotted as separate lines')
```
Desired Plot This produces the correct plot, but to many lines of code are used to unpack li. I need to unpack and plot with a single line of code, not multiple list-comprehensions.
```py
xs = [x[0] for x in li]
ys = [x[1] for x in li]
plt.plot(xs, ys)
plt.title('Correct Plot:\nBut uses to many lines to unpack li')
```
|
Given li in the question:
```
li = list(zip(range(1, 14), range(14, 27)))
```
To unpack the data from pairs into lists use zip:
```
x, y = zip(*li)
x → (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
y → (14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26)
```
The one-liner uses the unpacking operator (*), to unpack the list of tuples for zip, and unpacks the zip object into the plot API.
```
plt.scatter(*zip(*li))
```
```
plt.plot(*zip(*li))
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/21519203/plotting-a-list-of-x-y-coordinates
| 280 |
[
"python",
"list",
"matplotlib",
"plot",
"coordinates"
] | 1,316 | 450 | 5 |
Plt.show shows full graph but savefig is cropping the image My code is succesfully saving images to file, but it is cropping important details from the right hand side. Answers exist for fixing this problem when it arises for plt.show, but it is the savefig command that is incorrectly producing the graph in this example. How can this be fixed? The relevant sample of my code:
```
import glob
import os
for file in glob.glob("*.oax"):
try:
spc_file = open(file, 'r').read()
newName = file[6:8] + '-' + file[4:6] + '-' + file[0:4] + ' ' + file[8:12] + ' UTC (Observed) - No Sea Breeze Day'
plt.title(newName, fontsize=12, loc='left')
plt.savefig('X:/' + newName + '.png')
plt.show()
except Exception:
pass
```
And the images (top is plt.show and bottom is file produced from savefig:
|
You may try
```
plt.savefig('X:/' + newName + '.png', bbox_inches='tight')
```
Or you may define figure size like
```
fig = plt.figure(figsize=(9, 11))
...
plt.savefig(filename, bbox_inches = 'tight')
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/37427362/plt-show-shows-full-graph-but-savefig-is-cropping-the-image
| 351 |
[
"python",
"matplotlib"
] | 849 | 206 | 2 |
How to rotate x-axis tick labels in a pandas plot With the following code:
```
import matplotlib
matplotlib.style.use('ggplot')
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({ 'celltype':["foo","bar","qux","woz"], 's1':[5,9,1,7], 's2':[12,90,13,87]})
df = df[["celltype","s1","s2"]]
df.set_index(["celltype"],inplace=True)
df.plot(kind='bar',alpha=0.75)
plt.xlabel("")
```
I made this plot: How can I rotate the x-axis tick labels to 0 degrees? I tried adding this but did not work:
```
plt.set_xticklabels(df.index,rotation=90)
```
|
Pass param rot=0 to rotate the xticklabels:
```
import matplotlib
matplotlib.style.use('ggplot')
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({ 'celltype':["foo","bar","qux","woz"], 's1':[5,9,1,7], 's2':[12,90,13,87]})
df = df[["celltype","s1","s2"]]
df.set_index(["celltype"],inplace=True)
df.plot(kind='bar',alpha=0.75, rot=0)
plt.xlabel("")
plt.show()
```
yields plot:
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/32244019/how-to-rotate-x-axis-tick-labels-in-a-pandas-plot
| 329 |
[
"python",
"pandas",
"matplotlib"
] | 559 | 398 | 3 |
Remove or adapt border of frame of legend using matplotlib When plotting a plot using matplotlib: How to remove the box of the legend? How to change the color of the border of the legend box? How to remove only the border of the box of the legend?
|
When plotting a plot using matplotlib: How to remove the box of the legend?
```
plt.legend(frameon=False)
```
How to change the color of the border of the legend box?
```
leg = plt.legend()
leg.get_frame().set_edgecolor('b')
```
How to remove only the border of the box of the legend?
```
leg = plt.legend()
leg.get_frame().set_linewidth(0.0)
```
For the matplotlib object oriented approach:
```
axes.legend(frameon=False)
leg = axes.legend()
leg.get_frame().set_edgecolor('b')
leg.get_frame().set_linewidth(0.0)
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/25540259/remove-or-adapt-border-of-frame-of-legend-using-matplotlib
| 327 |
[
"python",
"matplotlib"
] | 247 | 521 | 2 |
How to pick a new color for each plotted line within a figure I'd like to NOT specify a color for each plotted line, and have each line get a distinct color. But if I run:
```
from matplotlib import pyplot as plt
for i in range(20):
plt.plot([0, 1], [i, i])
plt.show()
```
then I get this output: If you look at the image above, you can see that matplotlib attempts to pick colors for each line that are different, but eventually it re-uses colors - the top ten lines use the same colors as the bottom ten. I just want to stop it from repeating already used colors AND/OR feed it a list of colors to use.
|
I usually use the second one of these:
```py
from matplotlib.pyplot import cm
import numpy as np
#variable n below should be number of curves to plot
#version 1:
color = cm.rainbow(np.linspace(0, 1, n))
for i, c in enumerate(color):
plt.plot(x, y, c=c)
#or version 2:
color = iter(cm.rainbow(np.linspace(0, 1, n)))
for i in range(n):
c = next(color)
plt.plot(x, y, c=c)
```
Example of 2:
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/4971269/how-to-pick-a-new-color-for-each-plotted-line-within-a-figure
| 223 |
[
"python",
"matplotlib",
"colormap"
] | 610 | 402 | 3 |
How to share x axes of two subplots after they have been created I'm trying to share two subplots axes, but I need to share the x axis after the figure was created. E.g. I create this figure:
```py
import numpy as np
import matplotlib.pyplot as plt
t = np.arange(1000)/100.
x = np.sin(2*np.pi*10*t)
y = np.cos(2*np.pi*10*t)
fig = plt.figure()
ax1 = plt.subplot(211)
plt.plot(t,x)
ax2 = plt.subplot(212)
plt.plot(t,y)
# some code to share both x axes
plt.show()
```
Instead of the comment I want to insert some code to share both x axes. How do I do this? There are some relevant sounding attributes _shared_x_axes and _shared_x_axes when I check to figure axis (fig.get_axes()) but I don't know how to link them.
|
The usual way to share axes is to create the shared properties at creation. Either
```
fig=plt.figure()
ax1 = plt.subplot(211)
ax2 = plt.subplot(212, sharex = ax1)
```
or
```
fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)
```
Sharing the axes after they have been created should therefore not be necessary. However if for any reason, you need to share axes after they have been created (actually, using a different library which creates some subplots, like here might be a reason), there would still be a solution: Using
```
ax2.sharex(ax1)
```
creates a link between the two axes, ax1 and ax2. In contrast to the sharing at creation time, you will have to set the xticklabels off manually for one of the axes (in case that is wanted). A complete example:
```
import numpy as np
import matplotlib.pyplot as plt
t= np.arange(1000)/100.
x = np.sin(2*np.pi*10*t)
y = np.cos(2*np.pi*10*t)
fig=plt.figure()
ax1 = plt.subplot(211)
ax2 = plt.subplot(212)
ax1.plot(t,x)
ax2.plot(t,y)
ax2.sharex(ax1)
ax1.set_xticklabels([])
# ax2.autoscale() ## call autoscale if needed
plt.show()
```
For a list of axes you would do:
```
for ax in axes[1:]:
ax.sharex(axes[0])
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/42973223/how-to-share-x-axes-of-two-subplots-after-they-have-been-created
| 265 |
[
"python",
"matplotlib",
"axis"
] | 717 | 1,176 | 3 |
How to connect scatterplot points with line using matplotlib I have two lists, dates and values. I want to plot them using matplotlib. The following creates a scatter plot of my data.
```py
import matplotlib.pyplot as plt
plt.scatter(dates,values)
plt.show()
```
plt.plot(dates, values) creates a line graph. But what I really want is a scatterplot where the points are connected by a line. Similar to in R:
```r
plot(dates, values)
lines(dates, value, type="l")
```
which gives me a scatterplot of points overlaid with a line connecting the points. How do I do this in python?
|
I think @Evert has the right answer:
```
plt.scatter(dates,values)
plt.plot(dates, values)
plt.show()
```
Which is pretty much the same as
```
plt.plot(dates, values, '-o')
plt.show()
```
You can replace -o with another suitable format string as described in the documentation. You can also split the choices of line and marker styles using the linestyle= and marker= keyword arguments.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/20130227/how-to-connect-scatterplot-points-with-line-using-matplotlib
| 224 |
[
"python",
"matplotlib",
"scatter-plot"
] | 580 | 388 | 3 |
How to plot multiple functions on the same figure How can I plot the following 3 functions (i.e. sin, cos and the addition), on the domain t, in the same figure?
```py
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0, 2*np.pi, 400)
a = np.sin(t)
b = np.cos(t)
c = a + b
```
|
To plot multiple graphs on the same figure you will have to do:
```
from numpy import *
import math
import matplotlib.pyplot as plt
t = linspace(0, 2*math.pi, 400)
a = sin(t)
b = cos(t)
c = a + b
plt.plot(t, a, 'r') # plotting t, a separately
plt.plot(t, b, 'b') # plotting t, b separately
plt.plot(t, c, 'g') # plotting t, c separately
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/22276066/how-to-plot-multiple-functions-on-the-same-figure
| 263 |
[
"python",
"function",
"matplotlib",
"plot",
"graph"
] | 296 | 357 | 5 |
Moving x-axis to the top of a plot in matplotlib Based on this question about heatmaps in matplotlib, I wanted to move the x-axis titles to the top of the plot.
```
import matplotlib.pyplot as plt
import numpy as np
column_labels = list('ABCD')
row_labels = list('WXYZ')
data = np.random.rand(4,4)
fig, ax = plt.subplots()
heatmap = ax.pcolor(data, cmap=plt.cm.Blues)
# put the major ticks at the middle of each cell
ax.set_xticks(np.arange(data.shape[0])+0.5, minor=False)
ax.set_yticks(np.arange(data.shape[1])+0.5, minor=False)
# want a more natural, table-like display
ax.invert_yaxis()
ax.xaxis.set_label_position('top') # <-- This doesn't work!
ax.set_xticklabels(row_labels, minor=False)
ax.set_yticklabels(column_labels, minor=False)
plt.show()
```
However, calling matplotlib's set_label_position (as notated above) doesn't seem to have the desired effect. Here's my output: What am I doing wrong?
|
Use
```
ax.xaxis.tick_top()
```
to place the tick marks at the top of the image. The command
```
ax.set_xlabel('X LABEL')
ax.xaxis.set_label_position('top')
```
affects the label, not the tick marks.
```
import matplotlib.pyplot as plt
import numpy as np
column_labels = list('ABCD')
row_labels = list('WXYZ')
data = np.random.rand(4, 4)
fig, ax = plt.subplots()
heatmap = ax.pcolor(data, cmap=plt.cm.Blues)
# put the major ticks at the middle of each cell
ax.set_xticks(np.arange(data.shape[1]) + 0.5, minor=False)
ax.set_yticks(np.arange(data.shape[0]) + 0.5, minor=False)
# want a more natural, table-like display
ax.invert_yaxis()
ax.xaxis.tick_top()
ax.set_xticklabels(column_labels, minor=False)
ax.set_yticklabels(row_labels, minor=False)
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/14406214/moving-x-axis-to-the-top-of-a-plot-in-matplotlib
| 242 |
[
"python",
"matplotlib",
"plot",
"visualization"
] | 910 | 770 | 4 |
Prevent scientific notation I have the following code:
```
plt.plot(range(2003,2012,1),range(200300,201200,100))
# several solutions from other questions have not worked, including
# plt.ticklabel_format(style='sci', axis='x', scilimits=(-1000000,1000000))
# ax.get_xaxis().get_major_formatter().set_useOffset(False)
plt.show()
```
which produces the following plot: How do I prevent scientific notation here? Is ticklabel_format broken? does not resolve the issue of actually removing the offset.
```py
plt.plot(np.arange(1e6, 3 * 1e7, 1e6))
plt.ticklabel_format(useOffset=False)
```
|
In your case, you're actually wanting to disable the offset. Using scientific notation is a separate setting from showing things in terms of an offset value. However, ax.ticklabel_format(useOffset=False) should have worked (though you've listed it as one of the things that didn't). For example:
```
fig, ax = plt.subplots()
ax.plot(range(2003,2012,1),range(200300,201200,100))
ax.ticklabel_format(useOffset=False)
plt.show()
```
If you want to disable both the offset and scientific notaion, you'd use ax.ticklabel_format(useOffset=False, style='plain'). Difference between "offset" and "scientific notation" In matplotlib axis formatting, "scientific notation" refers to a multiplier for the numbers show, while the "offset" is a separate term that is added. Consider this example:
```
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(1000, 1001, 100)
y = np.linspace(1e-9, 1e9, 100)
fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()
```
The x-axis will have an offset (note the + sign) and the y-axis will use scientific notation (as a multiplier -- No plus sign). We can disable either one separately. The most convenient way is the ax.ticklabel_format method (or plt.ticklabel_format). For example, if we call:
```
ax.ticklabel_format(style='plain')
```
We'll disable the scientific notation on the y-axis: And if we call
```
ax.ticklabel_format(useOffset=False)
```
We'll disable the offset on the x-axis, but leave the y-axis scientific notation untouched: Finally, we can disable both through:
```
ax.ticklabel_format(useOffset=False, style='plain')
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/28371674/prevent-scientific-notation
| 262 |
[
"python",
"matplotlib",
"plot",
"scientific-notation",
"xticks"
] | 586 | 1,586 | 5 |
How to maximize a plt.show() window Just for curiosity I would like to know how to do this in the code below. I have been searching for an answer but is useless.
```
import numpy as np
import matplotlib.pyplot as plt
data=np.random.exponential(scale=180, size=10000)
print ('el valor medio de la distribucion exponencial es: ')
print np.average(data)
plt.hist(data,bins=len(data)**0.5,normed=True, cumulative=True, facecolor='red', label='datos tamano paqutes acumulativa', alpha=0.5)
plt.legend()
plt.xlabel('algo')
plt.ylabel('algo')
plt.grid()
plt.show()
```
|
I am on a Windows (WIN7), running Python 2.7.5 & Matplotlib 1.3.1. I was able to maximize Figure windows for TkAgg, QT4Agg, and wxAgg using the following lines:
```py
from matplotlib import pyplot as plt
### for 'TkAgg' backend
plt.figure(1)
plt.switch_backend('TkAgg') #TkAgg (instead Qt4Agg)
print '#1 Backend:',plt.get_backend()
plt.plot([1,2,6,4])
mng = plt.get_current_fig_manager()
### works on Ubuntu??? >> did NOT working on windows
# mng.resize(*mng.window.maxsize())
mng.window.state('zoomed') #works fine on Windows!
plt.show() #close the figure to run the next section
### for 'wxAgg' backend
plt.figure(2)
plt.switch_backend('wxAgg')
print '#2 Backend:',plt.get_backend()
plt.plot([1,2,6,4])
mng = plt.get_current_fig_manager()
mng.frame.Maximize(True)
plt.show() #close the figure to run the next section
### for 'Qt4Agg' backend
plt.figure(3)
plt.switch_backend('QT4Agg') #default on my system
print '#3 Backend:',plt.get_backend()
plt.plot([1,2,6,4])
figManager = plt.get_current_fig_manager()
figManager.window.showMaximized()
plt.show()
```
if you want to maximize multiple figures you can use
```
for fig in figs:
mng = fig.canvas.manager
# ...
```
Hope this summary of the previous answers (and some additions) combined in a working example (at least for windows) helps.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/12439588/how-to-maximize-a-plt-show-window
| 212 |
[
"python",
"matplotlib"
] | 562 | 1,303 | 2 |
How to create major and minor gridlines with different linestyles I am currently using matplotlib.pyplot to create graphs and would like to have the major gridlines solid and black and the minor ones either greyed or dashed. In the grid properties, which=both/major/mine, and then color and linestyle are defined simply by linestyle. Is there a way to specify minor linestyle only? The appropriate code I have so far is
```
plt.plot(current, counts, 'rd', markersize=8)
plt.yscale('log')
plt.grid(b=True, which='both', color='0.65', linestyle='-')
```
|
Actually, it is as simple as setting major and minor separately:
```
```python
plot([23, 456, 676, 89, 906, 34, 2345])
#Output
#[<matplotlib.lines.Line2D at 0x6112f90>]
```
```python
yscale('log')
```
```python
grid(visible=True, which='major', color='b', linestyle='-')
```
```python
grid(visible=True, which='minor', color='r', linestyle='--')
```
The gotcha with minor grids is that you have to have minor tick marks turned on too. In the above code this is done by yscale('log'), but it can also be done with plt.minorticks_on(). Note: before matplotlib 3.5, visible parameter was named b
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/9127434/how-to-create-major-and-minor-gridlines-with-different-linestyles
| 230 |
[
"python",
"matplotlib",
"gridlines"
] | 552 | 578 | 3 |
Adding an arbitrary line to a matplotlib plot in ipython notebook I'm rather new to both python/matplotlib and using it through the ipython notebook. I'm trying to add some annotation lines to an existing graph and I can't figure out how to render the lines on a graph. So, for example, if I plot the following:
```
import numpy as np
np.random.seed(5)
x = arange(1, 101)
y = 20 + 3 * x + np.random.normal(0, 60, 100)
p = plot(x, y, "o")
```
I get the following graph: So how would I add a vertical line from (70,100) up to (70,250)? What about a diagonal line from (70,100) to (90,200)? I've tried a few things with Line2D() resulting in nothing but confusion on my part. In R I would simply use the segments() function which would add line segments. Is there an equivalent in matplotlib?
|
You can directly plot the lines you want by feeding the plot command with the corresponding data (boundaries of the segments): plot([x1, x2], [y1, y2], color='k', linestyle='-', linewidth=2) (of course you can choose the color, line width, line style, etc.) From your example:
```
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(5)
x = np.arange(1, 101)
y = 20 + 3 * x + np.random.normal(0, 60, 100)
plt.plot(x, y, "o")
# draw vertical line from (70,100) to (70, 250)
plt.plot([70, 70], [100, 250], 'k-', lw=2)
# draw diagonal line from (70, 90) to (90, 200)
plt.plot([70, 90], [90, 200], 'k-')
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/12864294/adding-an-arbitrary-line-to-a-matplotlib-plot-in-ipython-notebook
| 231 |
[
"matplotlib",
"ipython"
] | 791 | 633 | 2 |
How to display multiple images in one figure [duplicate] This question already has answers here: Multiple figures in a single window (7 answers) Closed 7 years ago. I am trying to display 20 random images on a single Figure. The images are indeed displayed, but they are overlaid. I am using:
```
import numpy as np
import matplotlib.pyplot as plt
w=10
h=10
fig=plt.figure()
for i in range(1,20):
img = np.random.randint(10, size=(h,w))
fig.add_subplot(i,2,1)
plt.imshow(img)
plt.show()
```
I would like them to appear naturally in a grid layout (say 4x5), each with the same size. Part of the problem is that I do not know what the arguments to add_subplot mean. The documentation states that the arguments are the number of rows, number of columns, and plot number. There is no positioning argument. Additionally, the plot number can only be 1 or 2. How can I achieve this?
|
Here is my approach that you may try:
```
import numpy as np
import matplotlib.pyplot as plt
w = 10
h = 10
fig = plt.figure(figsize=(8, 8))
columns = 4
rows = 5
for i in range(1, columns*rows +1):
img = np.random.randint(10, size=(h,w))
fig.add_subplot(rows, columns, i)
plt.imshow(img)
plt.show()
```
The resulting image: (Original answer date: Oct 7 '17 at 4:20) Edit 1 Since this answer is popular beyond my expectation. And I see that a small change is needed to enable flexibility for the manipulation of the individual plots. So that I offer this new version to the original code. In essence, it provides:- access to individual axes of subplots possibility to plot more features on selected axes/subplot New code:
```
import numpy as np
import matplotlib.pyplot as plt
w = 10
h = 10
fig = plt.figure(figsize=(9, 13))
columns = 4
rows = 5
# prep (x,y) for extra plotting
xs = np.linspace(0, 2*np.pi, 60) # from 0 to 2pi
ys = np.abs(np.sin(xs)) # absolute of sine
# ax enables access to manipulate each of subplots
ax = []
for i in range(columns*rows):
img = np.random.randint(10, size=(h,w))
# create subplot and append to ax
ax.append( fig.add_subplot(rows, columns, i+1) )
ax[-1].set_title("ax:"+str(i)) # set title
plt.imshow(img, alpha=0.25)
# do extra plots on selected axes/subplots
# note: index starts with 0
ax[2].plot(xs, 3*ys)
ax[19].plot(ys**2, xs)
plt.show() # finally, render the plot
```
The resulting plot: Edit 2 In the previous example, the code provides access to the sub-plots with single index, which is inconvenient when the figure has many rows/columns of sub-plots. Here is an alternative of it. The code below provides access to the sub-plots with [row_index][column_index], which is more suitable for manipulation of array of many sub-plots.
```
import matplotlib.pyplot as plt
import numpy as np
# settings
h, w = 10, 10 # for raster image
nrows, ncols = 5, 4 # array of sub-plots
figsize = [6, 8] # figure size, inches
# prep (x,y) for extra plotting on selected sub-plots
xs = np.linspace(0, 2*np.pi, 60) # from 0 to 2pi
ys = np.abs(np.sin(xs)) # absolute of sine
# create figure (fig), and array of axes (ax)
fig, ax = plt.subplots(nrows=nrows, ncols=ncols, figsize=figsize)
# plot simple raster image on each sub-plot
for i, axi in enumerate(ax.flat):
# i runs from 0 to (nrows*ncols-1)
# axi is equivalent with ax[rowid][colid]
img = np.random.randint(10, size=(h,w))
axi.imshow(img, alpha=0.25)
# get indices of row/column
rowid = i // ncols
colid = i % ncols
# write row/col indices as axes' title for identification
axi.set_title("Row:"+str(rowid)+", Col:"+str(colid))
# one can access the axes by ax[row_id][col_id]
# do additional plotting on ax[row_id][col_id] of your choice
ax[0][2].plot(xs, 3*ys, color='red', linewidth=3)
ax[4][3].plot(ys**2, xs, color='green', linewidth=3)
plt.tight_layout(True)
plt.show()
```
The resulting plot: Ticks and Tick-labels for Array of Subplots Some of the ticks and tick-labels accompanying the subplots can be hidden to get cleaner plot if all of the subplots share the same value ranges. All of the ticks and tick-labels can be hidden except for the outside edges on the left and bottom like this plot. To achieve the plot with only shared tick-labels on the left and bottom edges, you can do the following:- Add options sharex=True, sharey=True in fig, ax = plt.subplots() That line of code will become:
```
fig,ax=plt.subplots(nrows=nrows,ncols=ncols,figsize=figsize,sharex=True,sharey=True)
```
To specify required number of ticks, and labels to plot, inside the body of for i, axi in enumerate(ax.flat):, add these code
```
axi.xaxis.set_major_locator(plt.MaxNLocator(5))
axi.yaxis.set_major_locator(plt.MaxNLocator(4))
```
the number 5, and 4 are the number of ticks/tick_labels to plot. You may need other values that suit your plots.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/46615554/how-to-display-multiple-images-in-one-figure
| 332 |
[
"python",
"matplotlib",
"imshow"
] | 888 | 3,944 | 3 |
Can Pandas plot a histogram of dates? I've taken my Series and coerced it to a datetime column of dtype=datetime64[ns] (though only need day resolution...not sure how to change).
```
import pandas as pd
df = pd.read_csv('somefile.csv')
column = df['date']
column = pd.to_datetime(column, coerce=True)
```
but plotting doesn't work:
```
ipdb> column.plot(kind='hist')
*** TypeError: ufunc add cannot use operands with types dtype('<M8[ns]') and dtype('float64')
```
I'd like to plot a histogram that just shows the count of dates by week, month, or year. Surely there is a way to do this in pandas?
|
Given this df:
```
date
0 2001-08-10
1 2002-08-31
2 2003-08-29
3 2006-06-21
4 2002-03-27
5 2003-07-14
6 2004-06-15
7 2003-08-14
8 2003-07-29
```
and, if it's not already the case:
```
df["date"] = df["date"].astype("datetime64")
```
To show the count of dates by month:
```
df.groupby(df["date"].dt.month).count().plot(kind="bar")
```
.dt allows you to access the datetime properties. Which will give you: You can replace month by year, day, etc.. If you want to distinguish year and month for instance, just do:
```
df.groupby([df["date"].dt.year, df["date"].dt.month]).count().plot(kind="bar")
```
Which gives:
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/27365467/can-pandas-plot-a-histogram-of-dates
| 232 |
[
"python",
"pandas",
"matplotlib",
"time-series"
] | 599 | 616 | 4 |
Pandas plot doesn't show When using this in a script (not IPython), nothing happens, i.e. the plot window doesn't appear :
```
import numpy as np
import pandas as pd
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts.plot()
```
Even when adding time.sleep(5), there is still nothing. Why? Is there a way to do it, without having to manually call matplotlib ?
|
Once you have made your plot, you need to tell matplotlib to show it. The usual way to do things is to import matplotlib.pyplot and call show from there:
```
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts.plot()
plt.show()
```
In older versions of pandas, you were able to find a backdoor to matplotlib, as in the example below. NOTE: This no longer works in modern versions of pandas, and I still recommend importing matplotlib separately, as in the example above.
```
import numpy as np
import pandas as pd
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts.plot()
pd.tseries.plotting.pylab.show()
```
But all you are doing there is finding somewhere that matplotlib has been imported in pandas, and calling the same show function from there. Are you trying to avoid calling matplotlib in an effort to speed things up? If so then you are really not speeding anything up, since pandas already imports pyplot:
```
python -mtimeit -s 'import pandas as pd'
100000000 loops, best of 3: 0.0122 usec per loop
python -mtimeit -s 'import pandas as pd; import matplotlib.pyplot as plt'
100000000 loops, best of 3: 0.0125 usec per loop
```
Finally, the reason the example you linked in comments doesn't need the call to matplotlib is because it is being run interactively in an iPython notebook, not in a script.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/34347145/pandas-plot-doesnt-show
| 241 |
[
"python",
"pandas",
"matplotlib"
] | 396 | 1,456 | 3 |
Matplotlib figure facecolor (background color) Can someone please explain why the code below does not work when setting the facecolor of the figure?
```
import matplotlib.pyplot as plt
# create figure instance
fig1 = plt.figure(1)
fig1.set_figheight(11)
fig1.set_figwidth(8.5)
rect = fig1.patch
rect.set_facecolor('red') # works with plt.show().
# Does not work with plt.savefig("trial_fig.png")
ax = fig1.add_subplot(1,1,1)
x = 1, 2, 3
y = 1, 4, 9
ax.plot(x, y)
# plt.show() # Will show red face color set above using rect.set_facecolor('red')
plt.savefig("trial_fig.png") # The saved trial_fig.png DOES NOT have the red facecolor.
# plt.savefig("trial_fig.png", facecolor='red') # Here the facecolor is red.
```
When I specify the height and width of the figure using fig1.set_figheight(11) fig1.set_figwidth(8.5) these are picked up by the command plt.savefig("trial_fig.png"). However, the facecolor setting is not picked up. Why? Thanks for your help.
|
It's because savefig overrides the facecolor for the background of the figure. (This is deliberate, actually... The assumption is that you'd probably want to control the background color of the saved figure with the facecolor kwarg to savefig. It's a confusing and inconsistent default, though!) The easiest workaround is just to do fig.savefig('whatever.png', facecolor=fig.get_facecolor(), edgecolor='none') (I'm specifying the edgecolor here because the default edgecolor for the actual figure is white, which will give you a white border around the saved figure)
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/4804005/matplotlib-figure-facecolor-background-color
| 197 |
[
"python",
"matplotlib"
] | 993 | 566 | 2 |
What is the difference between 'log' and 'symlog'? In matplotlib, I can set the axis scaling using either pyplot.xscale() or Axes.set_xscale(). Both functions accept three different scales: 'linear' | 'log' | 'symlog'. What is the difference between 'log' and 'symlog'? In a simple test I did, they both looked exactly the same. I know the documentation says they accept different parameters, but I still don't understand the difference between them. Can someone please explain it? The answer will be the best if it has some sample code and graphics! (also: where does the name 'symlog' come from?)
|
I finally found some time to do some experiments in order to understand the difference between them. Here's what I discovered: log only allows positive values, and lets you choose how to handle negative ones (mask or clip). symlog means symmetrical log, and allows positive and negative values. symlog allows to set a range around zero within the plot will be linear instead of logarithmic. I think everything will get a lot easier to understand with graphics and examples, so let's try them:
```
import numpy
from matplotlib import pyplot
# Enable interactive mode
pyplot.ion()
# Draw the grid lines
pyplot.grid(True)
# Numbers from -50 to 50, with 0.1 as step
xdomain = numpy.arange(-50,50, 0.1)
# Plots a simple linear function 'f(x) = x'
pyplot.plot(xdomain, xdomain)
# Plots 'sin(x)'
pyplot.plot(xdomain, numpy.sin(xdomain))
# 'linear' is the default mode, so this next line is redundant:
pyplot.xscale('linear')
```
```
# How to treat negative values?
# 'mask' will treat negative values as invalid
# 'mask' is the default, so the next two lines are equivalent
pyplot.xscale('log')
pyplot.xscale('log', nonposx='mask')
```
```
# 'clip' will map all negative values a very small positive one
pyplot.xscale('log', nonposx='clip')
```
```
# 'symlog' scaling, however, handles negative values nicely
pyplot.xscale('symlog')
```
```
# And you can even set a linear range around zero
pyplot.xscale('symlog', linthreshx=20)
```
Just for completeness, I've used the following code to save each figure:
```
# Default dpi is 80
pyplot.savefig('matplotlib_xscale_linear.png', dpi=50, bbox_inches='tight')
```
Remember you can change the figure size using:
```
fig = pyplot.gcf()
fig.set_size_inches([4., 3.])
# Default size: [8., 6.]
```
(If you are unsure about me answering my own question, read this)
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/3305865/what-is-the-difference-between-log-and-symlog
| 245 |
[
"python",
"matplotlib",
"scale",
"logarithm"
] | 598 | 1,806 | 4 |
matplotlib colorbar for scatter I'm working with data that has the data has 3 plotting parameters: x,y,c. How do you create a custom color value for a scatter plot? Extending this example I'm trying to do:
```
import matplotlib
import matplotlib.pyplot as plt
cm = matplotlib.cm.get_cmap('RdYlBu')
colors=[cm(1.*i/20) for i in range(20)]
xy = range(20)
plt.subplot(111)
colorlist=[colors[x/2] for x in xy] #actually some other non-linear relationship
plt.scatter(xy, xy, c=colorlist, s=35, vmin=0, vmax=20)
plt.colorbar()
plt.show()
```
but the result is TypeError: You must first set_array for mappable
|
From the matplotlib docs on scatter 1: cmap is only used if c is an array of floats So colorlist needs to be a list of floats rather than a list of tuples as you have it now. plt.colorbar() wants a mappable object, like the CircleCollection that plt.scatter() returns. vmin and vmax can then control the limits of your colorbar. Things outside vmin/vmax get the colors of the endpoints. How does this work for you?
```
import matplotlib.pyplot as plt
cm = plt.cm.get_cmap('RdYlBu')
xy = range(20)
z = xy
sc = plt.scatter(xy, xy, c=z, vmin=0, vmax=20, s=35, cmap=cm)
plt.colorbar(sc)
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/6063876/matplotlib-colorbar-for-scatter
| 264 |
[
"python",
"colors",
"matplotlib"
] | 604 | 598 | 3 |
Plot a bar using matplotlib using a dictionary Is there any way to plot a bar plot using matplotlib using data directly from a dict? My dict looks like this:
```
D = {u'Label1':26, u'Label2': 17, u'Label3':30}
```
I was expecting
```
fig = plt.figure(figsize=(5.5,3),dpi=300)
ax = fig.add_subplot(111)
bar = ax.bar(D,range(1,len(D)+1,1),0.5)
```
to work, but it does not. Here is the error:
```
```python
ax.bar(D,range(1,len(D)+1,1),0.5)
#Output
#Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# File "/usr/local/lib/python2.7/site-packages/matplotlib/axes.py", line 4904, in bar
# self.add_patch(r)
# File "/usr/local/lib/python2.7/site-packages/matplotlib/axes.py", line 1570, in add_patch
# self._update_patch_limits(p)
# File "/usr/local/lib/python2.7/site-packages/matplotlib/axes.py", line 1588, in _update_patch_limits
# xys = patch.get_patch_transform().transform(vertices)
# File "/usr/local/lib/python2.7/site-packages/matplotlib/patches.py", line 580, in get_patch_transform
# self._update_patch_transform()
# File "/usr/local/lib/python2.7/site-packages/matplotlib/patches.py", line 576, in _update_patch_transform
# bbox = transforms.Bbox.from_bounds(x, y, width, height)
# File "/usr/local/lib/python2.7/site-packages/matplotlib/transforms.py", line 786, in from_bounds
# return Bbox.from_extents(x0, y0, x0 + width, y0 + height)
#TypeError: coercing to Unicode: need string or buffer, float found
#```
```
|
You can do it in two lines by first plotting the bar chart and then setting the appropriate ticks:
```
import matplotlib.pyplot as plt
D = {u'Label1':26, u'Label2': 17, u'Label3':30}
plt.bar(range(len(D)), list(D.values()), align='center')
plt.xticks(range(len(D)), list(D.keys()))
# # for python 2.x:
# plt.bar(range(len(D)), D.values(), align='center') # python 2.x
# plt.xticks(range(len(D)), D.keys()) # in python 2.x
plt.show()
```
Note that the penultimate line should read plt.xticks(range(len(D)), list(D.keys())) in python3, because D.keys() returns a generator, which matplotlib cannot use directly.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/16010869/plot-a-bar-using-matplotlib-using-a-dictionary
| 208 |
[
"python",
"matplotlib",
"plot"
] | 1,441 | 615 | 3 |
OpenCV giving wrong color to colored images on loading I'm loading in a color image in Python OpenCV and plotting the same. However, the image I get has it's colors all mixed up. Here is the code:
```
import cv2
import numpy as np
from numpy import array, arange, uint8
from matplotlib import pyplot as plt
img = cv2.imread('lena_caption.png', cv2.IMREAD_COLOR)
bw_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
images = []
images.append(img)
images.append(bw_img)
titles = ['Original Image','BW Image']
for i in xrange(len(images)):
plt.subplot(1,2,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
```
Here is the original image: And here is the plotted image:
|
OpenCV uses BGR as its default colour order for images, matplotlib uses RGB. When you display an image loaded with OpenCv in matplotlib the channels will be back to front. The easiest way of fixing this is to use OpenCV to explicitly convert it back to RGB, much like you do when creating the greyscale image.
```
RGB_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
```
And then use that in your plot.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/39316447/opencv-giving-wrong-color-to-colored-images-on-loading
| 258 |
[
"python",
"opencv",
"matplotlib",
"colors",
"rgb"
] | 725 | 397 | 5 |
plot with custom text for x axis points I am drawing a plot using matplotlib and python like the sample code below.
```
x = array([0,1,2,3])
y = array([20,21,22,23])
plot(x,y)
show()
```
As it is the code above on the x axis I will see drawn values 0.0, 0.5, 1.0, 1.5 i.e. the same values of my reference x values. Is there anyway to map each point of x to a different string? So for example I want x axis to show months names( strings Jun, July,...) or other strings like people names ( "John", "Arnold", ... ) or clock time ( "12:20", "12:21", "12:22", .. ). Do you know what I can do or what function to have a look at? For my purpose could it be matplotlib.ticker of help?
|
You can manually set xticks (and yticks) using pyplot.xticks:
```
import matplotlib.pyplot as plt
import numpy as np
x = np.array([0,1,2,3])
y = np.array([20,21,22,23])
my_xticks = ['John','Arnold','Mavis','Matt']
plt.xticks(x, my_xticks)
plt.plot(x, y)
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/3100985/plot-with-custom-text-for-x-axis-points
| 260 |
[
"python",
"matplotlib"
] | 677 | 270 | 2 |
Scatter plot and Color mapping in Python I have a range of points x and y stored in numpy arrays. Those represent x(t) and y(t) where t=0...T-1 I am plotting a scatter plot using
```
import matplotlib.pyplot as plt
plt.scatter(x,y)
plt.show()
```
I would like to have a colormap representing the time (therefore coloring the points depending on the index in the numpy arrays) What is the easiest way to do so?
|
Here is an example
```
import numpy as np
import matplotlib.pyplot as plt
x = np.random.rand(100)
y = np.random.rand(100)
t = np.arange(100)
plt.scatter(x, y, c=t)
plt.show()
```
Here you are setting the color based on the index, t, which is just an array of [1, 2, ..., 100]. Perhaps an easier-to-understand example is the slightly simpler
```
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(100)
y = x
t = x
plt.scatter(x, y, c=t)
plt.show()
```
Note that the array you pass as c doesn't need to have any particular order or type, i.e. it doesn't need to be sorted or integers as in these examples. The plotting routine will scale the colormap such that the minimum/maximum values in c correspond to the bottom/top of the colormap. Colormaps You can change the colormap by adding
```
import matplotlib.cm as cm
plt.scatter(x, y, c=t, cmap=cm.cmap_name)
```
Importing matplotlib.cm is optional as you can call colormaps as cmap="cmap_name" just as well. There is a reference page of colormaps showing what each looks like. Also know that you can reverse a colormap by simply calling it as cmap_name_r. So either
```
plt.scatter(x, y, c=t, cmap=cm.cmap_name_r)
# or
plt.scatter(x, y, c=t, cmap="cmap_name_r")
```
will work. Examples are "jet_r" or cm.plasma_r. Here's an example with the new 1.5 colormap viridis:
```
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(100)
y = x
t = x
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.scatter(x, y, c=t, cmap='viridis')
ax2.scatter(x, y, c=t, cmap='viridis_r')
plt.show()
```
Colorbars You can add a colorbar by using
```
plt.scatter(x, y, c=t, cmap='viridis')
plt.colorbar()
plt.show()
```
Note that if you are using figures and subplots explicitly (e.g. fig, ax = plt.subplots() or ax = fig.add_subplot(111)), adding a colorbar can be a bit more involved. Good examples can be found here for a single subplot colorbar and here for 2 subplots 1 colorbar.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/17682216/scatter-plot-and-color-mapping-in-python
| 241 |
[
"python",
"matplotlib"
] | 411 | 1,938 | 2 |
Change figure window title in pylab How can I set a figure window's title in pylab/python?
```
fig = figure(9) # 9 is now the title of the window
fig.set_title("Test") #doesn't work
fig.title = "Test" #doesn't work
```
|
If you want to actually change the window you can do:
```
fig = pylab.gcf()
fig.canvas.manager.set_window_title('Test')
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/5812960/change-figure-window-title-in-pylab
| 185 |
[
"python",
"matplotlib"
] | 219 | 124 | 2 |
Defining the midpoint of a colormap in matplotlib I want to set the middle point of a colormap, i.e., my data goes from -5 to 10 and I want zero to be the middle point. I think the way to do it is by subclassing normalize and using the norm, but I didn't find any example and it is not clear to me, what exactly have I to implement?
|
I know this is late to the game, but I just went through this process and came up with a solution that perhaps less robust than subclassing normalize, but much simpler. I thought it'd be good to share it here for posterity. The function
```
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import AxesGrid
def shiftedColorMap(cmap, start=0, midpoint=0.5, stop=1.0, name='shiftedcmap'):
'''
Function to offset the "center" of a colormap. Useful for
data with a negative min and positive max and you want the
middle of the colormap's dynamic range to be at zero.
Input
-----
cmap : The matplotlib colormap to be altered
start : Offset from lowest point in the colormap's range.
Defaults to 0.0 (no lower offset). Should be between
0.0 and `midpoint`.
midpoint : The new center of the colormap. Defaults to
0.5 (no shift). Should be between 0.0 and 1.0. In
general, this should be 1 - vmax / (vmax + abs(vmin))
For example if your data range from -15.0 to +5.0 and
you want the center of the colormap at 0.0, `midpoint`
should be set to 1 - 5/(5 + 15)) or 0.75
stop : Offset from highest point in the colormap's range.
Defaults to 1.0 (no upper offset). Should be between
`midpoint` and 1.0.
'''
cdict = {
'red': [],
'green': [],
'blue': [],
'alpha': []
}
# regular index to compute the colors
reg_index = np.linspace(start, stop, 257)
# shifted index to match the data
shift_index = np.hstack([
np.linspace(0.0, midpoint, 128, endpoint=False),
np.linspace(midpoint, 1.0, 129, endpoint=True)
])
for ri, si in zip(reg_index, shift_index):
r, g, b, a = cmap(ri)
cdict['red'].append((si, r, r))
cdict['green'].append((si, g, g))
cdict['blue'].append((si, b, b))
cdict['alpha'].append((si, a, a))
newcmap = matplotlib.colors.LinearSegmentedColormap(name, cdict)
plt.register_cmap(cmap=newcmap)
return newcmap
```
An example
```
biased_data = np.random.random_integers(low=-15, high=5, size=(37,37))
orig_cmap = matplotlib.cm.coolwarm
shifted_cmap = shiftedColorMap(orig_cmap, midpoint=0.75, name='shifted')
shrunk_cmap = shiftedColorMap(orig_cmap, start=0.15, midpoint=0.75, stop=0.85, name='shrunk')
fig = plt.figure(figsize=(6,6))
grid = AxesGrid(fig, 111, nrows_ncols=(2, 2), axes_pad=0.5,
label_mode="1", share_all=True,
cbar_location="right", cbar_mode="each",
cbar_size="7%", cbar_pad="2%")
# normal cmap
im0 = grid[0].imshow(biased_data, interpolation="none", cmap=orig_cmap)
grid.cbar_axes[0].colorbar(im0)
grid[0].set_title('Default behavior (hard to see bias)', fontsize=8)
im1 = grid[1].imshow(biased_data, interpolation="none", cmap=orig_cmap, vmax=15, vmin=-15)
grid.cbar_axes[1].colorbar(im1)
grid[1].set_title('Centered zero manually,\nbut lost upper end of dynamic range', fontsize=8)
im2 = grid[2].imshow(biased_data, interpolation="none", cmap=shifted_cmap)
grid.cbar_axes[2].colorbar(im2)
grid[2].set_title('Recentered cmap with function', fontsize=8)
im3 = grid[3].imshow(biased_data, interpolation="none", cmap=shrunk_cmap)
grid.cbar_axes[3].colorbar(im3)
grid[3].set_title('Recentered cmap with function\nand shrunk range', fontsize=8)
for ax in grid:
ax.set_yticks([])
ax.set_xticks([])
```
Results of the example:
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/7404116/defining-the-midpoint-of-a-colormap-in-matplotlib
| 101 |
[
"python",
"matplotlib",
"colormap"
] | 332 | 3,530 | 3 |
Change grid interval and specify tick labels I am trying to plot counts in gridded plots, but I haven't been able to figure out how to go about it. I want: to have dotted grids at an interval of 5; to have major tick labels only every 20; for the ticks to be outside the plot; and to have "counts" inside those grids. I have checked for potential duplicates, such as here and here, but have not been able to figure it out. This is my code:
```py
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
for x, y, count in data.values():
fig = plt.figure()
ax = fig.add_subplot(111)
ax.annotate(count, xy = (x, y), size = 5)
# overwrites and I only get the last data point
plt.close()
# Without this, I get a "fail to allocate bitmap" error.
plt.suptitle('Number of counts', fontsize = 12)
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.axes().set_aspect('equal')
plt.axis([0, 1000, 0, 1000])
# This gives an interval of 200.
majorLocator = MultipleLocator(20)
majorFormatter = FormatStrFormatter('%d')
minorLocator = MultipleLocator(5)
# I want the minor grid to be 5 and the major grid to be 20.
plt.grid()
```
This is what I get.
|
There are several problems in your code. First the big ones: You are creating a new figure and a new axes in every iteration of your loop → put fig = plt.figure and ax = fig.add_subplot(1,1,1) outside of the loop. Don't use the Locators. Call the functions ax.set_xticks() and ax.grid() with the correct keywords. With plt.axes() you are creating a new axes again. Use ax.set_aspect('equal'). The minor things: You should not mix the MATLAB-like syntax like plt.axis() with the objective syntax. Use ax.set_xlim(a,b) and ax.set_ylim(a,b) This should be a working minimal example:
```
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
# Major ticks every 20, minor ticks every 5
major_ticks = np.arange(0, 101, 20)
minor_ticks = np.arange(0, 101, 5)
ax.set_xticks(major_ticks)
ax.set_xticks(minor_ticks, minor=True)
ax.set_yticks(major_ticks)
ax.set_yticks(minor_ticks, minor=True)
# And a corresponding grid
ax.grid(which='both')
# Or if you want different settings for the grids:
ax.grid(which='minor', alpha=0.2)
ax.grid(which='major', alpha=0.5)
plt.show()
```
Output is this:
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/24943991/change-grid-interval-and-specify-tick-labels
| 280 |
[
"python",
"matplotlib",
"xticks",
"gridlines",
"yticks"
] | 1,203 | 1,136 | 5 |
How to set xticks in subplots If I plot a single imshow plot I can use
```py
fig, ax = plt.subplots()
ax.imshow(data)
plt.xticks( [4, 14, 24], [5, 15, 25] )
```
to replace my xtick labels. Now, I am plotting 12 imshow plots using
```py
f, axarr = plt.subplots(4, 3)
axarr[i, j].imshow(data)
```
How can I change xticks just for one of these subplots? I can only access the axes of the subplots with axarr[i, j]. How can I access plt just for one particular subplot?
|
There are two ways: Use the axes methods of the subplot object (e.g. ax.set_xticks and ax.set_xticklabels) or Use plt.sca to set the current axes for the pyplot state machine (i.e. the plt interface). As an example (this also illustrates using setp to change the properties of all of the subplots):
```
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=3, ncols=4)
# Set the ticks and ticklabels for all axes
plt.setp(axes, xticks=[0.1, 0.5, 0.9], xticklabels=['a', 'b', 'c'],
yticks=[1, 2, 3])
# Use the pyplot interface to change just one subplot...
plt.sca(axes[1, 1])
plt.xticks(range(3), ['A', 'Big', 'Cat'], color='red')
fig.tight_layout()
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/19626530/how-to-set-xticks-in-subplots
| 221 |
[
"python",
"matplotlib",
"subplot"
] | 468 | 686 | 3 |
Getting vertical gridlines to appear in line plot in matplotlib I want to get both horizontal and vertical grid lines on my plot but only the horizontal grid lines are appearing by default. I am using a pandas.DataFrame from an sql query in python to generate a line plot with dates on the x-axis. I'm not sure why they do not appear on the dates and I have tried to search for an answer to this but couldn't find one. All I have used to plot the graph is the simple code below.
```
data.plot()
grid('on')
```
data is the DataFrame which contains the dates and the data from the sql query. I have also tried adding the code below but I still get the same output with no vertical grid lines.
```
ax = plt.axes()
ax.yaxis.grid() # horizontal lines
ax.xaxis.grid() # vertical lines
```
Any suggestions?
|
You may need to give boolean arg in your calls, e.g. use ax.yaxis.grid(True) instead of ax.yaxis.grid(). Additionally, since you are using both of them you can combine into ax.grid, which works on both, rather than doing it once for each dimension.
```
ax = plt.gca()
ax.grid(True)
```
That should sort you out.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/16074392/getting-vertical-gridlines-to-appear-in-line-plot-in-matplotlib
| 127 |
[
"python",
"matplotlib",
"pandas"
] | 809 | 312 | 3 |
prevent plot from showing in jupyter notebook How can I prevent a specific plot to be shown in Jupyter notebook? I have several plots in a notebook but I want a subset of them to be saved to a file and not shown on the notebook as this slows considerably. A minimal working example for a Jupyter notebook is:
```
%matplotlib inline
from numpy.random import randn
from matplotlib.pyplot import plot, figure
a=randn(3)
b=randn(3)
for i in range(10):
fig=figure()
plot(b)
fname='s%03d.png'%i
fig.savefig(fname)
if(i%5==0):
figure()
plot(a)
```
As you can see I have two types of plots, a and b. I want a's to be plotted and shown and I don't want the b plots to be shown, I just want them them to be saved in a file. Hopefully this will speed things a bit and won't pollute my notebook with figures I don't need to see. Thank you for your time
|
Perhaps just clear the axis, for example:
```
fig = plt.figure()
plt.plot(range(10))
fig.savefig("save_file_name.pdf")
plt.close()
```
This will not plot the output in inline mode. I can't work out if it is really clearing the data though.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/18717877/prevent-plot-from-showing-in-jupyter-notebook
| 163 |
[
"python",
"matplotlib",
"jupyter-notebook",
"figures"
] | 878 | 240 | 4 |
How to generate random colors in matplotlib? What's the trivial example of how to generate random colors for passing to plotting functions? I'm calling scatter inside a loop and want each plot a different color.
```
for X,Y in data:
scatter(X, Y, c=??)
```
c: a color. c can be a single color format string, or a sequence of color specifications of length N, or a sequence of N numbers to be mapped to colors using the cmap and norm specified via kwargs (see below). Note that c should not be a single numeric RGB or RGBA sequence because that is indistinguishable from an array of values to be colormapped. c can be a 2-D array in which the rows are RGB or RGBA, however.
|
I'm calling scatter inside a loop and want each plot in a different color. Based on that, and on your answer: It seems to me that you actually want n distinct colors for your datasets; you want to map the integer indices 0, 1, ..., n-1 to distinct RGB colors. Something like: Here is the function to do it:
```
import matplotlib.pyplot as plt
def get_cmap(n, name='hsv'):
'''Returns a function that maps each index in 0, 1, ..., n-1 to a distinct
RGB color; the keyword argument name must be a standard mpl colormap name.'''
return plt.cm.get_cmap(name, n)
```
Usage in your pseudo-code snippet in the question:
```
cmap = get_cmap(len(data))
for i, (X, Y) in enumerate(data):
scatter(X, Y, c=cmap(i))
```
I generated the figure in my answer with the following code:
```
import matplotlib.pyplot as plt
def get_cmap(n, name='hsv'):
'''Returns a function that maps each index in 0, 1, ..., n-1 to a distinct
RGB color; the keyword argument name must be a standard mpl colormap name.'''
return plt.cm.get_cmap(name, n)
def main():
N = 30
fig=plt.figure()
ax=fig.add_subplot(111)
plt.axis('scaled')
ax.set_xlim([ 0, N])
ax.set_ylim([-0.5, 0.5])
cmap = get_cmap(N)
for i in range(N):
rect = plt.Rectangle((i, -0.5), 1, 1, facecolor=cmap(i))
ax.add_artist(rect)
ax.set_yticks([])
plt.show()
if __name__=='__main__':
main()
```
Tested with both Python 2.7 & matplotlib 1.5, and with Python 3.5 & matplotlib 2.0. It works as expected.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/14720331/how-to-generate-random-colors-in-matplotlib
| 201 |
[
"python",
"matplotlib"
] | 676 | 1,526 | 2 |
Plotting images side by side using matplotlib I was wondering how I am able to plot images side by side using matplotlib for example something like this: The closest I got is this: This was produced by using this code:
```
f, axarr = plt.subplots(2,2)
axarr[0,0] = plt.imshow(image_datas[0])
axarr[0,1] = plt.imshow(image_datas[1])
axarr[1,0] = plt.imshow(image_datas[2])
axarr[1,1] = plt.imshow(image_datas[3])
```
But I can't seem to get the other images to show. I'm thinking that there must be a better way to do this as I would imagine trying to manage the indexes would be a pain. I have looked through the documentation although I have a feeling I may be look at the wrong one. Would anyone be able to provide me with an example or point me in the right direction? EDIT: See the answer from @duhaime if you want a function to automatically determine the grid size.
|
The problem you face is that you try to assign the return of imshow (which is an matplotlib.image.AxesImage to an existing axes object. The correct way of plotting image data to the different axes in axarr would be
```
f, axarr = plt.subplots(2,2)
axarr[0,0].imshow(image_datas[0])
axarr[0,1].imshow(image_datas[1])
axarr[1,0].imshow(image_datas[2])
axarr[1,1].imshow(image_datas[3])
```
The concept is the same for all subplots, and in most cases the axes instance provide the same methods than the pyplot (plt) interface. E.g. if ax is one of your subplot axes, for plotting a normal line plot you'd use ax.plot(..) instead of plt.plot(). This can actually be found exactly in the source from the page you link to.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/41793931/plotting-images-side-by-side-using-matplotlib
| 182 |
[
"python",
"matplotlib"
] | 872 | 717 | 2 |
Changing the color of an axis Is there a way to change the color of an axis (not the ticks) in matplotlib? I have been looking through the docs for Axes, Axis, and Artist, but no luck; the matplotlib gallery also has no hint. Any idea?
|
When using figures, you can easily change the spine color with:
```
ax.spines['bottom'].set_color('#dddddd')
ax.spines['top'].set_color('#dddddd')
ax.spines['right'].set_color('red')
ax.spines['left'].set_color('red')
```
Use the following to change only the ticks: which="both" changes both the major and minor tick colors
```py
ax.tick_params(axis='x', colors='red')
ax.tick_params(axis='y', colors='red')
```
And the following to change only the label:
```
ax.yaxis.label.set_color('red')
ax.xaxis.label.set_color('red')
```
And finally the title:
```
ax.title.set_color('red')
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/1982770/changing-the-color-of-an-axis
| 237 |
[
"python",
"matplotlib",
"axis"
] | 235 | 589 | 3 |
Histogram Matplotlib So I have a little problem. I have a data set in scipy that is already in the histogram format, so I have the center of the bins and the number of events per bin. How can I now plot is as a histogram. I tried just doing
```
bins, n=hist()
```
but it didn't like that. Any recommendations?
|
```
import matplotlib.pyplot as plt
import numpy as np
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
hist, bins = np.histogram(x, bins=50)
width = 0.7 * (bins[1] - bins[0])
center = (bins[:-1] + bins[1:]) / 2
plt.bar(center, hist, align='center', width=width)
plt.show()
```
The object-oriented interface is also straightforward:
```
fig, ax = plt.subplots()
ax.bar(center, hist, align='center', width=width)
fig.savefig("1.png")
```
If you are using custom (non-constant) bins, you can pass compute the widths using np.diff, pass the widths to ax.bar and use ax.set_xticks to label the bin edges:
```
import matplotlib.pyplot as plt
import numpy as np
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
bins = [0, 40, 60, 75, 90, 110, 125, 140, 160, 200]
hist, bins = np.histogram(x, bins=bins)
width = np.diff(bins)
center = (bins[:-1] + bins[1:]) / 2
fig, ax = plt.subplots(figsize=(8,3))
ax.bar(center, hist, align='center', width=width)
ax.set_xticks(bins)
fig.savefig("/tmp/out.png")
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/5328556/histogram-matplotlib
| 268 |
[
"python",
"numpy",
"matplotlib",
"scipy",
"histogram"
] | 310 | 1,033 | 5 |
Linear regression with matplotlib / numpy I'm trying to generate a linear regression on a scatter plot I have generated, however my data is in list format, and all of the examples I can find of using polyfit require using arange. arange doesn't accept lists though. I have searched high and low about how to convert a list to an array and nothing seems clear. Am I missing something? Following on, how best can I use my list of integers as inputs to the polyfit? Here is the polyfit example I am following:
```
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(data)
y = np.arange(data)
m, b = np.polyfit(x, y, 1)
plt.plot(x, y, 'yo', x, m*x+b, '--k')
plt.show()
```
|
arange generates lists (well, numpy arrays); type help(np.arange) for the details. You don't need to call it on existing lists.
```py
```python
x = [1,2,3,4]
```
```python
y = [3,5,7,9]
```
```python
```
```python
m,b = np.polyfit(x, y, 1)
```
```python
m
#Output
#2.0000000000000009
```
```python
b
#Output
#0.99999999999999833
#```
#I should add that I tend to use poly1d here rather than write out "m*x+b" and the higher-order equivalents, so my version of your code would look something like this:
#```py
#import numpy as np
#import matplotlib.pyplot as plt
```
x = [1,2,3,4]
y = [3,5,7,10] # 10, not 9, so the fit isn't perfect
coef = np.polyfit(x,y,1)
poly1d_fn = np.poly1d(coef)
# poly1d_fn is now a function which takes in x and returns an estimate for y
plt.plot(x,y, 'yo', x, poly1d_fn(x), '--k') #'--k'=black dashed line, 'yo' = yellow circle marker
plt.xlim(0, 5)
plt.ylim(0, 12)
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/6148207/linear-regression-with-matplotlib-numpy
| 246 |
[
"python",
"numpy",
"matplotlib",
"linear-regression",
"curve-fitting"
] | 685 | 821 | 5 |
Stop matplotlib repeating labels in legend Here is a very simplified example:
```
xvalues = [2,3,4,6]
for x in xvalues:
plt.axvline(x,color='b',label='xvalues')
plt.legend()
```
The legend will now show 'xvalues' as a blue line 4 times in the legend. Is there a more elegant way of fixing this than the following?
```
for i,x in enumerate(xvalues):
if not i:
plt.axvline(x,color='b',label='xvalues')
else:
plt.axvline(x,color='b')
```
|
plt.legend takes as parameters A list of axis handles which are Artist objects A list of labels which are strings These parameters are both optional defaulting to plt.gca().get_legend_handles_labels(). You can remove duplicate labels by putting them in a dictionary before calling legend. This is because dicts can't have duplicate keys. For example: For Python versions < 3.7
```
from collections import OrderedDict
import matplotlib.pyplot as plt
handles, labels = plt.gca().get_legend_handles_labels()
by_label = OrderedDict(zip(labels, handles))
plt.legend(by_label.values(), by_label.keys())
```
For Python versions > 3.7 As of Python 3.7, dictionaries retain input order by default. Thus, there is no need for OrderedDict form the collections module.
```
import matplotlib.pyplot as plt
handles, labels = plt.gca().get_legend_handles_labels()
by_label = dict(zip(labels, handles))
plt.legend(by_label.values(), by_label.keys())
```
Docs for plt.legend
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/13588920/stop-matplotlib-repeating-labels-in-legend
| 219 |
[
"python",
"matplotlib",
"legend"
] | 466 | 961 | 3 |
Why doesn't plt.imshow() display the image? I have this code, copied from a tutorial:
```
import numpy as np
np.random.seed(123)
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras.datasets import mnist
(X_train,y_train),(X_test,y_test) = mnist.load_data()
print X_train.shape
from matplotlib import pyplot as plt
plt.imshow(X_train[0])
```
No image was displayed. Why not? There doesn't appear to be anything wrong with the backend of matplotlib on my computer. I tested that like so:
```
import matplotlib.pyplot as plt
data = [[0, 0.25], [0.5, 0.75]]
fig, ax = plt.subplots()
im = ax.imshow(data, cmap=plt.get_cmap('hot'), interpolation='nearest',
vmin=0, vmax=1)
fig.colorbar(im)
plt.show()
```
and was able to produce an image: I also tried printing X_train[0] and it looks right.
|
The solution was as simple as adding plt.show() at the end of the code snippet:
```
import numpy as np
np.random.seed(123)
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras.datasets import mnist
(X_train,y_train),(X_test,y_test) = mnist.load_data()
print X_train.shape
from matplotlib import pyplot as plt
plt.imshow(X_train[0])
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/42812230/why-doesnt-plt-imshow-display-the-image
| 257 |
[
"python",
"matplotlib",
"keras"
] | 946 | 488 | 3 |
How to add a second x-axis I have a very simple question. I need to have a second x-axis on my plot and I want that this axis has a certain number of tics that correspond to certain position of the first axis. Let's try with an example. Here I am plotting the dark matter mass as a function of the expansion factor, defined as 1/(1+z), that ranges from 0 to 1.
```
semilogy(1/(1+z),mass_acc_massive,'-',label='DM')
xlim(0,1)
ylim(1e8,5e12)
```
I would like to have another x-axis, on the top of my plot, showing the corresponding z for some values of the expansion factor. Is that possible? If yes, how can I have xtics ax
|
I'm taking a cue from the comments in @Dhara's answer, it sounds like you want to set a list of new_tick_locations by a function from the old x-axis to the new x-axis. The tick_function below takes in a numpy array of points, maps them to a new value and formats them:
```
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twiny()
X = np.linspace(0,1,1000)
Y = np.cos(X*20)
ax1.plot(X,Y)
ax1.set_xlabel(r"Original x-axis: $X$")
new_tick_locations = np.array([.2, .5, .9])
def tick_function(X):
V = 1/(1+X)
return ["%.3f" % z for z in V]
ax2.set_xlim(ax1.get_xlim())
ax2.set_xticks(new_tick_locations)
ax2.set_xticklabels(tick_function(new_tick_locations))
ax2.set_xlabel(r"Modified x-axis: $1/(1+X)$")
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/10514315/how-to-add-a-second-x-axis
| 157 |
[
"python",
"matplotlib",
"twiny"
] | 623 | 788 | 3 |
How to set the range of y-axis for a seaborn boxplot [duplicate] This question already has answers here: How to set the axis limits in Matplotlib? (10 answers) Closed 2 years ago. From the official seaborn documentation, I learned that you can create a boxplot as below:
```py
import seaborn as sns
sns.set_style("whitegrid")
tips = sns.load_dataset("tips")
ax = sns.boxplot(x="day", y="total_bill", data=tips)
```
My question is: how do I limit the range of y-axis of this plot? For example, I want the y-axis to be within [10, 40]. Is there any easy way to do this?
|
It is standard matplotlib.pyplot:
```
import matplotlib.pyplot as plt
plt.ylim(10, 40)
```
Or simpler, as mwaskom comments below:
```
ax.set(ylim=(10, 40))
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/33227473/how-to-set-the-range-of-y-axis-for-a-seaborn-boxplot
| 174 |
[
"python",
"matplotlib",
"plot",
"seaborn",
"boxplot"
] | 568 | 162 | 5 |
How to forget previous plots - how can I flush/refresh? How do you get matplotlib.pyplot to "forget" previous plots I am trying to plot multiple time using matplotlib.pyplot The code looks like this:
```
def plottest():
import numpy as np
import matplotlib.pyplot as plt
a=np.random.rand(10,)
b=np.random.rand(10,)
c=np.random.rand(10,)
plt.plot(a,label='a')
plt.plot(b,label='b')
plt.plot(c,label='c')
plt.legend(loc='upper left')
plt.ylabel('mag')
plt.xlabel('element)')
plt.show()
e=np.random.rand(10,)
f=np.random.rand(10,)
g=np.random.rand(10,)
plt.plot(e,label='e')
plt.plot(f,label='f')
plt.plot(g,label='g')
plt.legend(loc='upper left')
plt.ylabel('mag')
plt.xlabel('element)')
plt.show()
```
Unfortunately I keep getting the same plot (actually from some other code which I ran and completed a while ago) no matter what I do. Similar code has worked previously for me. I have looked at these questions: How to "clean the slate"? Matplotlib pyplot show() doesn't work once closed (python) matplotlib pyplot show() .. blocking or not? and tried using plt.show(), plt.clf() and plt.close to no avail. Any ideas?
|
I would rather use plt.clf() after every plt.show() to just clear the current figure instead of closing and reopening it, keeping the window size and giving you a better performance and much better memory usage. Similarly, you could do plt.cla() to just clear the current axes. To clear a specific axes, useful when you have multiple axes within one figure, you could do for example:
```
fig, axes = plt.subplots(nrows=2, ncols=2)
axes[0, 1].clear()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/17106288/how-to-forget-previous-plots-how-can-i-flush-refresh
| 137 |
[
"python",
"matplotlib"
] | 1,213 | 455 | 2 |
How to draw a line with matplotlib? I cannot find a way to draw an arbitrary line with matplotlib Python library. It allows to draw horizontal and vertical lines (with matplotlib.pyplot.axhline and matplotlib.pyplot.axvline, for example), but i do not see how to draw a line through two given points (x1, y1) and (x2, y2). Is there a way? Is there a simple way?
|
This will draw a line that passes through the points (-1, 1) and (12, 4), and another one that passes through the points (1, 3) et (10, 2) x1 are the x coordinates of the points for the first line, y1 are the y coordinates for the same -- the elements in x1 and y1 must be in sequence. x2 and y2 are the same for the other line.
```
import matplotlib.pyplot as plt
x1, y1 = [-1, 12], [1, 4]
x2, y2 = [1, 10], [3, 2]
plt.plot(x1, y1, x2, y2, marker = 'o')
plt.show()
```
I suggest you spend some time reading / studying the basic tutorials found on the very rich matplotlib website to familiarize yourself with the library. What if I don't want line segments? [edit]: As shown by @thomaskeefe, starting with matplotlib 3.3, this is now builtin as a convenience: plt.axline((x1, y1), (x2, y2)), rendering the following obsolete. There are no direct ways to have lines extend to infinity... matplotlib will either resize/rescale the plot so that the furthest point will be on the boundary and the other inside, drawing line segments in effect; or you must choose points outside of the boundary of the surface you want to set visible, and set limits for the x and y axis. As follows:
```
import matplotlib.pyplot as plt
x1, y1 = [-1, 12], [1, 10]
x2, y2 = [-1, 10], [3, -1]
plt.xlim(0, 8), plt.ylim(-2, 8)
plt.plot(x1, y1, x2, y2, marker = 'o')
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/36470343/how-to-draw-a-line-with-matplotlib
| 128 |
[
"python",
"python-3.x",
"matplotlib"
] | 361 | 1,357 | 3 |
Relationship between dpi and figure size I have created a figure using matplotlib but I have realized the plot axis and the drawn line gets zoomed out. Reading this earlier discussion thread, it explains how to set the figure size.
```
fig, ax = plt.subplots()
fig.set_size_inches(3, 1.5)
plt.savefig(file.jpeg, edgecolor='black', dpi=400, facecolor='black', transparent=True)
```
With the above code (other configurations removed for brevity), I do get a resulting image file with 1200 X 600 desired dimensions(should we say resolution too?) and desired file size. The projected image is scaled out in an unusual way, annotations for example are enlarged. While I can set the size of the labels on the axis, the figure doesn't look proportional with respect to the scale since the bottom and right spines are large and so are the plotted lines. The question, therefore, is, what configurations are going wrong?
|
Figure size (figsize) determines the size of the figure in inches. This gives the amount of space the axes (and other elements) have inside the figure. The default figure size is (6.4, 4.8) inches in matplotlib 2. A larger figure size will allow for longer texts, more axes or more ticklabels to be shown. Dots per inches (dpi) determines how many pixels the figure comprises. The default dpi in matplotlib is 100. A figure of figsize=(w,h) will have
```
px, py = w*dpi, h*dpi # pixels
# e.g.
# 6.4 inches * 100 dpi = 640 pixels
```
So in order to obtain a figure with a pixel size of e.g. (1200,600) you may chose several combinations of figure size and dpi, e.g.
```
figsize=(15,7.5), dpi= 80
figsize=(12,6) , dpi=100
figsize=( 8,4) , dpi=150
figsize=( 6,3) , dpi=200
etc.
```
Now, what is the difference? This is determined by the size of the elements inside the figure. Most elements like lines, markers, texts have a size given in points. Matplotlib figures use Points per inch (ppi) of 72. A line with thickness 1 point will be 1./72. inch wide. A text with fontsize 12 points will be 12./72. inch heigh. Of course if you change the figure size in inches, points will not change, so a larger figure in inches still has the same size of the elements. Changing the figure size is thus like taking a piece of paper of a different size. Doing so, would of course not change the width of the line drawn with the same pen. On the other hand, changing the dpi scales those elements. At 72 dpi, a line of 1 point size is one pixel strong. At 144 dpi, this line is 2 pixels strong. A larger dpi will therefore act like a magnifying glass. All elements are scaled by the magnifying power of the lens. A comparisson for constant figure size and varying dpi is shown in the image below on the left. On the right you see a constant dpi and varying figure size. Figures in each row have the same pixel size. Code to reproduce:
```
import matplotlib.pyplot as plt
%matplotlib inline
def plot(fs,dpi):
fig, ax=plt.subplots(figsize=fs, dpi=dpi)
ax.set_title("Figsize: {}, dpi: {}".format(fs,dpi))
ax.plot([2,4,1,5], label="Label")
ax.legend()
figsize=(2,2)
for i in range(1,4):
plot(figsize, i*72)
dpi=72
for i in [2,4,6]:
plot((i,i), dpi)
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/47633546/relationship-between-dpi-and-figure-size
| 243 |
[
"matplotlib",
"plot",
"graph",
"visualization"
] | 912 | 2,266 | 4 |
Get matplotlib color cycle state Is it possible to query the current state of the matplotlib color cycle? In other words is there a function get_cycle_state that will behave in the following way?
```
```python
plot(x1, y1)
```
```python
plot(x2, y2)
```
```python
state = get_cycle_state()
```
```python
print state
#Output
#2
#```
#Where I expect the state to be the index of the next color that will be used in a plot. Alternatively, if it returned the next color ("r" for the default cycle in the example above), that would be fine too.
```
|
Accessing the color cycle iterator There's no "user-facing" (a.k.a. "public") method to access the underlying iterator, but you can access it through "private" (by convention) methods. However, you'd can't get the state of an iterator without changing it. Setting the color cycle Quick aside: You can set the color/property cycle in a variety of ways (e.g. ax.set_color_cycle in versions =1.5). Have a look at the example here for version 1.5 or greater, or the previous style here. Accessing the underlying iterator However, while there's no public-facing method to access the iterable, you can access it for a given axes object (ax) through the _get_lines helper class instance. ax._get_lines is a touch confusingly named, but it's the behind-the-scenes machinery that allows the plot command to process all of the odd and varied ways that plot can be called. Among other things, it's what keeps track of what colors to automatically assign. Similarly, there's ax._get_patches_for_fill to control cycling through default fill colors and patch properties. At any rate, the color cycle iterable is ax._get_lines.color_cycle for lines and ax._get_patches_for_fill.color_cycle for patches. On matplotlib >=1.5, this has changed to use the cycler library, and the iterable is called prop_cycler instead of color_cycle and yields a dict of properties instead of only a color. All in all, you'd do something like:
```
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
color_cycle = ax._get_lines.color_cycle
# or ax._get_lines.prop_cycler on version >= 1.5
# Note that prop_cycler cycles over dicts, so you'll want next(cycle)['color']
```
You can't view the state of an iterator However, this object is a "bare" iterator. We can easily get the next item (e.g. next_color = next(color_cycle), but that means that the next color after that is what will be plotted. By design, there's no way to get the current state of an iterator without changing it. In v1.5 or greater, it would be nice to get the cycler object that's used, as we could infer its current state. However, the cycler object itself isn't accessible (publicly or privately) anywhere. Instead, only the itertools.cycle instance created from the cycler object is accessible. Either way, there's no way to get to the underlying state of the color/property cycler. Match the color of the previously plotted item instead In your case, it sounds like you're wanting to match the color of something that was just plotted. Instead of trying to determine what the color/property will be, set the color/etc of your new item based on the properties of what's plotted. For example, in the case you described, I'd do something like this:
```
import matplotlib.pyplot as plt
import numpy as np
def custom_plot(x, y, **kwargs):
ax = kwargs.pop('ax', plt.gca())
base_line, = ax.plot(x, y, **kwargs)
ax.fill_between(x, 0.9*y, 1.1*y, facecolor=base_line.get_color(), alpha=0.5)
x = np.linspace(0, 1, 10)
custom_plot(x, x)
custom_plot(x, 2*x)
custom_plot(x, -x, color='yellow', lw=3)
plt.show()
```
It's not the only way, but its cleaner than trying to get the color of the plotted line before-hand, in this case.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/13831549/get-matplotlib-color-cycle-state
| 130 |
[
"python",
"matplotlib"
] | 493 | 3,178 | 2 |
How do I write a Latex formula in the legend of a plot using Matplotlib inside a .py file? I am writing a script in Python (.py file) and I am using Matplotlib to plot an array. I want to add a legend with a formula to the plot, but I haven't been able to do it. I have done this before in IPython or the terminal. In this case, writing something like this:
```
legend(ur'$The_formula$')
```
worked perfectly. However, this doesn't work when I call my .py script from the terminal/IPython.
|
The easiest way is to assign the label when you plot the data, e.g.:
```
import matplotlib.pyplot as plt
ax = plt.gca() # or any other way to get an axis object
ax.plot(x, y, label=r'$\sin (x)$')
ax.legend()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/14016217/how-do-i-write-a-latex-formula-in-the-legend-of-a-plot-using-matplotlib-inside-a
| 118 |
[
"python",
"matplotlib",
"latex"
] | 490 | 214 | 3 |
Drawing average line in histogram I am drawing a histogram using matplotlib in python, and would like to draw a line representing the average of the dataset, overlaid on the histogram as a dotted line (or maybe some other color would do too). Any ideas on how to draw a line overlaid on the histogram? I am using the plot() command, but not sure how to draw a vertical line (i.e. what value should I give for the y-axis? thanks!
|
You can use plot or vlines to draw a vertical line, but to draw a vertical line from the bottom to the top of the y axis, axvline is the probably the simplest function to use. Here's an example:
```
```python
import numpy as np
```
```python
import matplotlib.pyplot as plt
```
```python
np.random.seed(6789)
```
```python
x = np.random.gamma(4, 0.5, 1000)
```
```python
result = plt.hist(x, bins=20, color='c', edgecolor='k', alpha=0.65)
```
```python
plt.axvline(x.mean(), color='k', linestyle='dashed', linewidth=1)
#Output
#<matplotlib.lines.Line2D at 0x119758828>
#```
#Result:
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/16180946/drawing-average-line-in-histogram
| 178 |
[
"python",
"matplotlib",
"axis"
] | 428 | 560 | 3 |
How to remove lines in a Matplotlib plot How can I remove a line (or lines) of a matplotlib axes in such a way as it actually gets garbage collected and releases the memory back? The below code appears to delete the line, but never releases the memory (even with explicit calls to gc.collect())
```
from matplotlib import pyplot
import numpy
a = numpy.arange(int(1e7))
# large so you can easily see the memory footprint on the system monitor.
fig = pyplot.Figure()
ax = pyplot.add_subplot(1, 1, 1)
lines = ax.plot(a) # this uses up an additional 230 Mb of memory.
# can I get the memory back?
l = lines[0]
l.remove()
del l
del lines
# not releasing memory
ax.cla() # this does release the memory, but also wipes out all other lines.
```
So is there a way to just delete one line from an axes and get the memory back? This potential solution also does not work.
|
This is a very long explanation that I typed up for a coworker of mine. I think it would be helpful here as well. Be patient, though. I get to the real issue that you are having toward the end. Just as a teaser, it's an issue of having extra references to your Line2D objects hanging around. WARNING: One other note before we dive in. If you are using IPython to test this out, IPython keeps references of its own and not all of them are weakrefs. So, testing garbage collection in IPython does not work. It just confuses matters. Okay, here we go. Each matplotlib object (Figure, Axes, etc) provides access to its child artists via various attributes. The following example is getting quite long, but should be illuminating. We start out by creating a Figure object, then add an Axes object to that figure. Note that ax and fig.axes[0] are the same object (same id()).
```
```python
#Create a figure
```
```python
fig = plt.figure()
```
```python
fig.axes
#Output
#[]
```
```python
#Add an axes object
```
```python
ax = fig.add_subplot(1,1,1)
```
```python
#The object in ax is the same as the object in fig.axes[0], which is
```
```python
# a list of axes objects attached to fig
```
```python
print ax
#Output
#Axes(0.125,0.1;0.775x0.8)
```
```python
print fig.axes[0]
#Output
#Axes(0.125,0.1;0.775x0.8) #Same as "print ax"
```
```python
id(ax), id(fig.axes[0])
#Output
#(212603664, 212603664) #Same ids => same objects
#```
#This also extends to lines in an axes object:
#```
```
```python
#Add a line to ax
```
```python
lines = ax.plot(np.arange(1000))
```
```python
#Lines and ax.lines contain the same line2D instances
```
```python
print lines
#Output
#[<matplotlib.lines.Line2D object at 0xce84bd0>]
```
```python
print ax.lines
#Output
#[<matplotlib.lines.Line2D object at 0xce84bd0>]
```
```python
print lines[0]
#Output
#Line2D(_line0)
```
```python
print ax.lines[0]
#Output
#Line2D(_line0)
```
```python
#Same ID => same object
```
```python
id(lines[0]), id(ax.lines[0])
#Output
#(216550352, 216550352)
#```
#If you were to call plt.show() using what was done above, you would see a figure containing a set of axes and a single line: Now, while we have seen that the contents of lines and ax.lines is the same, it is very important to note that the object referenced by the lines variable is not the same as the object reverenced by ax.lines as can be seen by the following:
#```
```
```python
id(lines), id(ax.lines)
#Output
#(212754584, 211335288)
#```
#As a consequence, removing an element from lines does nothing to the current plot, but removing an element from ax.lines removes that line from the current plot. So:
#```
```
```python
#THIS DOES NOTHING:
```
```python
lines.pop(0)
```
```python
#THIS REMOVES THE FIRST LINE:
```
```python
ax.lines.pop(0)
#Output
#```
#So, if you were to run the second line of code, you would remove the Line2D object contained in ax.lines[0] from the current plot and it would be gone. Note that this can also be done via ax.lines.remove() meaning that you can save a Line2D instance in a variable, then pass it to ax.lines.remove() to delete that line, like so:
#```
```
```python
#Create a new line
```
```python
lines.append(ax.plot(np.arange(1000)/2.0))
```
```python
ax.lines
#Output
#[<matplotlib.lines.Line2D object at 0xce84bd0>, <matplotlib.lines.Line2D object at 0xce84dx3>]
#```
#```
```
```python
#Remove that new line
```
```python
ax.lines.remove(lines[0])
```
```python
ax.lines
#Output
#[<matplotlib.lines.Line2D object at 0xce84dx3>]
#```
#All of the above works for fig.axes just as well as it works for ax.lines Now, the real problem here. If we store the reference contained in ax.lines[0] into a weakref.ref object, then attempt to delete it, we will notice that it doesn't get garbage collected:
#```
```
```python
#Create weak reference to Line2D object
```
```python
from weakref import ref
```
```python
wr = ref(ax.lines[0])
```
```python
print wr
#Output
#<weakref at 0xb758af8; to 'Line2D' at 0xb757fd0>
```
```python
print wr()
#Output
#<matplotlib.lines.Line2D at 0xb757fd0>
```
```python
#Delete the line from the axes
```
```python
ax.lines.remove(wr())
```
```python
ax.lines
#Output
#[]
```
```python
#Test weakref again
```
```python
print wr
#Output
#<weakref at 0xb758af8; to 'Line2D' at 0xb757fd0>
```
```python
print wr()
#Output
#<matplotlib.lines.Line2D at 0xb757fd0>
#```
#The reference is still live! Why? This is because there is still another reference to the Line2D object that the reference in wr points to. Remember how lines didn't have the same ID as ax.lines but contained the same elements? Well, that's the problem.
#```
```
```python
#Print out lines
```
```python
print lines
#Output
#[<matplotlib.lines.Line2D object at 0xce84bd0>, <matplotlib.lines.Line2D object at 0xce84dx3>]
```
To fix this problem, we simply need to delete `lines`, empty it, or let it go out of scope.
```python
#Reinitialize lines to empty list
```
```python
lines = []
```
```python
print lines
#Output
#[]
```
```python
print wr
#Output
#<weakref at 0xb758af8; dead>
#```
#So, the moral of the story is, clean up after yourself. If you expect something to be garbage collected but it isn't, you are likely leaving a reference hanging out somewhere.
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/4981815/how-to-remove-lines-in-a-matplotlib-plot
| 95 |
[
"python",
"matplotlib",
"plot"
] | 862 | 4,588 | 3 |
Matplotlib/Pyplot: How to zoom subplots together? I have plots of 3-axis accelerometer time-series data (t,x,y,z) in separate subplots I'd like to zoom together. That is, when I use the "Zoom to Rectangle" tool on one plot, when I release the mouse all 3 plots zoom together. Previously, I simply plotted all 3 axes on a single plot using different colors. But this is useful only with small amounts of data: I have over 2 million data points, so the last axis plotted obscures the other two. Hence the need for separate subplots. I know I can capture matplotlib/pyplot mouse events (http://matplotlib.sourceforge.net/users/event_handling.html), and I know I can catch other events (http://matplotlib.sourceforge.net/api/backend_bases_api.html#matplotlib.backend_bases.ResizeEvent), but I don't know how to tell what zoom has been requested on any one subplot, and how to replicate it on the other two subplots. I suspect I have the all the pieces, and need only that one last precious clue... -BobC
|
The easiest way to do this is by using the sharex and/or sharey keywords when creating the axes:
```
from matplotlib import pyplot as plt
ax1 = plt.subplot(2,1,1)
ax1.plot(...)
ax2 = plt.subplot(2,1,2, sharex=ax1)
ax2.plot(...)
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/4200586/matplotlib-pyplot-how-to-zoom-subplots-together
| 161 |
[
"zooming",
"matplotlib"
] | 999 | 233 | 2 |
Is there a parameter in matplotlib/pandas to have the Y axis of a histogram as percentage? I would like to compare two histograms by having the Y axis show the percentage of each column from the overall dataset size instead of an absolute value. Is that possible? I am using Pandas and matplotlib. Thanks
|
The density=True (normed=True for matplotlib < 2.2.0) returns a histogram for which np.sum(pdf * np.diff(bins)) equals 1. If you want the sum of the histogram to be 1 you can use Numpy's histogram() and normalize the results yourself.
```
x = np.random.randn(30)
fig, ax = plt.subplots(1,2, figsize=(10,4))
ax[0].hist(x, density=True, color='grey')
hist, bins = np.histogram(x)
ax[1].bar(bins[:-1], hist.astype(np.float32) / hist.sum(), width=(bins[1]-bins[0]), color='grey')
ax[0].set_title('normed=True')
ax[1].set_title('hist = hist / hist.sum()')
```
Btw: Strange plotting glitch at the first bin of the left plot.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/17874063/is-there-a-parameter-in-matplotlib-pandas-to-have-the-y-axis-of-a-histogram-as-p
| 108 |
[
"python",
"pandas",
"matplotlib"
] | 304 | 623 | 3 |
Plotting a 3d cube, a sphere and a vector I search how to plot something with less instruction as possible with Matplotlib but I don't find any help for this in the documentation. I want to plot the following things: a wireframe cube centered in 0 with a side length of 2 a "wireframe" sphere centered in 0 with a radius of 1 a point at coordinates [0, 0, 0] a vector that starts at this point and goes to [1, 1, 1] How to do that?
|
It is a little complicated, but you can draw all the objects by the following code:
```
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from itertools import product, combinations
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_aspect("equal")
# draw cube
r = [-1, 1]
for s, e in combinations(np.array(list(product(r, r, r))), 2):
if np.sum(np.abs(s-e)) == r[1]-r[0]:
ax.plot3D(*zip(s, e), color="b")
# draw sphere
u, v = np.mgrid[0:2*np.pi:20j, 0:np.pi:10j]
x = np.cos(u)*np.sin(v)
y = np.sin(u)*np.sin(v)
z = np.cos(v)
ax.plot_wireframe(x, y, z, color="r")
# draw a point
ax.scatter([0], [0], [0], color="g", s=100)
# draw a vector
from matplotlib.patches import FancyArrowPatch
from mpl_toolkits.mplot3d import proj3d
class Arrow3D(FancyArrowPatch):
def __init__(self, xs, ys, zs, *args, **kwargs):
FancyArrowPatch.__init__(self, (0, 0), (0, 0), *args, **kwargs)
self._verts3d = xs, ys, zs
def draw(self, renderer):
xs3d, ys3d, zs3d = self._verts3d
xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)
self.set_positions((xs[0], ys[0]), (xs[1], ys[1]))
FancyArrowPatch.draw(self, renderer)
a = Arrow3D([0, 1], [0, 1], [0, 1], mutation_scale=20,
lw=1, arrowstyle="-|>", color="k")
ax.add_artist(a)
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/11140163/plotting-a-3d-cube-a-sphere-and-a-vector
| 216 |
[
"python",
"matplotlib",
"geometry",
"matplotlib-3d"
] | 431 | 1,369 | 4 |
How to show two figures using matplotlib? I have some troubles while drawing two figures at the same time, not shown in a single plot. But according to the documentation, I wrote the code and only the figure one shows. I think maybe I lost something important. Could anyone help me to figure out? Thanks. (The *tlist_first* used in the code is a list of data.)
```
plt.figure(1)
plt.hist(tlist_first, bins=2000000, normed = True, histtype ="step", cumulative = True, color = 'g',label = 'first answer')
plt.ylabel('Percentage of answered questions')
plt.xlabel('Minutes elapsed after questions are posted')
plt.axvline(x = 30, ymin = 0, ymax = 1, color = 'r', linestyle = '--', label = '30 min')
plt.axvline(x = 60, ymin = 0, ymax = 1, color = 'c', linestyle = '--', label = '1 hour')
plt.legend()
plt.xlim(0,120)
plt.ylim(0,1)
plt.show()
plt.close() ### not working either with this line or without it
plt.figure(2)
plt.hist(tlist_first, bins=2000000, normed = True, histtype ="step", cumulative = True, color = 'g',label = 'first answer')
plt.ylabel('Percentage of answered questions')
plt.xlabel('Minutes elapsed after questions are posted')
plt.axvline(x = 240, ymin = 0, ymax = 1, color = 'r', linestyle = '--', label = '30 min')
plt.axvline(x = 1440, ymin = 0, ymax = 1, color = 'c', linestyle = '--', label = '1 hour')
plt.legend(loc= 4)
plt.xlim(0,2640)
plt.ylim(0,1)
plt.show()
```
|
Alternatively to calling plt.show() at the end of the script, you can also control each figure separately doing:
```
f = plt.figure(1)
plt.hist........
............
f.show()
g = plt.figure(2)
plt.hist(........
................
g.show()
raw_input()
```
In this case you must call raw_input to keep the figures alive. This way you can select dynamically which figures you want to show Note: raw_input() was renamed to input() in Python 3
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/7744697/how-to-show-two-figures-using-matplotlib
| 110 |
[
"python",
"matplotlib"
] | 1,396 | 438 | 2 |
Putting newline in matplotlib label with TeX in Python? How can I add a newline to a plot's label (e.g. xlabel or ylabel) in matplotlib? For example,
```
plt.bar([1, 2], [4, 5])
plt.xlabel("My x label")
plt.ylabel(r"My long label with $\Sigma_{C}$ math \n continues here")
```
Ideally I'd like the y-labeled to be centered too. Is there a way to do this? It's important that the label have both TeX (enclosed in '$') and the newline.
|
You can have the best of both worlds: automatic "escaping" of LaTeX commands and newlines:
```
plt.ylabel(r"My long label with unescaped {\LaTeX} $\Sigma_{C}$ math"
"\n" # Newline: the backslash is interpreted as usual
r"continues here with $\pi$")
```
(instead of using three lines, separating the strings by single spaces is another option). In fact, Python automatically concatenates string literals that follow each other, and you can mix raw strings (r"…") and strings with character interpolation ("\n").
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/2660319/putting-newline-in-matplotlib-label-with-tex-in-python
| 143 |
[
"python",
"plot",
"graphing",
"matplotlib"
] | 434 | 534 | 4 |
How to display custom values on a bar plot I'm looking to see how to do two things in Seaborn with using a bar chart to display values that are in the dataframe, but not in the graph. I'm looking to display the values of one field in a dataframe while graphing another. For example, below, I'm graphing 'tip', but I would like to place the value of 'total_bill' centered above each of the bars (i.e.325.88 above Friday, 1778.40 above Saturday, etc.) Is there a way to scale the colors of the bars, with the lowest value of 'total_bill' having the lightest color (in this case Friday) and the highest value of 'total_bill' having the darkest? Obviously, I'd stick with one color (i.e., blue) when I do the scaling. While I see that others think that this is a duplicate of another problem (or two), I am missing the part of how I use a value that is not in the graph as the basis for the label or the shading. How do I say, use total_bill as the basis. I'm sorry, but I just can't figure it out based on those answers. Starting with the following code,
```
import pandas as pd
import seaborn as sns
%matplotlib inline
df = pd.read_csv("https://raw.githubusercontent.com/wesm/pydata-book/1st-edition/ch08/tips.csv", sep=',')
groupedvalues = df.groupby('day').sum().reset_index()
g = sns.barplot(x='day', y='tip', data=groupedvalues)
```
I get the following result: Interim Solution:
```
for index, row in groupedvalues.iterrows():
g.text(row.name, row.tip, round(row.total_bill, 2), color='black', ha="center")
```
On the shading, using the example below, I tried the following:
```
import pandas as pd
import seaborn as sns
%matplotlib inline
df = pd.read_csv("https://raw.githubusercontent.com/wesm/pydata-book/1st-edition/ch08/tips.csv", sep=',')
groupedvalues = df.groupby('day').sum().reset_index()
pal = sns.color_palette("Greens_d", len(data))
rank = groupedvalues.argsort().argsort()
g = sns.barplot(x='day', y='tip', data=groupedvalues)
for index, row in groupedvalues.iterrows():
g.text(row.name, row.tip, round(row.total_bill, 2), color='black', ha="center")
```
But that gave me the following error: AttributeError: 'DataFrame' object has no attribute 'argsort' So I tried a modification:
```
import pandas as pd
import seaborn as sns
%matplotlib inline
df = pd.read_csv("https://raw.githubusercontent.com/wesm/pydata-book/1st-edition/ch08/tips.csv", sep=',')
groupedvalues = df.groupby('day').sum().reset_index()
pal = sns.color_palette("Greens_d", len(data))
rank = groupedvalues['total_bill'].rank(ascending=True)
g = sns.barplot(x='day', y='tip', data=groupedvalues, palette=np.array(pal[::-1])[rank])
```
and that leaves me with IndexError: index 4 is out of bounds for axis 0 with size 4
|
New in matplotlib 3.4.0 There is now a built-in Axes.bar_label to automatically label bar containers:
```py
ax = sns.barplot(x='day', y='tip', data=groupedvalues)
ax.bar_label(ax.containers[0]) # only 1 container needed unless using `hue`
```
For custom labels (e.g., tip bars with total_bill values), use the labels parameter:
```py
ax = sns.barplot(x='day', y='tip', data=groupedvalues)
ax.bar_label(ax.containers[0], labels=groupedvalues['total_bill'])
# ----------------------------------
```
For multi-group bar plots (i.e., with hue), there will be multiple bar containers that need to be iterated:
```py
ax = sns.barplot(x='day', y='tip', hue='sex', data=df)
for container in ax.containers:
ax.bar_label(container)
```
More details: How to label percentage counts (fmt param) How to rotate labels (rotation param) How to vertically center labels (label_type param) How to add spacing to labels (padding param) Color-ranked version Is there a way to scale the colors of the bars, with the lowest value of total_bill having the lightest color (in this case Friday) and the highest value of total_bill having the darkest? Find the rank of each total_bill value: Either use Series.sort_values:
```py
ranks = groupedvalues.total_bill.sort_values().index
# Int64Index([1, 0, 3, 2], dtype='int64')
```
Or condense Ernest's Series.rank version by chaining Series.sub:
```py
ranks = groupedvalues.total_bill.rank().sub(1).astype(int).array
# [1, 0, 3, 2]
```
Then reindex the color palette using ranks:
```py
palette = sns.color_palette('Blues_d', len(ranks))
ax = sns.barplot(x='day', y='tip', palette=np.array(palette)[ranks], data=groupedvalues)
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/43214978/how-to-display-custom-values-on-a-bar-plot
| 165 |
[
"python",
"pandas",
"matplotlib",
"seaborn",
"bar-chart"
] | 2,721 | 1,689 | 5 |
Matplotlib scatter plot legend I created a 4D scatter plot graph to represent different temperatures in a specific area. When I create the legend, the legend shows the correct symbol and color but adds a line through it. The code I'm using is:
```
colors=['b', 'c', 'y', 'm', 'r']
lo = plt.Line2D(range(10), range(10), marker='x', color=colors[0])
ll = plt.Line2D(range(10), range(10), marker='o', color=colors[0])
l = plt.Line2D(range(10), range(10), marker='o',color=colors[1])
a = plt.Line2D(range(10), range(10), marker='o',color=colors[2])
h = plt.Line2D(range(10), range(10), marker='o',color=colors[3])
hh = plt.Line2D(range(10), range(10), marker='o',color=colors[4])
ho = plt.Line2D(range(10), range(10), marker='x', color=colors[4])
plt.legend((lo,ll,l,a, h, hh, ho),('Low Outlier', 'LoLo','Lo', 'Average', 'Hi', 'HiHi', 'High Outlier'),numpoints=1, loc='lower left', ncol=3, fontsize=8)
```
I tried changing Line2D to Scatter and scatter. Scatter returned an error and scatter changed the graph and returned an error. With scatter, I changed the range(10) to the lists containing the data points. Each list contains either the x, y, or z variable.
```
lo = plt.scatter(xLOutlier, yLOutlier, zLOutlier, marker='x', color=colors[0])
ll = plt.scatter(xLoLo, yLoLo, zLoLo, marker='o', color=colors[0])
l = plt.scatter(xLo, yLo, zLo, marker='o',color=colors[1])
a = plt.scatter(xAverage, yAverage, zAverage, marker='o',color=colors[2])
h = plt.scatter(xHi, yHi, zHi, marker='o',color=colors[3])
hh = plt.scatter(xHiHi, yHiHi, zHiHi, marker='o',color=colors[4])
ho = plt.scatter(xHOutlier, yHOutlier, zHOutlier, marker='x', color=colors[4])
plt.legend((lo,ll,l,a, h, hh, ho),('Low Outlier', 'LoLo','Lo', 'Average', 'Hi', 'HiHi', 'High Outlier'),scatterpoints=1, loc='lower left', ncol=3, fontsize=8)
```
When I run this, the legend no longer exists, it is a small white box in the corner with nothing in it. Any advice?
|
2D scatter plot Using the scatter method of the matplotlib.pyplot module should work (at least with matplotlib 1.2.1 with Python 2.7.5), as in the example code below. Also, if you are using scatter plots, use scatterpoints=1 rather than numpoints=1 in the legend call to have only one point for each legend entry. In the code below I've used random values rather than plotting the same range over and over, making all the plots visible (i.e. not overlapping each other).
```
import matplotlib.pyplot as plt
from numpy.random import random
colors = ['b', 'c', 'y', 'm', 'r']
lo = plt.scatter(random(10), random(10), marker='x', color=colors[0])
ll = plt.scatter(random(10), random(10), marker='o', color=colors[0])
l = plt.scatter(random(10), random(10), marker='o', color=colors[1])
a = plt.scatter(random(10), random(10), marker='o', color=colors[2])
h = plt.scatter(random(10), random(10), marker='o', color=colors[3])
hh = plt.scatter(random(10), random(10), marker='o', color=colors[4])
ho = plt.scatter(random(10), random(10), marker='x', color=colors[4])
plt.legend((lo, ll, l, a, h, hh, ho),
('Low Outlier', 'LoLo', 'Lo', 'Average', 'Hi', 'HiHi', 'High Outlier'),
scatterpoints=1,
loc='lower left',
ncol=3,
fontsize=8)
plt.show()
```
3D scatter plot To plot a scatter in 3D, use the plot method, as the legend does not support Patch3DCollection as is returned by the scatter method of an Axes3D instance. To specify the markerstyle you can include this as a positional argument in the method call, as seen in the example below. Optionally one can include argument to both the linestyle and marker parameters.
```
import matplotlib.pyplot as plt
from numpy.random import random
from mpl_toolkits.mplot3d import Axes3D
colors=['b', 'c', 'y', 'm', 'r']
ax = plt.subplot(111, projection='3d')
ax.plot(random(10), random(10), random(10), 'x', color=colors[0], label='Low Outlier')
ax.plot(random(10), random(10), random(10), 'o', color=colors[0], label='LoLo')
ax.plot(random(10), random(10), random(10), 'o', color=colors[1], label='Lo')
ax.plot(random(10), random(10), random(10), 'o', color=colors[2], label='Average')
ax.plot(random(10), random(10), random(10), 'o', color=colors[3], label='Hi')
ax.plot(random(10), random(10), random(10), 'o', color=colors[4], label='HiHi')
ax.plot(random(10), random(10), random(10), 'x', color=colors[4], label='High Outlier')
plt.legend(loc='upper left', numpoints=1, ncol=3, fontsize=8, bbox_to_anchor=(0, 0))
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/17411940/matplotlib-scatter-plot-legend
| 163 |
[
"python",
"matplotlib",
"legend",
"scatter-plot"
] | 1,930 | 2,538 | 4 |
How to add line based on slope and intercept In R, there is a function called abline in which a line can be drawn on a plot based on the specification of the intercept (first argument) and the slope (second argument). For instance,
```
plot(1:10, 1:10)
abline(0, 1)
```
where the line with an intercept of 0 and the slope of 1 spans the entire range of the plot. Is there such a function in Matplotlib?
|
A lot of these solutions are focusing on adding a line to the plot that fits the data. Here's a simple solution for adding an arbitrary line to the plot based on a slope and intercept.
```
import matplotlib.pyplot as plt
import numpy as np
def abline(slope, intercept):
"""Plot a line from slope and intercept"""
axes = plt.gca()
x_vals = np.array(axes.get_xlim())
y_vals = intercept + slope * x_vals
plt.plot(x_vals, y_vals, '--')
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/7941226/how-to-add-line-based-on-slope-and-intercept
| 128 |
[
"python",
"matplotlib"
] | 403 | 462 | 2 |
How to use matplotlib tight layout with Figure? [duplicate] This question already has answers here: Improve subplot size/spacing with many subplots (12 answers) Closed 2 years ago. I found tight_layout function for pyplot and want to use it. In my application I embed matplotlib plots into Qt GUI and use figure and not pyplot. Is there any way I can apply tight_layout there? Would it also work if I have several axes in one figure?
|
Just call fig.tight_layout() as you normally would. (pyplot is just a convenience wrapper. In most cases, you only use it to quickly generate figure and axes objects and then call their methods directly.) There shouldn't be a difference between the QtAgg backend and the default backend (or if there is, it's a bug). E.g.
```
import matplotlib.pyplot as plt
#-- In your case, you'd do something more like:
# from matplotlib.figure import Figure
# fig = Figure()
#-- ...but we want to use it interactive for a quick example, so
#-- we'll do it this way
fig, axes = plt.subplots(nrows=4, ncols=4)
for i, ax in enumerate(axes.flat, start=1):
ax.set_title('Test Axes {}'.format(i))
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
plt.show()
```
Before Tight Layout After Tight Layout
```
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=4, ncols=4)
for i, ax in enumerate(axes.flat, start=1):
ax.set_title('Test Axes {}'.format(i))
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
fig.tight_layout()
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/9603230/how-to-use-matplotlib-tight-layout-with-figure
| 152 |
[
"python",
"matplotlib",
"figure"
] | 433 | 1,060 | 3 |
Can i cycle through line styles in matplotlib I know how to cycle through a list of colors in matplotlib. But is it possible to do something similar with line styles (plain, dotted, dashed, etc.)? I'd need to do that so my graphs would be easier to read when printed. Any suggestions how to do that?
|
Something like this might do the trick:
```
import matplotlib.pyplot as plt
from itertools import cycle
lines = ["-","--","-.",":"]
linecycler = cycle(lines)
plt.figure()
for i in range(10):
x = range(i,i+10)
plt.plot(range(10),x,next(linecycler))
plt.show()
```
Result: Edit for newer version (v2.22)
```
import matplotlib.pyplot as plt
from cycler import cycler
#
plt.figure()
for i in range(5):
x = range(i,i+5)
linestyle_cycler = cycler('linestyle',['-','--',':','-.'])
plt.rc('axes', prop_cycle=linestyle_cycler)
plt.plot(range(5),x)
plt.legend(['first','second','third','fourth','fifth'], loc='upper left', fancybox=True, shadow=True)
plt.show()
```
For more detailed information consult the matplotlib tutorial on "Styling with cycler" To see the output click "show figure"
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/7799156/can-i-cycle-through-line-styles-in-matplotlib
| 136 |
[
"python",
"matplotlib"
] | 299 | 810 | 2 |
Barchart with vertical ytick labels I'm using matplotlib to generate a (vertical) barchart. The problem is my labels are rather long. Is there any way to display them vertically, either in the bar or above it or below it?
|
Do you mean something like this:
```
```python
from matplotlib import *
```
```python
plot(xrange(10))
```
```python
yticks(xrange(10), rotation='vertical')
#Output
#```
#? In general, to show any text in matplotlib with a vertical orientation, you can add the keyword rotation='vertical'. For further options, you can look at help(matplotlib.pyplot.text) The yticks function plots the ticks on the y axis; I am not sure whether you originally meant this or the ylabel function, but the procedure is alwasy the same, you have to add rotation='vertical' Maybe you can also find useful the options 'verticalalignment' and 'horizontalalignment', which allows you to define how to align the text with respect to the ticks or the other elements.
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/1221108/barchart-with-vertical-ytick-labels
| 116 |
[
"python",
"matplotlib",
"bar-chart",
"yaxis"
] | 221 | 705 | 4 |
What are the differences between add_axes and add_subplot? In a previous answer it was recommended to me to use add_subplot instead of add_axes to show axes correctly, but searching the documentation I couldn't understand when and why I should use either one of these functions. Can anyone explain the differences?
|
Common grounds Both, add_axes and add_subplot add an axes to a figure. They both return a (subclass of a) matplotlib.axes.Axes object. However, the mechanism which is used to add the axes differs substantially. add_axes The calling signature of add_axes is add_axes(rect), where rect is a list [x0, y0, width, height] denoting the lower left point of the new axes in figure coodinates (x0,y0) and its width and height. So the axes is positionned in absolute coordinates on the canvas. E.g.
```
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
```
places a figure in the canvas that is exactly as large as the canvas itself. add_subplot The calling signature of add_subplot does not directly provide the option to place the axes at a predefined position. It rather allows to specify where the axes should be situated according to a subplot grid. The usual and easiest way to specify this position is the 3 integer notation,
```
fig = plt.figure()
ax = fig.add_subplot(231)
```
In this example a new axes is created at the first position (1) on a grid of 2 rows and 3 columns. To produce only a single axes, add_subplot(111) would be used (First plot on a 1 by 1 subplot grid). (In newer matplotlib versions, add_subplot() without any arguments is possible as well.) The advantage of this method is that matplotlib takes care of the exact positioning. By default add_subplot(111) would produce an axes positioned at [0.125,0.11,0.775,0.77] or similar, which already leaves enough space around the axes for the title and the (tick)labels. However, this position may also change depending on other elements in the plot, titles set, etc. It can also be adjusted using pyplot.subplots_adjust(...) or pyplot.tight_layout(). In most cases, add_subplot would be the prefered method to create axes for plots on a canvas. Only in cases where exact positioning matters, add_axes might be useful. Example
```
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (5,3)
fig = plt.figure()
fig.add_subplot(241)
fig.add_subplot(242)
ax = fig.add_subplot(223)
ax.set_title("subplots")
fig.add_axes([0.77,.3,.2,.6])
ax2 =fig.add_axes([0.67,.5,.2,.3])
fig.add_axes([0.6,.1,.35,.3])
ax2.set_title("random axes")
plt.tight_layout()
plt.show()
```
Alternative The easiest way to obtain one or more subplots together with their handles is plt.subplots(). For one axes, use
```
fig, ax = plt.subplots()
```
or, if more subplots are needed,
```
fig, axes = plt.subplots(nrows=3, ncols=4)
```
The initial question In the initial question an axes was placed using fig.add_axes([0,0,1,1]), such that it sits tight to the figure boundaries. The disadvantage of this is of course that ticks, ticklabels, axes labels and titles are cut off. Therefore I suggested in one of the comments to the answer to use fig.add_subplot as this will automatically allow for enough space for those elements, and, if this is not enough, can be adjusted using pyplot.subplots_adjust(...) or pyplot.tight_layout().
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/43326680/what-are-the-differences-between-add-axes-and-add-subplot
| 172 |
[
"python",
"matplotlib",
"subplot",
"figure",
"axes"
] | 314 | 2,983 | 5 |
Automatically run %matplotlib inline in IPython Notebook Every time I launch IPython Notebook, the first command I run is
```
%matplotlib inline
```
Is there some way to change my config file so that when I launch IPython, it is automatically in this mode?
|
The configuration way IPython has profiles for configuration, located at ~/.ipython/profile_*. The default profile is called profile_default. Within this folder there are two primary configuration files: ipython_config.py ipython_kernel_config.py Add the inline option for matplotlib to ipython_kernel_config.py:
```
c = get_config()
# ... Any other configurables you want to set
c.InteractiveShellApp.matplotlib = "inline"
```
matplotlib vs. pylab Usage of %pylab to get inline plotting is discouraged. It introduces all sorts of gunk into your namespace that you just don't need. %matplotlib on the other hand enables inline plotting without injecting your namespace. You'll need to do explicit calls to get matplotlib and numpy imported.
```
import matplotlib.pyplot as plt
import numpy as np
```
The small price of typing out your imports explicitly should be completely overcome by the fact that you now have reproducible code.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/21176731/automatically-run-matplotlib-inline-in-ipython-notebook
| 87 |
[
"python",
"matplotlib",
"jupyter-notebook"
] | 257 | 934 | 3 |
How to create grouped boxplots Is there a way to group boxplots in matplotlib? Assume we have three groups "A", "B", and "C" and for each we want to create a boxplot for both "apples" and "oranges". If a grouping is not possible directly, we can create all six combinations and place them linearly side by side. What would be to simplest way to visualize the groupings? I'm trying to avoid setting the tick labels to something like "A + apples" since my scenario involves much longer names than "A".
|
How about using colors to differentiate between "apples" and "oranges" and spacing to separate "A", "B" and "C"? Something like this:
```
from pylab import plot, show, savefig, xlim, figure, \
hold, ylim, legend, boxplot, setp, axes
# function for setting the colors of the box plots pairs
def setBoxColors(bp):
setp(bp['boxes'][0], color='blue')
setp(bp['caps'][0], color='blue')
setp(bp['caps'][1], color='blue')
setp(bp['whiskers'][0], color='blue')
setp(bp['whiskers'][1], color='blue')
setp(bp['fliers'][0], color='blue')
setp(bp['fliers'][1], color='blue')
setp(bp['medians'][0], color='blue')
setp(bp['boxes'][1], color='red')
setp(bp['caps'][2], color='red')
setp(bp['caps'][3], color='red')
setp(bp['whiskers'][2], color='red')
setp(bp['whiskers'][3], color='red')
setp(bp['fliers'][2], color='red')
setp(bp['fliers'][3], color='red')
setp(bp['medians'][1], color='red')
# Some fake data to plot
A= [[1, 2, 5,], [7, 2]]
B = [[5, 7, 2, 2, 5], [7, 2, 5]]
C = [[3,2,5,7], [6, 7, 3]]
fig = figure()
ax = axes()
hold(True)
# first boxplot pair
bp = boxplot(A, positions = [1, 2], widths = 0.6)
setBoxColors(bp)
# second boxplot pair
bp = boxplot(B, positions = [4, 5], widths = 0.6)
setBoxColors(bp)
# thrid boxplot pair
bp = boxplot(C, positions = [7, 8], widths = 0.6)
setBoxColors(bp)
# set axes limits and labels
xlim(0,9)
ylim(0,9)
ax.set_xticklabels(['A', 'B', 'C'])
ax.set_xticks([1.5, 4.5, 7.5])
# draw temporary red and blue lines and use them to create a legend
hB, = plot([1,1],'b-')
hR, = plot([1,1],'r-')
legend((hB, hR),('Apples', 'Oranges'))
hB.set_visible(False)
hR.set_visible(False)
savefig('boxcompare.png')
show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/16592222/how-to-create-grouped-boxplots
| 119 |
[
"python",
"matplotlib",
"boxplot"
] | 499 | 1,733 | 3 |
Superscript in Python plots I want to label my x axis at follows :
```
pylab.xlabel('metres 10^1')
```
But I don't want to have the ^ symbol included .
```
pylab.xlabel('metres 10$^{one}$')
```
This method works and will superscript letters but doesn't seem to work for numbers . If I try :
```
pylab.xlabel('metres 10$^1$')
```
It superscripts a letter N for some reason . Anyone know how to superscript numbers in python plots ? thanks .
|
You just need to have the full expression inside the $. Basically, you need "meters $10^1$". You don't need usetex=True to do this (or most any mathematical formula). You may also want to use a raw string (e.g. r"\t", vs "\t") to avoid problems with things like \n, \a, \b, \t, \f, etc. For example:
```
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set(title=r'This is an expression $e^{\sin(\omega\phi)}$',
xlabel='meters $10^1$', ylabel=r'Hertz $(\frac{1}{s})$')
plt.show()
```
If you don't want the superscripted text to be in a different font than the rest of the text, use \mathregular (or equivalently \mathdefault). Some symbols won't be available, but most will. This is especially useful for simple superscripts like yours, where you want the expression to blend in with the rest of the text.
```
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set(title=r'This is an expression $\mathregular{e^{\sin(\omega\phi)}}$',
xlabel='meters $\mathregular{10^1}$',
ylabel=r'Hertz $\mathregular{(\frac{1}{s})}$')
plt.show()
```
For more information (and a general overview of matplotlib's "mathtext"), see: http://matplotlib.org/users/mathtext.html
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/21226868/superscript-in-python-plots
| 164 |
[
"python",
"matplotlib"
] | 442 | 1,199 | 2 |
How to draw grid lines behind matplotlib bar graph
```
x = ['01-02', '02-02', '03-02', '04-02', '05-02']
y = [2, 2, 3, 7, 2]
fig, ax = plt.subplots(1, 1)
ax.bar(range(len(y)), y, width=0.3,align='center',color='skyblue')
plt.xticks(range(len(y)), x, size='small')
plt.savefig('/home/user/graphimages/foo2.png')
plt.close()
```
I want to draw grid lines (of x & y) behind the bar graph.
|
To add a grid you simply need to add ax.grid() If you want the grid to be behind the bars then add
```
ax.grid(zorder=0)
ax.bar(range(len(y)), y, width=0.3, align='center', color='skyblue', zorder=3)
```
The important part is that the zorder of the bars is greater than grid. Experimenting it seems zorder=3 is the lowest value that actually gives the desired effect. I have no idea why zorder=1 isn't sufficient. EDIT: I have noticed this question has already been answered here using a different method although it suffers some link rot. Both methods yield the same result as far as I can see but andrew cooke's answer is more elegant.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/23357798/how-to-draw-grid-lines-behind-matplotlib-bar-graph
| 147 |
[
"python",
"matplotlib"
] | 387 | 638 | 2 |
How to hide axes and gridlines I would like to be able to hide the axes and gridlines on a 3D matplotlib graph. I want to do this because when zooming in and out the image gets pretty nasty. I'm not sure what code to include here but this is what I use to create the graph.
```
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.view_init(30, -90)
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
plt.xlim(0,pL)
plt.ylim(0,pW)
ax.set_aspect("equal")
plt.show()
```
This is an example of the plot that I am looking at:
|
```
# Hide grid lines
ax.grid(False)
# Hide axes ticks
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])
```
Note, you need matplotlib>=1.2 for set_zticks() to work.
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/45148704/how-to-hide-axes-and-gridlines
| 181 |
[
"python",
"matplotlib",
"matplotlib-3d"
] | 528 | 170 | 3 |
Pandas dataframe groupby plot I have a dataframe which is structured as:
```
Date ticker adj_close
0 2016-11-21 AAPL 111.730
1 2016-11-22 AAPL 111.800
2 2016-11-23 AAPL 111.230
3 2016-11-25 AAPL 111.790
4 2016-11-28 AAPL 111.570
...
8 2016-11-21 ACN 119.680
9 2016-11-22 ACN 119.480
10 2016-11-23 ACN 119.820
11 2016-11-25 ACN 120.740
...
```
How can I plot based on the ticker the adj_close versus Date?
|
Simple plot, you can use:
```
df.plot(x='Date',y='adj_close')
```
Or you can set the index to be Date beforehand, then it's easy to plot the column you want:
```
df.set_index('Date', inplace=True)
df['adj_close'].plot()
```
If you want a chart with one series by ticker on it You need to groupby before:
```
df.set_index('Date', inplace=True)
df.groupby('ticker')['adj_close'].plot(legend=True)
```
If you want a chart with individual subplots:
```
grouped = df.groupby('ticker')
ncols=2
nrows = int(np.ceil(grouped.ngroups/ncols))
fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(12,4), sharey=True)
for (key, ax) in zip(grouped.groups.keys(), axes.flatten()):
grouped.get_group(key).plot(ax=ax)
ax.legend()
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/41494942/pandas-dataframe-groupby-plot
| 158 |
[
"python",
"pandas",
"matplotlib",
"time-series",
"seaborn"
] | 565 | 746 | 5 |
Histogram values of a Pandas Series I have some values in a Python Pandas Series (type: pandas.core.series.Series)
```
```python
series = pd.Series([0.0,950.0,-70.0,812.0,0.0,-90.0,0.0,0.0,-90.0,0.0,-64.0,208.0,0.0,-90.0,0.0,-80.0,0.0,0.0,-80.0,-48.0,840.0,-100.0,190.0,130.0,-100.0,-100.0,0.0,-50.0,0.0,-100.0,-100.0,0.0,-90.0,0.0,-90.0,-90.0,63.0,-90.0,0.0,0.0,-90.0,-80.0,0.0,])
```
```python
series.min()
#Output
#-100.0
```
```python
series.max()
#Output
#950.0
#```
#I would like to get values of histogram (not necessary plotting histogram)... I just need to get the frequency for each interval. Let's say that my intervals are going from [-200; -150] to [950; 1000] so lower bounds are
#```
#lwb = range(-200,1000,50)
#```
#and upper bounds are
#```
#upb = range(-150,1050,50)
#```
#I don't know how to get frequency (the number of values that are inside each interval) now... I'm sure that defining lwb and upb is not necessary... but I don't know what function I should use to perform this! (after diving in Pandas doc, I think cut function can help me because it's a discretization problem... but I'm don't understand how to use it) After being able to do this, I will have a look at the way to display histogram (but that's an other problem)
```
|
You just need to use the histogram function of NumPy:
```
import numpy as np
count, division = np.histogram(series)
```
where division is the automatically calculated border for your bins and count is the population inside each bin. If you need to fix a certain number of bins, you can use the argument bins and specify a number of bins, or give it directly the boundaries between each bin.
```
count, division = np.histogram(series, bins = [-201,-149,949,1001])
```
to plot the results you can use the matplotlib function hist, but if you are working in pandas each Series has its own handle to the hist function, and you can give it the chosen binning:
```
series.hist(bins=division)
```
Edit: As mentioned by another poster, Pandas is built on top of NumPy. Since OP is explicitly using Pandas, we can do away with the additional import by accessing NumPy through Pandas:
```py
count, division = pd.np.histogram(series)
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/13129618/histogram-values-of-a-pandas-series
| 122 |
[
"python",
"pandas",
"numpy",
"matplotlib"
] | 1,232 | 930 | 4 |
matplotlib: drawing lines between points ignoring missing data I have a set of data which I want plotted as a line-graph. For each series, some data is missing (but different for each series). Currently matplotlib does not draw lines which skip missing data: for example
```
import matplotlib.pyplot as plt
xs = range(8)
series1 = [1, 3, 3, None, None, 5, 8, 9]
series2 = [2, None, 5, None, 4, None, 3, 2]
plt.plot(xs, series1, linestyle='-', marker='o')
plt.plot(xs, series2, linestyle='-', marker='o')
plt.show()
```
results in a plot with gaps in the lines. How can I tell matplotlib to draw lines through the gaps? (I'd rather not have to interpolate the data).
|
You can mask the NaN values this way:
```
import numpy as np
import matplotlib.pyplot as plt
xs = np.arange(8)
series1 = np.array([1, 3, 3, None, None, 5, 8, 9]).astype(np.double)
s1mask = np.isfinite(series1)
series2 = np.array([2, None, 5, None, 4, None, 3, 2]).astype(np.double)
s2mask = np.isfinite(series2)
plt.plot(xs[s1mask], series1[s1mask], linestyle='-', marker='o')
plt.plot(xs[s2mask], series2[s2mask], linestyle='-', marker='o')
plt.show()
```
This leads to
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/14399689/matplotlib-drawing-lines-between-points-ignoring-missing-data
| 119 |
[
"python",
"matplotlib"
] | 669 | 474 | 2 |
GridSpec with shared axes in Python This solution to another thread suggests using gridspec.GridSpec instead of plt.subplots. However, when I share axes between subplots, I usually use a syntax like the following
```
fig, axes = plt.subplots(N, 1, sharex='col', sharey=True, figsize=(3,18))
```
How can I specify sharex and sharey when I use GridSpec ?
|
First off, there's an easier workaround for your original problem, as long as you're okay with being slightly imprecise. Just reset the top extent of the subplots to the default after calling tight_layout:
```
fig, axes = plt.subplots(ncols=2, sharey=True)
plt.setp(axes, title='Test')
fig.suptitle('An overall title', size=20)
fig.tight_layout()
fig.subplots_adjust(top=0.9)
plt.show()
```
However, to answer your question, you'll need to create the subplots at a slightly lower level to use gridspec. If you want to replicate the hiding of shared axes like subplots does, you'll need to do that manually, by using the sharey argument to Figure.add_subplot and hiding the duplicated ticks with plt.setp(ax.get_yticklabels(), visible=False). As an example:
```
import matplotlib.pyplot as plt
from matplotlib import gridspec
fig = plt.figure()
gs = gridspec.GridSpec(1,2)
ax1 = fig.add_subplot(gs[0])
ax2 = fig.add_subplot(gs[1], sharey=ax1)
plt.setp(ax2.get_yticklabels(), visible=False)
plt.setp([ax1, ax2], title='Test')
fig.suptitle('An overall title', size=20)
gs.tight_layout(fig, rect=[0, 0, 1, 0.97])
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/22511550/gridspec-with-shared-axes-in-python
| 95 |
[
"python",
"matplotlib"
] | 353 | 1,131 | 2 |
Fitting a Normal distribution to 1D data I have a 1 dimensional array. I can compute the "mean" and "standard deviation" of this sample and plot the "Normal distribution" but I have a problem: I want to plot the data and Normal distribution in the same figure. I dont know how to plot both the data and the normal distribution. Any Idea about "Gaussian probability density function in scipy.stats"?
```
s = np.std(array)
m = np.mean(array)
plt.plot(norm.pdf(array,m,s))
```
|
You can use matplotlib to plot the histogram and the PDF (as in the link in @MrE's answer). For fitting and for computing the PDF, you can use scipy.stats.norm, as follows.
```
import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt
# Generate some data for this demonstration.
data = norm.rvs(10.0, 2.5, size=500)
# Fit a normal distribution to the data:
mu, std = norm.fit(data)
# Plot the histogram.
plt.hist(data, bins=25, density=True, alpha=0.6, color='g')
# Plot the PDF.
xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax, 100)
p = norm.pdf(x, mu, std)
plt.plot(x, p, 'k', linewidth=2)
title = "Fit results: mu = %.2f, std = %.2f" % (mu, std)
plt.title(title)
plt.show()
```
Here's the plot generated by the script:
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/20011122/fitting-a-normal-distribution-to-1d-data
| 191 |
[
"python",
"numpy",
"matplotlib",
"scipy"
] | 474 | 755 | 4 |
Delete a subplot I'm trying to figure out a way of deleting (dynamically) subplots in matplotlib. I see they have a remove method, but I get the error
```
NotImplementedError: cannot remove artist
```
I'm surprised that I can't find this anywhere. Does anyone know how to do this?
```py
from matplotlib import pyplot as plt
fig, axs = plt.subplots(1,3)
axs[0].plot([1,2],[3,4])
axs[2].plot([0,1],[2,3])
plt.draw()
plt.tight_layout()
```
|
Use fig.delaxes or plt.delaxes to remove unwanted subplots
```py
fig, axs = plt.subplots(1,3)
axs[0].plot([1,2],[3,4])
axs[2].plot([0,1],[2,3])
fig.delaxes(axs[1])
plt.draw()
plt.tight_layout()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/14694501/delete-a-subplot
| 167 |
[
"python",
"matplotlib",
"subplot"
] | 441 | 200 | 3 |
Keras - Plot training, validation and test set accuracy I want to plot the output of this simple neural network:
```
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(x_test, y_test, nb_epoch=10, validation_split=0.2, shuffle=True)
model.test_on_batch(x_test, y_test)
model.metrics_names
```
I have plotted accuracy and loss of training and validation:
```
print(history.history.keys())
# "Accuracy"
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
# "Loss"
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
```
Now I want to add and plot test set's accuracy from model.test_on_batch(x_test, y_test), but from model.metrics_names I obtain the same value 'acc' utilized for plotting accuracy on training data plt.plot(history.history['acc']). How could I plot test set's accuracy?
|
```
import keras
from matplotlib import pyplot as plt
history = model1.fit(train_x, train_y,validation_split = 0.1, epochs=50, batch_size=4)
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()
```
```
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/41908379/keras-plot-training-validation-and-test-set-accuracy
| 152 |
[
"python",
"matplotlib",
"keras"
] | 1,142 | 554 | 3 |
creating over 20 unique legend colors using matplotlib I am plotting 20 different lines on a single plot using matplotlib. I use a for loop for plotting and label every line with its key and then use the legend function
```
for key in dict.keys():
plot(x,dict[key], label = key)
graph.legend()
```
But using this way, the graph repeats a lot of colors in the legend. Is there any way to ensure a unique color is assigned to each line using matplotlib and over 20 lines? thanks
|
The answer to your question is related to two other SO questions. The answer to How to pick a new color for each plotted line within a figure in matplotlib? explains how to define the default list of colors that is cycled through to pick the next color to plot. This is done with the Axes.set_color_cycle method. You want to get the correct list of colors though, and this is most easily done using a color map, as is explained in the answer to this question: Create a color generator from given colormap in matplotlib. There a color map takes a value from 0 to 1 and returns a color. So for your 20 lines, you want to cycle from 0 to 1 in steps of 1/20. Specifically you want to cycle form 0 to 19/20, because 1 maps back to 0. This is done in this example:
```
import matplotlib.pyplot as plt
import numpy as np
NUM_COLORS = 20
cm = plt.get_cmap('gist_rainbow')
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_prop_cycle(color=[cm(1.*i/NUM_COLORS) for i in range(NUM_COLORS)])
for i in range(NUM_COLORS):
ax.plot(np.arange(10)*(i+1))
fig.savefig('moreColors.png')
plt.show()
```
This is the resulting figure: Alternative, better (debatable) solution There is an alternative way that uses a ScalarMappable object to convert a range of values to colors. The advantage of this method is that you can use a non-linear Normalization to convert from line index to actual color. The following code produces the same exact result:
```
import matplotlib.pyplot as plt
import matplotlib.cm as mplcm
import matplotlib.colors as colors
import numpy as np
NUM_COLORS = 20
cm = plt.get_cmap('gist_rainbow')
cNorm = colors.Normalize(vmin=0, vmax=NUM_COLORS-1)
scalarMap = mplcm.ScalarMappable(norm=cNorm, cmap=cm)
fig = plt.figure()
ax = fig.add_subplot(111)
# old way:
#ax.set_prop_cycle(color=[cm(1.*i/NUM_COLORS) for i in range(NUM_COLORS)])
# new way:
ax.set_prop_cycle(color=[scalarMap.to_rgba(i) for i in range(NUM_COLORS)])
for i in range(NUM_COLORS):
ax.plot(np.arange(10)*(i+1))
fig.savefig('moreColors.png')
plt.show()
```
| 0.8 |
matplotlib
|
https://stackoverflow.com/questions/8389636/creating-over-20-unique-legend-colors-using-matplotlib
| 149 |
[
"python",
"matplotlib",
"legend"
] | 481 | 2,040 | 3 |
End of preview. Expand
in Data Studio
Dataset Card for Dataset Name
This dataset card aims to be a base template for new datasets. It has been generated using this raw template.
Dataset Details
Dataset Description
- Curated by: [More Information Needed]
- Funded by [optional]: [More Information Needed]
- Shared by [optional]: [More Information Needed]
- Language(s) (NLP): [More Information Needed]
- License: [More Information Needed]
Dataset Sources [optional]
- Repository: [More Information Needed]
- Paper [optional]: [More Information Needed]
- Demo [optional]: [More Information Needed]
Uses
Direct Use
[More Information Needed]
Out-of-Scope Use
[More Information Needed]
Dataset Structure
[More Information Needed]
Dataset Creation
Curation Rationale
[More Information Needed]
Source Data
Data Collection and Processing
[More Information Needed]
Who are the source data producers?
[More Information Needed]
Annotations [optional]
Annotation process
[More Information Needed]
Who are the annotators?
[More Information Needed]
Personal and Sensitive Information
[More Information Needed]
Bias, Risks, and Limitations
[More Information Needed]
Recommendations
Users should be made aware of the risks, biases and limitations of the dataset. More information needed for further recommendations.
Citation [optional]
BibTeX:
[More Information Needed]
APA:
[More Information Needed]
Glossary [optional]
[More Information Needed]
More Information [optional]
[More Information Needed]
Dataset Card Authors [optional]
[More Information Needed]
Dataset Card Contact
[More Information Needed]
- Downloads last month
- 76