In short, when I use the plot command, my data is recognized as real double precision numbers, but asking for a bar graph for some reason returns the complaint that the data are strings, not reals. Here is my simple code:
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
#for counter in range(102,401):
for counter in range(400,401):
temp1=str(counter)
temp="column_1_file_"+temp1
temp2="column_2_file_"+temp1
xdata=np.loadtxt(temp,delimiter=" ",dtype=str)
ydata=np.loadtxt(temp2,delimiter=" ",dtype=str)
plt.plot(xdata,ydata,'or')
# width=1.0
# plt.bar(xdata,ydata,width,color="blue")
plt.xlabel(r' Particle Radius ($\mu$m)')
plt.ylabel("Frequency")
plt.title("Histogram of Particle Radius")
plt.xlim(-1,40)
plt.ylim(0,1.1)
# plt.show()
var1=str(counter)+".png"
plt.savefig(var1,format='png')
counter=counter+1
When executed, the program above works fine, producing the desired line plot just fine. When I comment out "plt.plot(xdata,ydata,'or')" and un-comment the plt.bar and width lines, I get the below error:
On the command line, the commands and responses given are:
python Histogram_Evolution.py
Traceback (most recent call last):
File "Histogram_Evolution.py", line 13, in <module>
plt.bar(xdata,ydata,width,color="blue")
File "/N/soft/rhel6/python/2.7.3/lib/python2.7/site-packages/matplotlib- 1.3.1-py2.7-linux-x86_64.egg/matplotlib/pyplot.py", line 2515, in bar
ret = ax.bar(left, height, width=width, bottom=bottom, **kwargs)
File "/N/soft/rhel6/python/2.7.3/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/axes.py", line 5053, in bar
self.add_patch(r)
File "/N/soft/rhel6/python/2.7.3/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/axes.py", line 1562, in add_patch
self._update_patch_limits(p)
File "/N/soft/rhel6/python/2.7.3/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/axes.py", line 1580, in _update_patch_limits
xys = patch.get_patch_transform().transform(vertices)
File "/N/soft/rhel6/python/2.7.3/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/patches.py", line 576, in get_patch_transform
self._update_patch_transform()
File "/N/soft/rhel6/python/2.7.3/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/patches.py", line 569, in _update_patch_transform
bbox = transforms.Bbox.from_bounds(x, y, width, height)
File "/N/soft/rhel6/python/2.7.3/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/transforms.py", line 821, in from_bounds
return Bbox.from_extents(x0, y0, x0 + width, y0 + height)
TypeError: cannot concatenate 'str' and 'float' objects
Here is an example of the xdata file that is read, followed by an example of the ydata file that is read:
0.3454E-03
0.3801E-03
0.4095E-03
0.4245E-03
0.4456E-03
0.4661E-03
0.4861E-03
0.5049E-03
0.5292E-03
0.5435E-03
0.5588E-03
0.5732E-03
0.5840E-03
0.5925E-03
0.6037E-03
0.6158E-03
0.6852E-03
0.9102E-03
0.1083E-02
0.1197E-02
0.1306E-02
0.1477E-02
0.1986E-02
0.2352E-02
0.2577E-02
0.2810E-02
0.3253E-02
0.4226E-02
0.5017E-02
0.5535E-02
0.6018E-02
0.6973E-02
0.9046E-02
0.1079E-01
0.1192E-01
0.1294E-01
0.1502E-01
0.1948E-01
0.2325E-01
0.2568E-01
0.2789E-01
0.3235E-01
0.4198E-01
0.5012E-01
0.5532E-01
0.6012E-01
0.781791E-01
0.782330E-01
0.105108E-02
0.108144E-02
0.180879E-04
0.544883E-03
0.545426E-03
0.544242E-04
0.298458E-06
0.287814E-06
0.329474E-06
0.473319E-07
0.829702E-07
0.123443E-08
0.411156E-07
0.421080E-07
0.218848E+00
0.279559E+00
0.234184E+00
0.291552E+00
0.240008E+00
0.449513E+00
0.485745E+00
0.434142E+00
0.473585E+00
0.457689E+00
0.576471E+00
0.551371E+00
0.493782E+00
0.456229E+00
0.409499E+00
0.497298E+00
0.529838E+00
0.529549E+00
0.533233E+00
0.531061E+00
0.635727E+00
0.654071E+00
0.653801E+00
0.655098E+00
0.653899E+00
0.712113E+00
0.721604E+00
0.721187E+00
0.721345E+00
0.726954E+00
0.793069E+00
0.806902E+00
0.805833E+00
Matplotlib's plot has a convenience function integrated that automatically converts strings to numbers. bar does not have this functionality. So in order to plot a bar plot, you need to use numerical data.
This shouldn't be a problem, just don't specify str as dtype when reading in the data
np.loadtxt(temp,delimiter=" ")
Related
I am trying to create a scatter plot with x and y grid where every point gets a color by a preassigned value:
{x: 1, y: 2, value: n}
I have a list of x and y and another list for the values, tried using this:
# make range of x(0 - 359) and y(-90 - 90)
x, y = np.meshgrid(range(0, 360), range(-90, 90))
colors = [a very long list (64800 values, one for each point)]
print(colors)
plt.scatter(x, y, c=colors)
plt.colorbar()
plt.show()
Errors:
Traceback (most recent call last):
File "C:\python3.6.6\lib\site-packages\matplotlib\colors.py", line 158, in to_rgba
rgba = _colors_full_map.cache[c, alpha]
KeyError: (1.0986122886681098, None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\python3.6.6\lib\site-packages\matplotlib\axes\_axes.py", line 4210, in scatter
colors = mcolors.to_rgba_array(c)
File "C:\python3.6.6\lib\site-packages\matplotlib\colors.py", line 259, in to_rgba_array
result[i] = to_rgba(cc, alpha)
File "C:\python3.6.6\lib\site-packages\matplotlib\colors.py", line 160, in to_rgba
rgba = _to_rgba_no_colorcycle(c, alpha)
File "C:\python3.6.6\lib\site-packages\matplotlib\colors.py", line 211, in _to_rgba_no_colorcycle
raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
ValueError: Invalid RGBA argument: 1.0986122886681098
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/amit neumark/Documents/עמית/alpha/grbs data/grbs/find_burst_location.py", line 168, in <module>
main()
File "C:/Users/amit neumark/Documents/עמית/alpha/grbs data/grbs/find_burst_location.py", line 161, in main
ra2, dec2 = chi_square(model, relations)
File "C:/Users/amit neumark/Documents/עמית/alpha/grbs data/grbs/find_burst_location.py", line 33, in chi_square
create_plot(sums)
File "C:/Users/amit neumark/Documents/עמית/alpha/grbs data/grbs/find_burst_location.py", line 134, in create_plot
plt.scatter(x, y, c=colors)
File "C:\python3.6.6\lib\site-packages\matplotlib\pyplot.py", line 2793, in scatter
verts=verts, edgecolors=edgecolors, data=data, **kwargs)
File "C:\python3.6.6\lib\site-packages\matplotlib\__init__.py", line 1785, in inner
return func(ax, *args, **kwargs)
File "C:\python3.6.6\lib\site-packages\matplotlib\axes\_axes.py", line 4223, in scatter
.format(nc=n_elem, xs=x.size, ys=y.size)
ValueError: 'c' argument has 64800 elements, which is not acceptable for use with 'x' with size 64800, 'y' with size 64800.
The problem is in your x and y data and not in the colors c parameter. Your x and y is currently a 2d array (meshgrid). It should be a list of positions. One way to do so is to flatten your 2d meshgrids to get a 1-d array. The one to one correspondence between x and y data points will be maintained. The meshgrids work normally for scatter 3d plots.
I am choosing some random colors to provide a solution.
x, y = np.meshgrid(range(0, 360), range(-90, 90))
colors = np.random.random(360*180)
plt.scatter(x.flatten(), y.flatten(), c=colors)
plt.colorbar()
It might make more sense to plot using something like imshow or pcolormesh. This creates a "heatmap" across a grid of x,y coordinates. The x,y meshgrid is optional for these functions.
colors = np.arange(64800)
plt.pcolormesh(colors.reshape(360, 180).T)
# OR #
x, y = np.meshgrid(range(0, 360), range(-90, 90))
plt.pcolormesh(x, y, colors.reshape(360, 180).T)
You should pay attention to how you reshape colors. You can fill either by rows or by columns. The default is by rows (last axis). This is also important to note in the other answer as you flatten your meshgrid.
I tried to use distplot to plot an array of double value but failed. Below is my source code:
>>> import seaborn as sns, numpy as np
>>> sns.set(); np.random.seed(0)
>>> x = np.random.randn(100)
>>> ax = sns.distplot(x)
Below is the error I got. I don't know what wrong with my code. Does anyone know the issue?
>>> ax = sns.distplot(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/anaconda/lib/python3.6/site-packages/seaborn/distributions.py", line 221, in distplot
kdeplot(a, vertical=vertical, ax=ax, color=kde_color, **kde_kws)
File "/anaconda/lib/python3.6/site-packages/seaborn/distributions.py", line 604, in kdeplot
cumulative=cumulative, **kwargs)
File "/anaconda/lib/python3.6/site-packages/seaborn/distributions.py", line 270, in _univariate_
kdeplot
cumulative=cumulative)
File "/anaconda/lib/python3.6/site-packages/seaborn/distributions.py", line 328, in _statsmodels
_univariate_kde
kde.fit(kernel, bw, fft, gridsize=gridsize, cut=cut, clip=clip)
File "/anaconda/lib/python3.6/site-packages/statsmodels/nonparametric/kde.py", line 146, in fit
clip=clip, cut=cut)
File "/anaconda/lib/python3.6/site-packages/statsmodels/nonparametric/kde.py", line 506, in kden
sityfft
f = revrt(zstar)
File "/anaconda/lib/python3.6/site-packages/statsmodels/nonparametric/kdetools.py", line 20, in
revrt
y = X[:m/2+1] + np.r_[0,X[m/2+1:],0]*1j
TypeError: slice indices must be integers or None or have an __index__ method
BTW, I am using python3.6.
This is caused by an old version of statsmodels and the problem is fixed in version 0.8.0. Upgrade it as described in https://github.com/mwaskom/seaborn/issues/1092
conda update statsmodels
I have been using lmfit for about a day now and needless to say I know very little about the library. I have been using several built-in models for curve fitting and all of them work flawlessly with the data except the Lognormal Model.
Here is my code:
from numpy import *
from lmfit.models import LognormalModel
import pandas as pd
import scipy.integrate as integrate
import matplotlib.pyplot as plt
data = pd.read_csv('./data.csv', delimiter = ",")
x = data.ix[:, 0]
y = data.ix[:, 1]
print (x)
print (y)
mod = LognormalModel()
pars = mod.guess(y, x=x)
out = mod.fit(y, pars , x=x)
print(out.best_values)
print(out.fit_report(min_correl=0.25))
out.plot()
plt.plot(x, y, 'bo')
plt.plot(x, out.init_fit, 'k--')
plt.plot(x, out.best_fit, 'r-')
plt.show()
and the error output is:
Traceback (most recent call last):
File "Cs_curve_fit.py", line 17, in <module>
pvout = pvmod.fit(y, amplitude= 1, center = 1, sigma =1 , x=x)
File "C:\Users\NAME\Anaconda3\lib\site-packages\lmfit\model.py", line 731, in fit
output.fit(data=data, weights=weights)
File "C:\Users\NAME\Anaconda3\lib\site-packages\lmfit\model.py", line 944, in fit
self.init_fit = self.model.eval(params=self.params, **self.userkws)
File "C:\Users\NAME\Anaconda3\lib\site-packages\lmfit\model.py", line 569, in eval
return self.func(**self.make_funcargs(params, kwargs))
File "C:\Users\NAME\Anaconda3\lib\site-packages\lmfit\lineshapes.py", line 162, in lognormal
x[where(x <= 1.e-19)] = 1.e-19
File "C:\Users\NAME\Anaconda3\lib\site-packages\pandas\core\series.py", line 773, in __setitem__
setitem(key, value)
File "C:\Users\NAME\Anaconda3\lib\site-packages\pandas\core\series.py", line 755, in setitem
raise ValueError("Can only tuple-index with a MultiIndex")
ValueError: Can only tuple-index with a MultiIndex
First, the error message you show cannot have come from the code you post. The error message says that line 17 of the file "Cs_curve_fit.py" reads
pvout = pvmod.fit(y, amplitude= 1, center = 1, sigma =1 , x=x)
but that is not anywhere in your code. Please post the actual code and the actual output.
Second, the problem appears to happening because the data for x is cannot be turned into a 1D numpy array. Not being able to trust your code or output, I would just suggest converting the data to 1D numpy arrays yourself as a first test. Lmfit should be able to handle Pandas series, but it just does a simple coercion to 1D numpy arrays.
I have written the following python plotting script using matplotlib:
import pynbody as pyn
import numpy as np
import matplotlib.pyplot as plt
import glob
s = pyn.load('./ballsV2.00001')
sl = s.g[np.where((s.g['z'] < 0.005) & (s.g['z']>-0.005))]
sx = s.s['x'][0]
sy = s.s['y'][0]
sz = s.s['z'][0]
r2 = ((s.g['x']-sx)**2+(s.g['y']-sy)**2+(s.g['z']-sz)**2)
Flux = np.array(1./(4*np.pi*r2)*np.exp(-1*7.00114988051*np.sqrt(r2)))
print(type(np.log10(sl['radFlux'])))
print(type(np.log10(Flux)))
plt.figure(figsize = (15,12))
#plt.scatter(sl['x'],sl['y'],c=np.log10(sl['radFlux']),s=75,edgecolors='none', marker = '.',vmin=-6,vmax=1)
plt.scatter(sl['x'],sl['y'],c=np.log10(Flux),s=75,edgecolors='none', marker = '.',vmin=-8,vmax=4)
plt.xlim([-0.5,0.5])
plt.ylim([-0.5,0.5])
plt.xlabel("x")
plt.ylabel("y")
plt.colorbar(label="log(Code Flux)")
plt.savefig('./ballsV2_0.1.pdf')
plt.savefig('./ballsV2_0.1.png')
plt.show()
plt.close()
When I run the script I get the following error:
foo#bar ~/Data/RadTransfer/Scaling_Tests/ballsV2 $ py
balls.py
balls.py:15: RuntimeWarning: divide by zero encountered in log10
print(type(np.log10(sl['radFlux'])))
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
Traceback (most recent call last):
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/colors.py", line 141, in to_rgba
rgba = _colors_full_map.cache[c, alpha]
KeyError: (-4.1574455411341349, None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/colors.py", line 192, in _to_rgba_no_colorcycle
c = tuple(map(float, c))
TypeError: 'numpy.float64' object is not iterable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "balls.py", line 17, in <module>
plt.scatter(sl['x'],sl['y'],c=np.log10(Flux),s=75,edgecolors='none', marker = '.',vmin=-8,vmax=4)
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py", line 3435, in scatter
edgecolors=edgecolors, data=data, **kwargs)
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py", line 1892, in inner
return func(ax, *args, **kwargs)
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 4028, in scatter
alpha=alpha
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/collections.py", line 890, in __init__
Collection.__init__(self, **kwargs)
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/collections.py", line 139, in __init__
self.set_facecolor(facecolors)
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/collections.py", line 674, in set_facecolor
self._set_facecolor(c)
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/collections.py", line 659, in _set_facecolor
self._facecolors = mcolors.to_rgba_array(c, self._alpha)
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/colors.py", line 237, in to_rgba_array
result[i] = to_rgba(cc, alpha)
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/colors.py", line 143, in to_rgba
rgba = _to_rgba_no_colorcycle(c, alpha)
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/colors.py", line 194, in _to_rgba_no_colorcycle
raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
ValueError: Invalid RGBA argument: -4.1574455411341349
Ignore the divide by zero stuff,the issue is the scatter plot function isn't taking my array of values to map colour to. What is strange is that the commented out scatter plot command above it runs fine. The only difference is the array of values I am passing it. I made sure to cast them to the same type (they are both <class 'numpy.ndarray'>). Also, the values themselves are more sane ranging between ~4000 and 1E-7 in the Flux array, it is only the np.log10(sl['radFlux'] that has the divide by zero errors and that one works. Any suggestions?
Flux and np.log10(sl['radFlux']) ended up being different lengths. sl (a slice of s) was not used to compute r2, so Flux ended up being to big. It would be nice if matplotlib checked that the color array was the same length as the scatter x and y arrays and had an error message like it does when the x and y arrays are different lengths.
I am new to python, so I am sorry if this is too trivial.
This is an example of the first two lines of the text file.
ra dec major_axis minor_axis position_angle
149.20562 2.29594 0.00418 0.00310 83.40
Each line of the file has 5 parameters which is needed to plot one ellipse. The first two columns are for the center. The next 3 columns are major axis, minor axis and position angle respectively. This file is part of a huge catalog which has many lines. I want to plot all those ellipses in one figure.
Here is what I tried.
import matplotlib.pyplot as plt
import numpy as np
import astropy.io.ascii as asciitable
from matplotlib.patches import Ellipse
path=/users/vishnu/Desktop/
fw=open(path + 'data_plot.txt', 'r')
table = asciitable.read(path+ "data_plot.txt")
ra_degrees=[table['ra']]
dec_degrees=[table['dec']]
major_axis_deg=[table['major_axis']]
minor_axis_deg=[table['minor_axis']]
position_angle_deg=[table['position_angle']]
for ra, dec, w, h, angle in zip(ra_degrees,
dec_degrees,major_axis_deg, minor_axis_deg, position_angle_deg):
ellipse = Ellipse(xy=(ra, dec), width=w, height=h, angle=angle)
ax.add_patch(ellipse)
plt.axis('scaled')
plt.show()
fw.close()
This is the error log.
runfile('/users/vishnu/.spyder2-py3/radio_sources.py', wdir='/users/vishnu/.spyder2-py3')
Traceback (most recent call last):
File "<ipython-input-299-a0011c0326f5>", line 1, in <module>
runfile('/users/vishnu/.spyder2-py3/radio_sources.py', wdir='/users/vishnu/.spyder2-py3')
File "/users/vishnu/anaconda3/lib/python3.5/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "/users/vishnu/anaconda3/lib/python3.5/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 88, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "/users/vishnu/.spyder2-py3/radio_sources.py", line 63, in <module>
ax.add_patch(ellipse)
File "/users/vishnu/anaconda3/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 1783, in add_patch
self._update_patch_limits(p)
File "/users/vishnu/anaconda3/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 1803, in _update_patch_limits
xys = patch.get_patch_transform().transform(vertices)
File "/users/vishnu/anaconda3/lib/python3.5/site-packages/matplotlib/patches.py", line 1409, in get_patch_transform
self._recompute_transform()
File "/users/vishnu/anaconda3/lib/python3.5/site-packages/matplotlib/patches.py", line 1398, in _recompute_transform
.scale(width * 0.5, height * 0.5) \
File "/users/vishnu/anaconda3/lib/python3.5/site-packages/matplotlib/transforms.py", line 1965, in scale
np.float_)
ValueError: setting an array element with a sequence.
Please also let me know if there is a smarter way to do this without the need to create arrays.
I'm guessing the problem is that you have one too many levels of nesting on ra_degrees, etc.
That is, if you print table['ra'] you will probably find that it is already an array. When you enclose it in square brackets, [table['ra']] will then be a list of length one. The zip function takes one item from each sequence at a time, so in your for loop, ra will be assigned table['ra'] the first and only time through the loop.
Another issue is that you are opening the file twice. You are passing the filename to asciitable.read, and are never actually using fw.
The code below might work better.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import astropy.io.ascii as asciitable
path=/users/vishnu/Desktop/
table = asciitable.read(path + "data_plot.txt")
ra_degrees = table['ra']
dec_degrees = table['dec']
major_axis_deg = table['major_axis']
minor_axis_deg = table['minor_axis']
position_angle_deg = table['position_angle']
for ra, dec, w, h, angle in zip(ra_degrees, dec_degrees,
major_axis_deg, minor_axis_deg, position_angle_deg):
ellipse = Ellipse(xy=(ra, dec), width=w, height=h, angle=angle)
ax.add_patch(ellipse)
plt.axis('scaled')
plt.show()
If asciitable works with an open file rather than a file name, then use the following instead:
with open(path + "data_plot.txt") as fw:
table = asciitable.read(fw)
This makes sure that the file is closed even if there are errors in asciitable.read that cause it to raise an exception. Given the error above, though, it looks like you have already read the data.
Seems like you need to use a PatchCollection, see this example. Try something like this
plt.figure()
ax = plt.gca()
ellipses = []
plt.xlim([0,100])
plt.ylim([0,100])
for ra, dec, w ... zip(...):
ellipse = Ellipse(xy=(ra, dec), width=w, height=h, angle=angle)
ellipses.append(ellipse)
p = PatchCollection(ellipses)
ax.add_collection(p)
plt.show()