# Questions tagged [matplotlib]

18040 questions

1

votes

1

answer

2.1k

Views

### Plot percentiles using matplotlib

I have three dataframes df1, df2 and df3. I combine these into one dataframe df. Now i want to find the min, 5 percentile, 25 percentile, median, 90 percentile and max for each date in the dataframe and plot it (line graph for each date) where X axis has the percentiles and Y axis has the values.
df...

1

votes

2

answer

451

Views

### Matplotlib x-axis overlap

I have two lists, x_axis which is list of time in the format of '12:30:00'. The y-axis is percent values. I need to plot all the values on a graph, however since x-axis string is too long they overlap. Is there anyway I can have matplotlib not show every single time on x-axis? Any help would be appr...

1

votes

2

answer

241

Views

### Place xticks and yticks in the pixel center on imshow plot

I am working with matplotlib to plot a heat map with some information and I want to move the xticks and the yticks to the center. I have searched in stackoverflow for previous questions but I couldn't reach one suitable for the problem. I attach my code and the image that I get:
import matplotlib.p...

1

votes

2

answer

1.2k

Views

### Convert float notation to power of 10 scientific notation in Python

I have several values given for example by 0.000001,0.00001,0.0001,....,1.0 stored in my_Array (np.array). For each of these values I need to label a different curve in a plot and store it in the legend as val = 10e-6 instead of val = 0.000001.
The second version is automatically stored if I use (fo...

1

votes

1

answer

870

Views

### Set custom seaborn color palette using hex codes, and name the colors

My company has a formal color palette so I need to use these colors in my seaborn charts. I would therefore like to set the default seaborn color palette, and give these colors easy-to-use names such as 'p' for purple and 'g' for green.
Here is what I have so far for code:
# Required libraries
impo...

1

votes

1

answer

59

Views

### Plot multiple values with matplotlib without loop

I have two nested lists with the values for the x-axis and y-axis that I want to plot in the same figure.
Whith a for loop to iterate through the values produces the expected plot, but for large lists is relatively slow. So I was looking for the same functionality but without the loop, which I thoug...

1

votes

2

answer

156

Views

### Stop showing plots in spyder

I have scripts that make lots of plots. I save them directly with plt.savefig() rather than plt.show() so I don't have to close all the windows. In spyder, they all show up in the console if I use inline (meaning I have to scroll way back to see other output), and they all show up in separate window...

1

votes

1

answer

490

Views

### Adding a trend line to a matplotlib line plot python

Apologies if this has already been asked but I can't find the answer anywhere. I want to add an overall trend line to a plt plot. Sample data:
import pandas as pd
data = pd.DataFrame({'year': [2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018,
2019],
'value': [2, 5, 8, 4, 1, 6, 10, 14, 8]})
import mat...

1

votes

2

answer

73

Views

### matplotlib matshow xtick labels on top and bottom

I'm trying to visualize a name co-occurrence matrix. This version works okay:
import pandas as pd
import numpy as np
import string
import matplotlib.pyplot as plt
n = 10
names = ['Long Name ' + suffix for suffix in string.ascii_uppercase[:n]]
df = pd.DataFrame(np.random.randint(0, 100, size=(n,n)),...

1

votes

1

answer

30

Views

### Find y value for respective x from python plot (matplotlib)

I'm trying to find the code to find the value of 'y' to a respective 'x' value by using a plotted line graph.
I've used matplotlib.pyplot to plot a graph.
The 'x' value for which I want the 'y' value is not a part of the x values array. Is there a way to find the respective 'y' value for the same?...

1

votes

1

answer

45

Views

### How can I generate numbers between x and n with a curve with Python?

I'm trying to generate a non-linear curve between two integers, let's say 0 and 25 that matches up to this curve here:
It seems like a stupid simple problem, the curve I need even looks like part of a circle, so generating a large circle, and taking the points at the bottom right would solve this, b...

1

votes

1

answer

31

Views

### Is there a way to apply a threshold to an image 1 row at a time

I am trying to apply a threshold to an image 1 row at a time. I want to be able to select the the row the threshold would begin and end. Ex. if I have a 1000 x 1000 image I would like to apply my threshold beginning at row 200 and end at row 850. Currently I am able to apply a threshold to an entire...

1

votes

1

answer

39

Views

### How to plot the normal at a point for a given parametric curve

I want to draw the normal of a curve at a specific point t_0 = 2*sp.pi/5.
The curve is given by the parametric equations x(t) = sin(3t) and y(y) = sin(4t) where t[0, 2pi]. For this type of parametric curve, the parameter equations for the normal line are given by the following equations:
Attempt:
i...

1

votes

2

answer

1.2k

Views

### Matplotlib histogram2d normalize not equal 1

Having one of those days, I can't get my head around what is going on. I have a tool which generates 2D histograms from arrays (Numpy arrays) of X,Y,Z coords (Z is unimportant at the minute). I need the results to be normalised so I can do some further calculation. However, when using the normed=Tru...

0

votes

0

answer

6

Views

### Embed Matplotlib into HWND

After scouring the web for a while now, it seems its possible to embed Python into tkinter, qt5, etc. however, I am wondering if it is possible to simply embed it into a Windows application. Similar to how Jupyter embeds with %matplotlib inline, I am wondering if a similar functionality is possible...

0

votes

1

answer

28

Views

### Plotting elements from a list

Suppose I have a list of lists, such as:
['010', '01', '7']
['010', '02', '6']
['010', '03', '24']
['010', '04', '6']
['015', '02', '8']
['015', '03', '27']
['015', '04', '27']
['020', '01', '130']
['020', '02', '140']
['020', '03', '202']
['020', '04', '3']
['021', '01', '3']
['021', '02', '4']
['0...

2

votes

1

answer

136

Views

### Animate scatter and contour plot

I am trying to generate an animated scatter plot within a functioning animated contour plot. I can get both working separately but not together.
The code below generates the contour from coordinates A and B in the df. I've attempted to include a separated animated scatter within the sample plot usin...

0

votes

0

answer

5

Views

### A simple way of freeing Memory in python

For some time now, I am trying to solve my memory issues. I wrote a program to plot (and save the fig) oscilloscope .trc files. These contain 100 million data points. Matplotlib can plot one file but as soon as I put the plot operation in a for loop, it throws a MemoryError. The reason I put the plo...

1

votes

0

answer

13

Views

### 3D surface plot of colorspace in python

My computer setup is Mac Mojave 10.14.4.
I am new to Python so I am using Jupyter Lab so that I can understand what each part is producing so please can you respond similarly.
I want to produce a 3d surface plot of a digitally printed fabric sample with z-axis plotting the color space. Created a...

0

votes

0

answer

20

Views

### How to plot this type of graph (errorbar)?

I have a dataframe:
import pandas as pd
import numpy as np
df = pd.read_csv(r'https://exploratory.io/data/kanaugust/2016-California-Election-Data-oTv4Hgd1UT/2016%20California%20Election%20Data.csv')
df['cluster'] = [3, 3, 1, 2, 1, 1, 3, 1, 1, 2, 1, 3, 2, 1, 1, 1, 2, 1, 3, 1, 3, 1, 3, 2, 1, 2, 3, 3,...

1

votes

1

answer

2.4k

Views

### Pandas Plotting with twinx

I'm attempting to have a bar plot and line plot on the same figure using Pandas matplotlib API. However, it is not going very well. I am using twinx() which seems to be the generally accepted way of accomplishing this.
Note that this is done in a Jupyter notebook, with the plot being shown inline. T...

1

votes

3

answer

1.6k

Views

### Plot some numbers in x axis

My question is related to the figure format generated using the matplotlib library.
So, in the x axis, I have the integers from 1 to 100, but in my code I plot just the data related to some numbers (e.g 1,5,77,86,97).
At the end, my code shows me a figure with x axis from 1 to 100.
How can I show ju...

1

votes

1

answer

3.1k

Views

### Using pyplot to create grids of plots

I am new to python and having some difficulties with plotting using pyplot. My goal is to plot a grid of plots in-line (%pylab inline) in Juypter Notebook.
I programmed a function plot_CV which plots cross-validation erorr over the degree of polynomial of some x where across plots the degree of pena...

1

votes

2

answer

2k

Views

### How to make matplotlib.pyplot subplots that overlap?

I am learning how to use subplots. For example:
import numpy
import matplotlib.pyplot as plt
plt.figure(1)
plt.subplot(221)
plt.subplot(222)
plt.subplot(223)
plt.show()
plt.close(1)
I am getting 3 subplots in figure1
Now I want to make a large subplot with the other subplots within the first one. I...

-2

votes

3

answer

25

Views

### Remove elements from numpy array smaller than 1

I am trying to plot large data (50 million values) but I am getting a MemoryError. Now I am trying to clear my dataset from redundant values. In my case, these are all values below 1 and above -1. One thing to keep in mind is that the plot should look the same as the original, but without the noise....

0

votes

0

answer

5

Views

### How to add box and label to pie graph exactly as shown in figure below

Trying to create a graph exactly like in figure below and want to save it as jpeg.
I have a dataframe named 'custpref' as below
tov_type count
Inpatient 7
Office Visit 6
Appointment Schedule 1
Allergy Sheet 1
Tried Code is as follows:-
def addPieGraph...

1

votes

1

answer

1.7k

Views

### pcolormesh ticks center for each data point/tile

I have some z=f(x,y) data that I would like to display in a heat map. So I am using np.meshgrid to create a (x,y)-grid and then call pcolormesh. However the ticks are not centered for each 'tile' that correspond to a data point -- in the docs, I did not find any instructions on how to center the tic...

1

votes

1

answer

2k

Views

### How to set labels in matplotlib.hlines

I'm trying to put labels for each line in matplotlib.hlines:
from matplotlib import pyplot as plt
plt.hlines(y=1, xmin=1, xmax=4, label='somelabel1')
plt.hlines(y=2, xmin=2, xmax=5, label='somelabel2')
I need a plot with two horizintal lines with labels on 'y' axis for each line.
Instead of it I get...

1

votes

1

answer

9.3k

Views

### Matplotlib dates in datetime format

I am plotting a candlestick chart (ohlc+volume) and unfortunately I can't figure out how to display the date in datetime format. Several approaches did not work.
import matplotlib.pyplot as plt
from matplotlib.finance import candlestick_ohlc
from matplotlib import style
import pandas as pd
style.use...

1

votes

1

answer

1.1k

Views

### Matplotlib boxplot width in log scale

I am trying to plot a boxplot with logarithmic x-axis. As you can see on the example below width of each box decreases because of the scale. Is there any way to make the width of all boxes same?

1

votes

1

answer

1.2k

Views

### How do I set matplotlib plot to “no fill”?

I'd like to superimpose one plot over another (they are polygons, really in some lat/lon space, using geopandas, but the plot is simply derived from matplotlib)
I have:
figZ, axZ = plt.subplots(1, figsize=(11,8.5))
Sfig = X.plot(ax=axZ, color='white', edgecolor='black', lw=0.7)
Y.plot(ax=axZ, color=...

1

votes

2

answer

786

Views

### How to add a reference line to the pyplot line chart ?

I have a dataframe for which I have drawn a line chart and added a reference number using the below code
plt.figure(figsize=(15,5))
plt.title('Parento Analysis',fontsize=20, color = 'blue')
plt.xlabel('Product', fontsize=14)
plt.ylabel('Sales Quantity', fontsize=14)
plt.plot(parento['Cum_Product%']...

1

votes

2

answer

474

Views

### Cropping image by the center

I have a PNG image of size 218, 178. I am using matplotlib's function imread to convert it into a ndarray. I would like to crop it to get the middle 64X64 part of the image.
I tried cropping with np.reshape, but it makes no sense. I also tried slicing as a normal array, but I can't get it right sin...

1

votes

1

answer

59

Views

### Long vertical bar plot with matplotlib

I need to create a Python script that plots a list of (sorted) value as a vertical bar plot. I'd like to plot all the values and save it as a long vertical plot, so that both the yticks labels and bars are clearly visible. That is, I'd like a 'long' verticalplot. The number of elements in the list v...

1

votes

2

answer

279

Views

### Plotting Time and float value using python matplotlib from File

I am having a text file with time and a float value. I have heard that it is possible to plot these two columns using matplotlib. Searched similar threads but could not make it happening. My code and Data are-
import math
import datetime
import matplotlib
import matplotlib.pyplot as plt
import csv
w...

1

votes

2

answer

66

Views

### How can I plot four subplots with different colspans?

I try to fit four images using matplotlib.pyplot like the following:
| plot1 | plot2|
| plot3 |
| plot4 |
Most examples I found cover three plots like these:
ax1 = plt.subplot(221)
ax2 = plt.subplot(222)
ax3 = plt.subplot(212)
And this plots the three plots successfully (However, I don...

1

votes

1

answer

36

Views

### How to indicate on a graph where the line started?

I would like to indicate the starting point of the graph - where the line started. This is my code
import numpy as np
from scipy.integrate import odeint
from numpy import sin, cos, pi, array
import matplotlib
from matplotlib import rcParams
import matplotlib.pyplot as plt
from pylab import fig...

1

votes

2

answer

84

Views

### How to do xticklabel with different major grid spacing in Matplotlib?

How to plot a chart with minor grid of 1, major grid of 10, and with xticklabels increment 20 units?
Here is my sample code and output with xticklabels increment every 10 units:
plt.figure(figsize=(10, 10))
ax = plt.gca()
major_ticks = np.arange(0, 60, 10)
minor_ticks = np.arange(0, 60, 1)
ax.se...

1

votes

2

answer

45

Views

### How to repeat a plot in different subplots in matplotlib?

I have
fig, (((ax1, ax2), (ax3, ax4))) = plt.subplots(ncols=2, nrows=2,
sharex='col',
sharey='row',
figsize=(12, 12))
and I want to repeat one plot, say P in all axes, something like:
P.plot(ax = [ax1, ax2, ax3, ax4], facecolor = 'none',
edgecolor = 'black')
without having to repeat the line for...

1

votes

2

answer

76

Views

### Pyplot: Plot a curve with ticks on one side

Can I use the plot() function in matplotlib.pyplot to plot curves like this which have ticks on one side?: