How to plot multiple line plots on Matplotlib programmatically? - matplotlib

I'm doing a web analytics data trying to examine the impact of emails on our traffic. The code I have for plotting is simple:
for cid in cids:
vdf = df.query('cid_short == #cid')
plt.plot(vdf['counter'],vdf['visits'], color='red', alpha=0.05)
The goal with the format is the transparency will highlight volume. The darker the region, the greater the volume in that area.
However, when I graph the plots, I see that each line is connected by the previous line, which creates weird shapes as seen in the image below.
How can I distinguish each plot programmatically (I'm dealing with 1000s of campaigns - labelled as cids).

To solve this, I identified that if there are multiple counter instances and are not grouped, then it will show the weird graph. This is important as the line chart is created based on the order of data I feed into it.
To solve this, I did the following:
for cid in cids:
vdf = df.query('cid_short == #cid').groupby(['cid_short','counter'])['visits'].sum().reset_index()
plt.plot(vdf['counter'],vdf['visits'], color='red', alpha=0.05)

Related

Altair sorting Chart

I'm trying to plot the data of my DataFarme in a groupedChart and I want the columns to preserve the order I gave them before. The data looks as follows (its not all there but its in the same way organized)
dataframe
When I plot it I get the following Graph:
graph
So the months were sorted even though I specified not to sort in the chart. I used the following code:
chart2 = alt.Chart(melted).mark_bar().encode(
column=alt.Column('variable',sort=None),
x=alt.X('room',sort=None),
y=alt.Y('value'),
color='room',
tooltip= ['room', 'value']
)
Does anyone know how I could fix that?
You've already used sort=None, which is the correct way to make scales in a non-faceted chart reflect the input order.
The missing piece is that faceted charts share scales by default (See Scale and Guide Resolution), so each facet is being forced to share an order.
If you make the x scale resolution independent, then each facet should retain the input order:
chart2 = alt.Chart(melted).mark_bar().encode(
column=alt.Column('variable',sort=None),
x=alt.X('room',sort=None),
y=alt.Y('value'),
color='room',
tooltip= ['room', 'value']
).resolve_scale(x='independent')

PowerBI: Formatting totals on combined stacked bar & line graph

I have a visualization (below) that is a combination Stack bar and Line graph . I am trying to format the data labels for the line graph totals- so that they all hover above the stacked bar. This works(with default settings) for all of the dates except the first bar (see image below- highlighted in yellow). There appears to be no formatting options available for the data labels of the line graph to control the label position (unlike in the stacked bar).
Any suggestions on how to force the totals to always appear above the bars?
A simple example file is available here https://drive.google.com/file/d/1m4qicc5gv5fCmVPiBe2m6THuHGKjacnx/view?usp=sharing
Any suggestions appreciated.
Go to Format > Y-Axis > Show secondary and set it to the Off position and it should look like this:
What's happening is that your line and bars were on independent axes, which makes them align not how you intend.
PowerBI supports Total labels for stacked visuals:
You can now turn on total labels for your stacked bar/column, stacked area, and line and stacked column charts, allowing you to see the aggregates of your data at a glance

build colorbar based on matplotlib.color.normalize

I am using matplotlib.colors.Normalize to pick colors for filling patches.
The outline of the code looks as follows:
norm = matplotlib.colors.Normalize(vmin=max(min_val,1), vmax=max_val)
mpl_patch = mpl.patches.polygon(item.shape, facecolor=norm(item.value))
plt.gca().add_patch(mpl_patch)
The second line is repeated for multiple patches. How can show a colorbar (as separate figure for the color values chosen?

Optimal display for overlapping series in a line chart

In a context of a line chart displaying time data in regular intervals where multiple series might overlap what would be the optimal way to:
A) hint the user that the chart has overlapping series?
B) give the user the capability to visualize all those series? Like spanning the series somehow?
For overlapping series in a line chart, I would keep the traditional line chart but put a label at the end of the graph with a color legend. The legend and label will help the user get information quickly.
Another version of a line chart for overlapping series can be a line area chat.
If you are not stuck on only line charts, I would suggest a bar chart. Below are some examples that you can use.
Example 1:
Example 2:
Example 3:
There are couple ways to indicate that there are overlapping series on a chart. You can increase the marker radius of one of them. The number of legend elements tells you how many series there is, too. Finally, you can distribute series on a different yAxis, with different top and height properties. Also, in styled mode, when you hover on legend item, other series opacity changes.
API Reference:
http://api.highcharts.com/highcharts/plotOptions.line.marker.radius
Examples:
http://jsfiddle.net/whsgpdyw/ - changing marker radius
http://jsfiddle.net/fuq6j4sg/ - each series on a different yAxis

Selective patterns with Matplotlib imshow without using patches

Is there a way to place patterns into selected areas on an imshow graph? To be precise, I need to make it so that, in addition to the numerical-data-carrying colored squares, I also have different patterns in other squares indicate different failure modes for the experiment (and also generate a key explaining the meaning of these different patterns). An example of a pattern that would be useful would be various types of crosshatches. I need to be able to do this without disrupting the main color-numerical data relationship on the graph.
Due to the back-end I am working within for the GUI containing the graph, I cannot utilize patches (they fail to pickle and make it from the back-end to the front-end via the multiprocessing package). I was wondering if anyone knew of another way to do this.
grid = np.ma.array(grid, mask=np.isnan(grid))
ax.imshow(grid, interpolation='nearest', aspect='equal', vmax = private.vmax, vmin = private.vmin)
# Up to here works fine and draws the graph showing only the data with white spaces for any point that failed
if show_fail and faildat != []:
faildat = faildat[np.lexsort((faildat[:,yind],faildat[:,xind]))]
fails = []
for i in range(len(faildat)): #gives coordinates with failures as (x,y)
fails.append((faildat[i,1],faildat[i,0]))
for F in fails:
ax.FUNCTION NEEDED HERE
ax.minorticks_off()
ax.set_xticks(range(len(placex)))
ax.set_yticks(range(len(placey)))
ax.set_xticklabels(placex)
ax.set_yticklabels(placey, rotation = 0)
ax.colorbar()
ax.show()