Asked  7 Months ago    Answers:  5   Viewed   31 times

I am writing a quick-and-dirty script to generate plots on the fly. I am using the code below (from Matplotlib documentation) as a starting point:

from pylab import figure, axes, pie, title, show

# Make a square figure and axes
figure(1, figsize=(6, 6))
ax = axes([0.1, 0.1, 0.8, 0.8])

labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
fracs = [15, 30, 45, 10]

explode = (0, 0.05, 0, 0)
pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True)
title('Raining Hogs and Dogs', bbox={'facecolor': '0.8', 'pad': 5})

show()  # Actually, don't show, just save to foo.png

I don't want to display the plot on a GUI, instead, I want to save the plot to a file (say foo.png), so that, for example, it can be used in batch scripts. How do I do that?

 Answers

93

While the question has been answered, I'd like to add some useful tips when using matplotlib.pyplot.savefig. The file format can be specified by the extension:

from matplotlib import pyplot as plt

plt.savefig('foo.png')
plt.savefig('foo.pdf')

Will give a rasterized or vectorized output respectively, both which could be useful. In addition, you'll find that pylab leaves a generous, often undesirable, whitespace around the image. You can remove the whitespace using:

plt.savefig('foo.png', bbox_inches='tight')
Tuesday, June 1, 2021
 
CAMason
answered 7 Months ago
48

There are many options for line styles and marker in MPL. Have a look here, here and here.

For your specific example (I quickly made up some functions and roughly plotted the first few examples):

import matplotlib.pyplot as plt
import numpy as np

x=np.arange(6)

fig=plt.figure()
fig.show()
ax=fig.add_subplot(111)

ax.plot(x,x,c='b',marker="^",ls='--',label='GNE',fillstyle='none')
ax.plot(x,x+1,c='g',marker=(8,2,0),ls='--',label='MMR')
ax.plot(x,(x+1)**2,c='k',ls='-',label='Rand')
ax.plot(x,(x-1)**2,c='r',marker="v",ls='-',label='GMC')
ax.plot(x,x**2-1,c='m',marker="o",ls='--',label='BSwap',fillstyle='none')
ax.plot(x,x-1,c='k',marker="+",ls=':',label='MSD')

plt.legend(loc=2)
plt.draw()

This should give you something like this.

enter image description here

Friday, July 30, 2021
 
KouiK
answered 4 Months ago
73

Is your server sending the .svg file down with the correct MIME type? It should be image/svg+xml.

See Nginx offers of downoload SVG instead of showing it for another question along the same lines.

Sunday, August 22, 2021
 
malrok44
answered 4 Months ago
84

OK, it appears that what I'm asking is impossible.

BTW, this was also discussed here: There is in Windows file systems a pre computed hash for each file?

Monday, August 23, 2021
 
ChriskOlson
answered 4 Months ago
66

You haven't provided a complete code snippet with a data sample, so I'm going to have to suggest a solution that builds on an example here.

In any case, you're getting that error message simply because go.Candlestick does not have a Volume attribute. And it might not seem so at first, but you can easily set up go.Candlestick as an individual trace, and then include an individual go.Bar() trace for Volumes using:

  1. fig = make_subplots(specs=[[{"secondary_y": True}]])
  2. fig.add_traces(go.Candlestick(...), secondary_y=True)
  3. fig.add_traces(go.Bar(...), secondary_y=False)

Plot:

enter image description here

Complete code:

import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd

# data
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')

# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])

# include candlestick with rangeselector
fig.add_trace(go.Candlestick(x=df['Date'],
                open=df['AAPL.Open'], high=df['AAPL.High'],
                low=df['AAPL.Low'], close=df['AAPL.Close']),
               secondary_y=True)

# include a go.Bar trace for volumes
fig.add_trace(go.Bar(x=df['Date'], y=df['AAPL.Volume']),
               secondary_y=False)

fig.layout.yaxis2.showgrid=False
fig.show()
Wednesday, September 22, 2021
 
Greg Malcolm
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share