I’m the one supporting CairoPlot and I’m very proud it came up here.
Surely matplotlib is great, but I believe CairoPlot is better looking.
So, for presentations and websites, it’s a very good choice.
Today I released version 1.1. If interested, check it out at CairoPlot v1.1
EDIT: After a long and cold winter, CairoPlot is being developed again. Check out the new version on GitHub.
For interactive work, Matplotlib is the mature standard. It provides an OO-style API as well as a Matlab-style interactive API.
Chaco is a more modern plotting library from the folks at Enthought. It uses Enthought’s Kiva vector drawing library and currently works only with Wx and Qt with OpenGL on the way (Matplotlib has backends for Tk, Qt, Wx, Cocoa, and many image types such as PDF, EPS, PNG, etc.). The main advantages of Chaco are its speed relative to Matplotlib and its integration with Enthought’s Traits API for interactive applications.
You can also use pygooglechart, which uses the Google Chart API. This isn’t something you’d always want to use, but if you want a small number of good, simple, charts, and are always online, and especially if you’re displaying in a browser anyway, it’s a good choice.
It’s all native python and does not have a busload of dependencies. I’m sure matplotlib is lovely but I’d be downloading and installing for days and I just want one measley bar chart!
It doesn’t seem to have been updated in a few years but hey it works!
If you like to use gnuplot for plotting, you should consider Gnuplot.py. It provides an object-oriented interface to gnuplot, and also allows you to pass commands directly to gnuplot. Unfortunately, it is no longer being actively developed.
It just just a package that makes it easy to generate the JSON data needed for Open Flash Charts 2, which are very beautiful. Check out the examples on the link above.
Not technically a python API, but you can use it from python, it’s reasonably fast to code for, and the results tend to look nice. If you happen to be using your plots online, then this would be an even better solution.
PLplot is a cross-platform software package for creating scientific plots. They aren’t very pretty (eye catching), but they look good enough. Have a look at some examples (both source code and pictures).
The PLplot core library can be used to create standard x-y plots, semi-log plots, log-log plots, contour plots, 3D surface plots, mesh plots, bar charts and pie charts. It runs on Windows (2000, XP and Vista), Linux, Mac OS X, and other Unices.
If you are using Matplotlib and are trying to get good figures in a LaTeX document, save as an EPS. Specifically, try something like this after running the commands to plot the image:
plt.savefig('destination_path.eps', format='eps')
I have found that EPS files work best and the dpi parameter is what really makes them look good in a document.
To specify the orientation of the figure before saving, simply call the following before the plt.savefig call, but after creating the plot (assuming you have plotted using an axes with the name ax):
Where elevation_angle is a number (in degrees) specifying the polar angle (down from vertical z axis) and the azimuthal_angle specifies the azimuthal angle (around the z axis).
I find that it is easiest to determine these values by first plotting the image and then rotating it and watching the current values of the angles appear towards the bottom of the window just below the actual plot. Keep in mind that the x, y, z, positions appear by default, but they are replaced with the two angles when you start to click+drag+rotate the image.
.eps made all my text bold and removed transparency. .svg gave me high-resolution pictures that actually looked like my graph.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
# Do the plot code
fig.savefig('myimage.svg', format='svg', dpi=1200)
I used 1200 dpi because a lot of scientific journals require images in 1200 / 600 / 300 dpi, depending on what the image is of. Convert to desired dpi and format in GIMP or Inkscape.
Obviously the dpi doesn’t matter since .svg are vector graphics and have “infinite resolution”.
Okay, I found spencerlyon2’s answer working. However, in case anybody would find himself/herself not knowing what to do with that one line, I had to do it this way:
In case you are working with seaborn plots, instead of Matplotlib, you can save a .png image like this:
Let’s suppose you have a matrix object (either Pandas or NumPy), and you want to take a heatmap:
import seaborn as sb
image = sb.heatmap(matrix) # This gets you the heatmap
image.figure.savefig("C:/Your/Path/ ... /your_image.png") # This saves it
This code is compatible with the latest version of Seaborn. Other code around Stack Overflow worked only for previous versions.
Another way I like is this. I set the size of the next image as follows:
plt.subplots(figsize=(15,15))
And then later I plot the output in the console, from which I can copy-paste it where I want. (Since Seaborn is built on top of Matplotlib, there will not be any problem.)
You can save to a figure that is 1920×1080 (or 1080p) using:
fig = plt.figure(figsize=(19.20,10.80))
You can also go much higher or lower. The above solutions work well for printing, but these days you want the created image to go into a PNG/JPG or appear in a wide screen format.
from PIL importImage
im =Image.open('dead_parrot.jpg')# Can be many different formats.
pix = im.load()print im.size # Get the width and hight of the image for iterating overprint pix[x,y]# Get the RGBA Value of the a pixel of an image
pix[x,y]= value # Set the RGBA Value of the image (tuple)
im.save('alive_parrot.png')# Save the modified pixels as .png
It’s probably best to use the Python Image Library to do this which I’m afraid is a separate download.
The easiest way to do what you want is via the load() method on the Image object which returns a pixel access object which you can manipulate like an array:
from PIL import Image
im = Image.open('dead_parrot.jpg') # Can be many different formats.
pix = im.load()
print im.size # Get the width and hight of the image for iterating over
print pix[x,y] # Get the RGBA Value of the a pixel of an image
pix[x,y] = value # Set the RGBA Value of the image (tuple)
im.save('alive_parrot.png') # Save the modified pixels as .png
Alternatively, look at ImageDraw which gives a much richer API for creating images.
Here is my working code snippet printing the pixel colours from an
image:
import os, sys
import Image
im = Image.open("image.jpg")
x = 3
y = 4
pix = im.load()
print pix[x,y]
回答 4
photo =Image.open('IN.jpg')#your image
photo = photo.convert('RGB')
width = photo.size[0]#define W and H
height = photo.size[1]for y in range(0, height):#each pixel has coordinates
row =""for x in range(0, width):
RGB = photo.getpixel((x,y))
R,G,B = RGB #now you can use the RGB value
photo = Image.open('IN.jpg') #your image
photo = photo.convert('RGB')
width = photo.size[0] #define W and H
height = photo.size[1]
for y in range(0, height): #each pixel has coordinates
row = ""
for x in range(0, width):
RGB = photo.getpixel((x,y))
R,G,B = RGB #now you can use the RGB value
Image manipulation is a complex topic, and it’s best if you do use a library. I can recommend gdmodule which provides easy access to many different image formats from within Python.
There’s a really good article on wiki.wxpython.org entitled Working With Images. The article mentions the possiblity of using wxWidgets (wxImage), PIL or PythonMagick. Personally, I’ve used PIL and wxWidgets and both make image manipulation fairly easy.
from pygame import surfarray, image, display
import pygame
import numpy #important to import
pygame.init()
image = image.load("myimagefile.jpg")#surface to render
resolution =(image.get_width(),image.get_height())
screen = display.set_mode(resolution)#create space for display
screen.blit(image,(0,0))#superpose image on screen
display.flip()
surfarray.use_arraytype("numpy")#important!
screenpix = surfarray.pixels3d(image)#pixels in 3d array:#[x][y][rgb]for y in range(resolution[1]):for x in range(resolution[0]):for color in range(3):
screenpix[x][y][color]+=128#reverting colors
screen.blit(surfarray.make_surface(screenpix),(0,0))#superpose on screen
display.flip()#update displaywhile1:print finished
You can use pygame‘s surfarray module. This module has a 3d pixel array returning method called pixels3d(surface). I’ve shown usage below:
from pygame import surfarray, image, display
import pygame
import numpy #important to import
pygame.init()
image = image.load("myimagefile.jpg") #surface to render
resolution = (image.get_width(),image.get_height())
screen = display.set_mode(resolution) #create space for display
screen.blit(image, (0,0)) #superpose image on screen
display.flip()
surfarray.use_arraytype("numpy") #important!
screenpix = surfarray.pixels3d(image) #pixels in 3d array:
#[x][y][rgb]
for y in range(resolution[1]):
for x in range(resolution[0]):
for color in range(3):
screenpix[x][y][color] += 128
#reverting colors
screen.blit(surfarray.make_surface(screenpix), (0,0)) #superpose on screen
display.flip() #update display
while 1:
print finished
I hope been helpful. Last word: screen is locked for lifetime of screenpix.
import PIL
importImage
FILENAME='fn.gif'#image can be in gif jpeg or png format
im=Image.open(FILENAME).convert('RGB')
pix=im.load()
w=im.size[0]
h=im.size[1]for i in range(w):for j in range(h):print pix[i,j]
install PIL using the command “sudo apt-get install python-imaging” and run the following program. It will print RGB values of the image. If the image is large redirect the output to a file using ‘>’ later open the file to see RGB values
import PIL
import Image
FILENAME='fn.gif' #image can be in gif jpeg or png format
im=Image.open(FILENAME).convert('RGB')
pix=im.load()
w=im.size[0]
h=im.size[1]
for i in range(w):
for j in range(h):
print pix[i,j]
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img=mpimg.imread('Cricket_ACT_official_logo.png')
imgplot = plt.imshow(img)
回答 12
如果您希望以RGB颜色代码的形式包含三位数,则以下代码应能做到这一点。
i =Image.open(path)
pixels = i.load()# this is not a list, nor is it list()'able
width, height = i.size
all_pixels =[]for x in range(width):for y in range(height):
cpixel = pixels[x, y]
all_pixels.append(cpixel)
If you are looking to have three digits in the form of an RGB colour code, the following code should do just that.
i = Image.open(path)
pixels = i.load() # this is not a list, nor is it list()'able
width, height = i.size
all_pixels = []
for x in range(width):
for y in range(height):
cpixel = pixels[x, y]
all_pixels.append(cpixel)