0

I'm trying to optimize the image quality i'm generating with matplotlib. the dataset is around +200 000 points and I would like to have something readable even when i'm zoom in. Based on this post : (1), I tried to generate a graph in pdf, png, change the DPI, but everytime, the results wasn't as I expected...

My code :

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import csv
import seaborn as sns


sns.set_style("darkgrid")

x=[]
y=[]

i=1

with open('data_dut_2_2019-2-22-17-34-36.csv', 'r') as csvfile:
    plots= csv.reader(csvfile, delimiter=';')
    next(plots)
    for row in plots:
        y.append(float((row[1]).replace(',','.')))
        x.append(i)

        i+=1


#plt.plot(x,y, marker=',')

f, (ax, ax2) = plt.subplots(2, 1, sharex=True)
ax.plot(x,y,  linewidth=1)
ax2.plot(x,y,  linewidth=1)

# zoom-in / limit the view to different portions of the data
ax.set_ylim(200, 1000)  # outliers only
ax2.set_ylim(0, .12)  # most of the data

# hide the spines between ax and ax2
ax.spines['bottom'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax.xaxis.tick_top()
ax.tick_params(labeltop='off')  # don't put tick labels at the top
ax2.xaxis.tick_bottom()

d = .015  # how big to make the diagonal lines in axes coordinates
# arguments to pass to plot, just so we don't keep repeating them
kwargs = dict(transform=ax.transAxes, color='k', clip_on=False)
ax.plot((-d, +d), (-d, +d), **kwargs)        # top-left diagonal
ax.plot((1 - d, 1 + d), (-d, +d), **kwargs)  # top-right diagonal

kwargs.update(transform=ax2.transAxes)  # switch to the bottom axes
ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs)  # bottom-left diagonal
ax2.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs)  # bottom-right diagonal


f.suptitle('Data from the CSV File: conso en mA')

plt.xlabel('Date')
plt.ylabel('Conso')

plt.savefig('test_init_0_1.pdf', dpi=1000)

The result :

Result zoom

Julie96
  • 331
  • 1
  • 15
  • 1
    What exactly is not expected about the result? How would you like to have it look like? – ImportanceOfBeingErnest Feb 25 '19 at 12:53
  • Hi, I would like to have a more precise graph, especially when i'm zooming in (if you look the "zoom" image you'll see that it is unreadable – Julie96 Feb 25 '19 at 13:12
  • 1
    When you zoom into a pdf with a pdf viewer, it does not adapt the linewidth. But of course you can create a pdf with a smaller linewidth (e.g. `linewidth=0.2`). – ImportanceOfBeingErnest Feb 25 '19 at 13:35
  • I tried with linewidth=0.1, but i was not fully satisfied... My goal is to have a linewidth which adapts itself when i'm zoom in (sorry, i'm not very clear ahahah) – Julie96 Feb 25 '19 at 14:02
  • You will have such "adaptive" linewidth when zooming the figure interactively (e.g. using `TkAgg` or `Qt5Agg` as backend). Once you created a pdf of it, it's fixed. The zoom functionality in pdf viewers is really just a magnifying glass. – ImportanceOfBeingErnest Feb 25 '19 at 14:11
  • So is there any other format allowing us to have an "adaptative" format ? – Julie96 Feb 25 '19 at 14:19
  • 1
    Not that I know of. It would need to be a format that allows to define sizes in percentage of viewport size or so... I've never heard of anything alike. – ImportanceOfBeingErnest Feb 25 '19 at 16:12
  • Ok... anyway, thank you for your help ! – Julie96 Feb 26 '19 at 08:55

1 Answers1

0

An answer that hasn't been discuss until now is to simply extend the format file (for instance the "classic" dimension of a .png file generated is 600x800. it is possible to have a different format by using for instance plt.rcParams["figure.figsize"] = (80,12)

see this post

Julie96
  • 331
  • 1
  • 15