So here is my python code.
import matplotlib.pyplot as plt
M=[]
for i in np.arange(0.01,8,0.01):
M.append(test(i))
plt.plot(M)
plt.grid(b=True,which="major",color='#666666', linestyle='-',linewidth=0.2)
plt.show()
Where test(x) is some complicated function.
When i try to plot it python for some plots on X-axis from 1 to 800, but i want have scaled it plot from 0.01 to 8. So scaled down without changing graph.
Due to complicated form of test(x) function, i would like to use arrays, and this method of ploting.
Add an index to plot against (essentially your x-axis values):
import matplotlib.pyplot as plt
M=[]
indices = []
for i in np.arange(0.01,8,0.01):
M.append(test(i))
indices.append(i)
plt.plot(indices, M)
plt.grid(b=True,which="major",color='#666666', linestyle='-',linewidth=0.2)
plt.show()
Related
I want to add to my plot a colorbar, which has a nonlinear scale. For example, for such a plot:
I would like to have just 5 different colors on the bar on the right-hand side, instead of the gradient (don't pay attention to the plot itself; it's just an example).
I don't want to use contourf and would like to find some more general solution.
If you want to have discrete values in your colorbar, a quick way to do this would be to use the cmap=plt.cm.get_cmap() function and pass the name of whatever colormap class you are working with, along with the desired number of bins.
import matplotlib.pyplot as plt
plt.style.use('classic')
%matplotlib inline
import numpy as np
# Random Data Visualation
x = np.linspace(0, 10, 1000)
data = np.sin(x) * np.cos(x[:, np.newaxis])
plt.imshow(data, cmap=plt.cm.get_cmap('viridis', 5))
plt.colorbar()
plt.clim(-1, 1);
More documentation on everything color maps in Matplotlib [here]
Is there any way to make my x axis of the plot go from 0 to 10^5 in intervals of 10^n?
plt.plot(tau,c)
plt.xlim(0,1000)
plt.show()
I would like to add to this code.
You can used semilogx instead of plot to make a plot with log scaling on the x axis.
See here for this function's documentation.
You can then use xlim to specify the limits of the x axis. See here for this function's documenation.
Below is a code example of how to use both functions:
import numpy as np
from scipy.misc import electrocardiogram
import matplotlib.pyplot as plt
data = electrocardiogram()
plt.semilogx(data)
plt.xlim(left=0,right=100000)
This code returns the following image:
You can set a log scale to your axes. Here is an example:
plt.plot(tau,c)
plt.xscale("log")
plt.xlim(0,1000)
plt.xticks([1e0,1e1,1e2,1e3])
note: on a logscale you should not start with 0. Use 1 (1e0) or if you have values below 1, e.g. between 0.1 and 1, then use 0.1 (1e-1) as the first tick.
I was searching about how to trace function graphs, but not only linear ones, I know how to plot with simple points, they are the linear ones like this one below:
import numpy
import matplotlib.pyplot as plt
%matplotlib inline
_=plt.plot([4,7],[5,7],color ='w')
_=plt.plot([4,7],[7,7],color ='w')
ax = plt.gca()
ax.set_facecolor('xkcd:red')
plt.show()
then after a bit of searching, I've found this code:
import pylab
import numpy
x = numpy.linspace(-15,15,100) # 100 linearly spaced numbers
y = numpy.sin(x)/x # computing the values of sin(x)/x
# compose plot
pylab.plot(x,y) # sin(x)/x
pylab.plot(x,y,'co') # same function with cyan dots
pylab.plot(x,2*y,x,3*y) # 2*sin(x)/x and 3*sin(x)/x
pylab.show() # show the plot
That works perfectly! But what I'm wondering is: do we really need to use standard functions that have defined by Numpy?( like sin(x)/x here ) Or can we define a function ourselves and use it in Numpy function too, like x**3?
This solved issue, Thanks FlyingTeller
An example of y=x**3 graph:
import pylab
import numpy
x = numpy.linspace(-15,15,100) # 100 linearly spaced numbers
y = x**3 # we change this to tracer graphs as we want
# compose plot
pylab.plot(x,y)
pylab.show()
Why is the number of ticks on the x and y axes not reduced to 3 in this example?
import numpy as np
import matplotlib.pyplot as plt
fig,ax=plt.subplots(nrows=4,ncols=3)
for n in range(0,4):
for f in range(0,3):
ax[n,f].plot(range(10), range(10,20))
ax[n,f].locator_params(axis='x', nticks=3)
ax[n,f].locator_params(axis='y', nticks=3)
fig.savefig('not_3_ticks.png')
I am left with the following figure:
This also works:
import numpy as np
import matplotlib.pyplot as plt
fig,ax = plt.subplots(nrows=4,ncols=3)
for n in range(0,4):
for f in range(0,3):
ax[n,f].plot(range(10), range(10,20))
ax[n,f].xaxis.set_major_locator(plt.MaxNLocator(3))
ax[n,f].yaxis.set_major_locator(plt.MaxNLocator(3))
plt.plot()
plt.show()
fig.savefig('yes_3_ticks.png')
The reason locator_params(axis='x', nticks=3) is not working as expected is that nticks is not a valid argument to the matplotlib.ticker.AutoLocator in use.
From the documentation:
Typically one might want to reduce the maximum number
of ticks and use tight bounds when plotting small
subplots, for example::
ax.locator_params(tight=True, nbins=4)
So replace nticks by nbins.
I want to plot vertical bars instead of points. The actual data I have are irregularly spaced, so this will help visualize gaps more easily.
When I try to plot it, the best I can do are points, which don't increase in size as you zoom in!
import matplotlib
from matplotlib import pyplot as plt
import datetime
XX = [datetime.date.today()+datetime.timedelta(x) for x in range(10)]
YY = range(10)
plt.plot_date(XX,YY,'o')
Any ideas on how I can make taller/bigger (but not wider!) points?
You can use ax.vlines to plot a collection of vertical lines.
You can adjust ymin and ymax to suit your data.
import matplotlib
from matplotlib import pyplot as plt
import datetime
XX = [datetime.date.today()+datetime.timedelta(x) for x in range(10)]
plt.vlines(XX, ymin=0, ymax=1, linewidth=5)
plt.show()
Did you mean bars like this?
And here is the code:
import matplotlib
from matplotlib import pyplot as plt
import datetime
XX = [datetime.date.today()+datetime.timedelta(x) for x in range(10)]
YY = range(10)
plt.plot_date(XX,YY,'|')
plt.show()
You can change the shape of your plot by changing the third argument you pass in the plt.plot_date function.
In your code you are passing an 'o' that is why you get a dot. Here i pass bar to plot bar.